方法一:容器设置height和line-height值相等

  1. 1 //css
  2. 2 .container{
  3. 3 width: 100px;
  4. 4 height: 100px;
  5. 5 line-height: 100px;
  6. 6 background-color: #ccc;
  7. 7 }
  8. 8
  9. 9 //html
  10. 10 <div class="container">
  11. 11 单行文本
  12. 12 </div>

– 优点: 适用于所有浏览器
– 缺点: 仅适用于单行文本

 

方法二: 内容设置绝对定位(position:absolute),并将top设置为50%,margin-top设置为height的一半

  1. 1 //css
  2. 2 .container{
  3. 3 width: 200px;
  4. 4 height: 200px;
  5. 5 border: 1px solid red;
  6. 6 position: relative;
  7. 7 }
  8. 8 .content{
  9. 9 background-color: #ccc;
  10. 10 height: 50px;
  11. 11 width: 100%;
  12. 12 position: absolute;
  13. 13 top: 50%;
  14. 14 margin-top: -25px;
  15. 15 }
  16. 16
  17. 17 //html
  18. 18 <div class="container">
  19. 19 <div class="content"></div>
  20. 20 </div>

– 优点: 适用于所有浏览器
– 缺点: 内容高度必须固定

 

方法三:设置vertical-align:middle;属性+:after伪元素

  1. 1 //css
  2. 2 .wrapper {
  3. 3 width: 500px;
  4. 4 height: 500px;
  5. 5 background-color: #ccc;
  6. 6 text-align: center;
  7. 7 }
  8. 8
  9. 9 .wrapper:after {
  10. 10 content: '';
  11. 11 height: 100%;
  12. 12 width: 0;
  13. 13 display: inline-block;
  14. 14 vertical-align: middle;
  15. 15 }
  16. 16
  17. 17 .align {
  18. 18 background-color: #f00;
  19. 19 width: 20%;
  20. 20 height: 20%;
  21. 21 display: inline-block;
  22. 22 vertical-align: middle;
  23. 23 }
  24. 24
  25. 25 //html
  26. 26 <div class="wrapper">
  27. 27 <div class="align"></div>
  28. 28 </div>

– 优点: 内容高度不必固定
– 缺点: 代码繁琐

 

方法四:利用flex布局(display:flex;),设置justify-content:center;垂直对齐,设置align-items:cneter;水平对齐

  1. 1 //css
  2. 2 .wrapper {
  3. 3 width: 500px;
  4. 4 height: 500px;
  5. 5 background-color: #ccc;
  6. 6 display: flex;
  7. 7 justify-content: center;
  8. 8 align-items: center;
  9. 9 }
  10. 10
  11. 11 //html
  12. 12 <div class="wrapper">
  13. 13 <div class="align">我是内容</div>
  14. 14 </div>

– 优点: 代码简单
– 缺点: flex布局浏览器兼容性不好

 

 方法五: 设置内容绝对定位(position:absolute)+移动元素位置(transform:translate(-50%,-50%);)

  1. 1 //css
  2. 2 .wrapper {
  3. 3 width: 500px;
  4. 4 height: 500px;
  5. 5 background-color: #ccc;
  6. 6 position: relative;
  7. 7 }
  8. 8
  9. 9 .align {
  10. 10 position: absolute;
  11. 11 top: 50%;
  12. 12 left: 50%;
  13. 13 transform: translate(-50%, -50%);
  14. 14 }
  15. 15
  16. 16 //html
  17. 17 <div class="wrapper">
  18. 18 <div class="align">我是内容</div>
  19. 19 </div>

– 优点: 内容高度不必固定
– 缺点: transform属性兼容性不好

 

方法六:设置内容绝对定位(position:absolute)+(margin:auto;)

  1. 1 //css
  2. 2 .wrapper {
  3. 3 position: relative;
  4. 4 width: 500px;
  5. 5 height: 500px;
  6. 6 border: 1px solid red;
  7. 7 }
  8. 8 .content{
  9. 9 position: absolute;
  10. 10 width: 200px;
  11. 11 height: 200px;
  12. 12 border: 1px solid green;
  13. 13 top: 0;
  14. 14 bottom: 0;
  15. 15 left: 0;
  16. 16 right: 0;
  17. 17 margin:auto;
  18. 18 }
  19. 19
  20. 20 //html
  21. 21 <div class="wrapper">
  22. 22 <div class="content"></div>
  23. 23 </div>

版权声明:本文为mengbing原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/mengbing/p/10869127.html