经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
CSS样式中的各种居中方式
来源:cnblogs  作者:杨传伟  时间:2021/4/19 8:42:12  对本文有异议

1、水平居中

将margin-left和margin-right属性设置为auto,从而达到水平居中的效果。

代码:

margin:0 auto;

2、文字水平垂直居中

利用line-height设为height的一样

代码:

line-height: 200px;/*垂直居中关键*/

height: 200px;

3、利用padding和background-clip配合实现div的水平垂直居中

通过backgroun-clip设置为content-box,将背景裁剪到内容区外沿,再利用padding设为外div减去内div的差的一半

  1. 1 .parent{
  2. 2 margin:0 auto;
  3. 3 width:200px;
  4. 4 height:200px;
  5. 5 background-color:red;
  6. 6 }
  7. 7 .children {
  8. 8 width: 100px;
  9. 9 height: 100px;
  10. 10 padding: 50px;
  11. 11 background-color: black;
  12. 12 background-clip:content-box;/*居中的关键*/

4、absolute定位

 其中的margin中的值为该div宽度的一半

 利用position:absolute搭配top,left 50%,再将margin设为负值也可以对div进行水平垂直居中

  1. 1 .parent {
  2. 2 position:relative;
  3. 3 margin:0 auto;
  4. 4 width:200px;
  5. 5 height:200px;
  6. 6 background-color:red;
  7. 7 }
  8. 8 .children {
  9. 9 position:absolute;
  10. 10 left:50%;
  11. 11 top:50%;
  12. 12 margin:-25px 0 0 -25px ;
  13. 13 height:50px;
  14. 14 width:50px;
  15. 15 background-color: black;
  16. 16 }

5、text-align居中

将子div的display设为inline-block。

  1. 1 .parent {
  2. 2 text-align:center;
  3. 3 margin:0 auto;
  4. 4 width:200px;
  5. 5 height:200px;
  6. 6 background:red;
  7. 7 }
  8. 8 .children {
  9. 9 positiona;absolute;
  10. 10 margin-top:75px;
  11. 11 width:50px;
  12. 12 height:50px;
  13. 13 background: black;
  14. 14 display:inline-block;/*使其父元素text-align生效*/
  15. 15 }

 


 

图片居中

1、top

  1. 1 position:absolute;
  2. 2 right:50%;
  3. 3 bottom:50%;

2、transform

不需要定宽度的父div实现图片的水平垂直居中

  1. 1 <div class="parent">
  2. 2
  3. 3   <div class="children">
  4. 4
  5. 5     <div class="children-inline">我是水平垂直居中噢!</div>
  6. 6
  7. 7   </div>
  8. 8
  9. 9 </div>
  1. 1 .parent {
  2. 2 float: left;
  3. 3 width: 100%;
  4. 4 height: 200px;
  5. 5 background-color: red;
  6. 6 }
  7. 7 .children {
  8. 8 float:left;
  9. 9 position:relative;
  10. 10 top:50%;
  11. 11 left:50%;
  12. 12 }
  13. 13 .children-inline {
  14. 14 position: relative;
  15. 15 left: -50%;
  16. 16 -webkit-transform : translate3d(0, -50%, 0);
  17. 17 transform : translate3d(0, -50%, 0);
  18. 18 background-color: black;
  19. 19 color:white;
  20. 20 }

3、flex水平垂直居中

  1. 1 <div class="parent">
  2. 2
  3. 3   <div class="children">我是通过flex的水平垂直居中噢!</div>
  4. 4
  5. 5 </div>
  1. 1 html,body{
  2. 2 width: 100%;
  3. 3 height: 200px;
  4. 4 }
  5. 5 .parent {
  6. 6 display:flex;
  7. 7 align-items: center;/*垂直居中*/
  8. 8 justify-content: center;/*水平居中*/
  9. 9 width:100%;
  10. 10 height:100%;
  11. 11 background-color:red;
  12. 12 }
  13. 13 .children {
  14. 14 background-color:blue;
  15. 15 }

 

  1. 1 水平居中
  2. 2 .center-vertical {
  3. 3 position: relative;
  4. 4 top: 50%;
  5. 5 transform: translateY(-50%);
  6. 6 }
  7. 7
  8. 8
  9. 9
  10. 10 垂直居中
  11. 11 .center-horizontal {
  12. 12 position: relative;
  13. 13 left: 50%;
  14. 14 transform: translateX(-50%);
  15. 15 }

 

原文链接:http://www.cnblogs.com/rainbow-1/p/14674401.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号