CSS 水平垂直居中

聚沙成塔,聚水成涓!

line-height

父元素高度确定的单行文本设置 height=line-height

   body {
        background: black;
    }
    
    .c2 {
        height: 80px;
        line-height: 80px;
        width: 80px;
        text-align: center;
        background: red;
    }
   <body>

<div class="block">
    123123
</div>
</body>
image.png

只是单行文本水平垂直居中,块级元素并没有

position:absolute 实现水平垂直居中方法一

使用position:absolute,设置 left、top、margin-left、margin-top

  .c1 {
        height: 300px;
        width: 300px;
        background: black;
        position: relative;
    }
    
    .c2 {
        height: 200px;
        width: 200px;
        position: absolute;
        left: 50%;
        top: 50%;
        margin-top: -100px;
        margin-left: -100px;
        background: red;
    }
     <div class="c1">
        <div class="c2">
        </div>
    </div>
image.png

特点:兼容性好,不过需固定宽高
注意:如果不设置c1 position为relative,则c2将基于根元素定位居中

position: absolute 实现水平垂直居中方法二

position:absolute,同时设置top/bottom/right/left

body {
        background: black;
    }
    
    .block {
        height: 200px;
        width: 200px;
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
        background: red;
    }
image.png

特点: 可不用设置高宽,且兼容性好

position: fixed 可实现水平垂直居中方法一

使用position:fixed,同样设置left、top、margin-left、margin-top的属性

 body {
        background: black;
    }
    
    .block {
        height: 200px;
        width: 200px;
        position: fixed;
        left: 50%;
        top: 50%;
        margin-top: -100px;
        margin-left: -100px;
        background: red;
    }

  <body>

<div class="block">
    </div>
</body>

特点:position:fixed; IE 不支持,且需固定宽高

position: fixed 实现水平垂直居中方法二

设置position:fixed ,同时设置left/right/top/bottom为0,margin:auto

   body {
        background: black;
    }
    
    .block {
        height: 200px;
        width: 200px;
        position: fixed;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
        background: red;
    }
    <body>

<div class="block">
    </div>
</body>

特点: position:fixed IE 不支持,不过不需固定宽高

display: table-cell

display:table-cell属性使内容垂直居中

 body {
        background: black;
    }
    
    .block {
        height: 200px;
        width: 200px;
        display: table-cell;
        vertical-align: middle;
        text-align: center;
        background: red;
    }
<body>
<div class="block">
    123123
  </div>
</body>
image.png

特点:使内容居中,块级元素不能居中

CSS3 display: -webkit-box

使用css3的display:-webkit-box属性,再设置-webkit-box-pack:center/-webkit-box-align:center

  body {
        display: -webkit-box;
        -webkit-box-pack: center;
        -webkit-box-align: center;
        background: black;
    }
    
    .block {
        height: 200px;
        width: 200px;
        color: yellow;
        display: -webkit-box;
        -webkit-box-pack: center;
        -webkit-box-align: center;
        background: red;
    }
    <body>
       <div class="block">
           <span>123123</span>
      </div>
 </body>
image.png

特点: 无需定宽高,行内和块级元素都可水平垂直居中!但CSS3 IE 兼容性要考虑

CSS3 transform:translate(x,y)

 body {
        background: black;
    }
    
    .block {
        height: 200px;
        width: 200px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        -webkit-transform: translate(-50%, -50%);
        -moz-transform: translate(-50%, -50%);
        -ms-transform: translate(-50%, -50%);
        background: red;
    }
    <body>
       <div class="block">
         <span>123123</span>
       </div>
    </body>
image.png

特点: 无需固定宽高但只能使块级元素水平垂直居中!但CSS3 在 IE 兼容性需要考虑

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 水平居中元素: 方式一:CSS3 transform .parent { position: relative...
    心羽暖姐姐阅读 2,812评论 0 3
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,687评论 1 92
  • 水平居中方案 行内元素的水平居中对父元素设置 text-align: center; 我是行内元素 在父...
    FConfidence阅读 3,146评论 0 1
  • 在春熙路有两个乞丐,都是男性。 早上上班的时候会见到一个, 晚上下班的时候会见到另外一个。 早上的乞丐,衣着除了脏...
    菊大侠说阅读 3,751评论 0 1
  • 周一挨骂了啊。惯用套路,给你们上劲儿,也确实做得不是特别好。拿结果,不只做过程。之后就是封闭了。run as a ...
    筱筱殿下阅读 1,058评论 1 1

友情链接更多精彩内容