CSS设置水平、垂直居中方案总结

2018-02-1121:12:58网页制作Comments2,813 views字数 3470阅读模式

面试一家公司,被问到垂直居中的方法,我只答出了margin、table-cell、flex三种。回来之后觉得特别惭愧,于是整理了一下居中的方案做个记录,希望对大家也有帮助。
如果哪里写的不对,欢迎指正,非常感谢。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

块级元素居中 html代码部分文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

<div class="parent">
   <div class="child">child</div>
</div>

行内元素居中 html代码部分文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

<div class="parent">
   <span class="child">child</span>
</div>

水平居中

01 行内元素 text-align: center;

.parent {
   text-align: center;
}

02 块级元素 margin: auto;

(低版本浏览器还需要设置 text-align: center;)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

.parent {
    text-align: center; 
}
.child {
    margin: auto; 
    border: 1px solid blue;
}

由于本文主要想记录的是垂直居中的方案,这里水平垂直的其他方案就不做过多记录了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

垂直居中

01 行内元素(单行文字垂直居中):设置 line-height = height

.parent {
   height: 200px;
   line-height: 200px;
   border: 1px solid red;
}

02 块级元素:绝对定位(需要提前知道尺寸)

优点:兼容性不错
缺点:需要提前知道尺寸,margin-top: -(高度的一半); margin-left: -(宽度的一半);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

.parent {
    position: relative;
    height: 200px;
}
.child {
    width: 80px;
    height: 40px;
    background: blue;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-top: -20px;
    margin-left: -40px;
}

03 块级元素:绝对定位 + transform

优点:不需要提前知道尺寸
缺点:兼容性不好文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

.parent {
    position: relative;
    height: 200px;
}
.child {
    width: 80px;
    height: 40px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background: blue;
}

04 块级元素:绝对定位 + margin: auto;

优点:不需要提前知道尺寸,兼容性好
缺点:这个方法是我最喜欢用的一个,要说缺点的话,我目前还不知道。
此方法出自张鑫旭老师的博客 小tip: margin:auto实现绝对定位元素的水平垂直居中文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

.parent {
    position: relative;
    height: 200px;
}
.child {
    width: 80px;
    height: 40px;
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    background: blue;
}

05 块级元素:padding

缺点:如果高度固定,需要提前计算尺寸(只在某些特定情况适用)。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

.parent {
    padding: 5% 0;
}
.child {
    padding: 10% 0;
    background: blue;
}

06 块级元素:display: table-cell

.parent {
    width: 600px;
    height: 200px;
    border: 1px solid red;
    display: table;
}
.child {
    display: table-cell;
    vertical-align: middle;
}

或:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

这个方案是在知乎看到的,原文说是淘宝团队的方案:
用 CSS 实现元素垂直居中,有哪些好的方案? - Gino的回答 - 知乎
张鑫旭老师的博客也有提到过:
我所知道的几种display:table-cell的应用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

.parent {
    height: 300px;
    border: 1px solid red;
    display: table-cell;
    vertical-align: middle;
    /* *display: block;
    *font-size: (heightX0.873);
    *font-family: arial; */
}

同样适用于多行文字的垂直居中处理文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

HTML代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

<div class="parent">
    <span class="child">child child child child child child child child child child child child child child child child child child child childchild child child </span>
</div>

CSS代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

.parent {
    width: 400px;
    height: 300px;
    display: table-cell;        
    vertical-align: middle;
    border: 1px solid red;
}
.child {
    display: inline-block;
    vertical-align: middle;
    background: blue;
}

07 块级元素:display: flex

缺点:兼容性不好文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

.parent {
    width: 600px;
    height: 200px;
    border: 1px solid red;
    display: flex;
    align-items: center;
    justify-content: center;  /*水平居中*/
}
.child {
    background: blue;
}

08 块级元素:伪元素

这个方案是先从这位博主的文章中看到:
CSS:使用伪元素做水平垂直居中的微深入研究
然后发现张鑫旭老师的文章中也有提到:
:after伪类+content内容生成经典应用举例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

.parent {
    width: 300px;
    height: 300px;
    border: 1px solid red;
    text-align: center;
}
.child {
    background: blue;
    width: 100px;
    height: 40px;
    display: inline-block;
    vertical-align: middle;
}
.parent::before {
    content: '';
    height: 100%;
    display: inline-block;
    vertical-align: middle;            
}

09 块级元素:calc()

并不推荐,但也是个方法。
缺点:兼容性太差,需要计算。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

.parent {
    width: 300px;
    height: 300px;
    border: 1px solid red;
    position: relative;
}
.child {
    width: 100px;
    height: 100px;
    background: blue;
    padding: -webkit-calc((100% - 100px) / 2);
    padding: -moz-calc((100% - 100px) / 2);
    padding: -ms-calc((100% - 100px) / 2);
    padding: calc((100% - 100px) / 2);
    background-clip: content-box;
}

10 块级元素:inline-block

HTML代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

<div class="parent">
    <div class="child">child</div>
    <div class="brother">brother</div>
</div>

CSS代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

.parent {
    width: 400px;
    height: 400px;
    border: 1px solid red;
    position: relative;
}
.child, .brother {
    display: inline-block;
    vertical-align: middle;
}
.child {
    background: blue;
    font-size: 12px;
}
.brother {
    height: 400px;
    font-size: 0;
}

其他

当然,还有一种方法,就是使用table布局:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

<table>
     <tr>
         <td align="center" valign="middle">content</td> 
     </tr>
 </table>

因为html还要加table等标签,冗余有点多,而且结构也改变了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

作者:Suplum
链接:https://juejin.im/post/5a7a9a545188257a892998ef
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/952.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/zhizuo/952.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定