CSS之元素水平居中

html-css010

CSS之元素水平居中,第1张

我们以一个面试题开始吧

这道题看似貌似很简单,但是我们需要分析具体的场景,因为不同的显示方式的使用利弊是不一样的。

以下的讨论都是子元素相对于其父元素的水平居中。垂直居中,以后会说到。

常见的行内元素有 span img 等,对这些元素居中设置,只需要在父元素的css中添加 text-align:center 即可。同样它也对行内块元素也是有效的。

但是,有个缺点,由于 text-align 是可继承属性,即父元素内部的所有元素都会继承这个属性,从而它的子元素内部的文本都会居中显示了。因此需要对子元素的文本居中方式单独设定。

针对块级元素的居中,我们将分为定宽和不定宽两种情况来讨论。

1.定宽

(1)子元素是正常流布局

若子元素的宽度是已知的,那么我们可以设置子元素的左右 margin 为 auto 即可

行内块元素也算块级元素,同样适用

目前所有浏览器都是支持的

(2)子元素绝对定位布局(absolute)

如果子元素是绝对定位的,由于子元素此时是脱离文档流,上面的方法就失效了。这时需要明确指出子元素的定位,四个方位均要指定。如下:

这样也可以实现绝对定位元素的居中。原因可点击 这里 。如果绝对定位子元素的margin为auto,你会发现它水平垂直居中了!此方法仅适用于IE8+浏览器中,IE7就挂了,不过你可以下面这个方法:

2.不定宽

不定宽元素如果要居中需要额外的辅助手段

(1)css3新特性 transform

如果你对浏览器并没有什么特别要求,那么可以使用css3提出的新属性 transform 完成居中需求

这里需要子元素是绝对定位

(2)借助table元素

上面定宽元素设定左右margin为auto之所以可以居中,原因是左右margin会平分父元素剩下的空间。有个元素除外,那就是tabel元素。table有趣的地方在于它本身并不是块级元素,如果不给它设定宽度的话,它的宽度由内部元素的宽度“撑起”,但即使不设定它的宽度,仅设置margin-left:auto和margin-right:auto就可以实现水平居中。

最大的缺点想必你也是知道,就是无关标签太多,加深了嵌套的层级,维护性很差。

(3)父元素使用float布局

这里需要多添加一层父元素,父子元素均float布局,之后设置position为relative,left为50%

缺点是你需要额外处理浮动所带来的一些问题。并且如果你设置了背景色,布局会有些混乱

以上是我对元素实现居中的一些方法,欢迎大家补充。

2017.6.11 晴

于上海浦东

1、绝对定位+margin:auto

<style type="text/css">

.wrp {        background-color: #b9b9b9       width: 240px       height: 160px   }

.box {        color: white       background-color: #3e8e41       width: 200px       height: 120px       overflow: auto   }

.wrp1 { position: relative}

.box1 {        margin: auto       position: absolute       left: 0right: 0top: 0bottom: 0   }</style><div class="wrp wrp1">

<div class="box box1">

<h3>完全居中层1:</h3>

<h3>开发工具 【 WeX5 】: 高性能轻架构、开源免费、跨端、可视化</h3>

</div></div>1234567891011121314151617181920212223242526

效果: 

实现原理:利用css定位规则,设置左右、上下方向定位为0,margin为auto,让css根据定位计算margin值,用hack的方式实现居中。居中块(绿色)的尺寸需要可控,因为css计算margin时也需要参考尺寸值,由于四周为0,所以自动计算的尺寸是与父容器一样的。无论是设置width、height或者是 max-height、max-width,都是让尺寸不会扩大到与父级一样。

2、绝对定位+margin反向偏移

</style><style type="text/css">

.wrp2 { position: relative}

.box2 {        position: absolute       top: 50%left: 50%       margin-left: -100px/* (width + padding)/2 */

margin-top: -75px/* (height + padding)/2 */

}</style><div class="wrp wrp2">

<div class="box box2">

<h3>完全居中方案二:</h3>

<h3>开发工具 【 WeX5 】: 高性能轻架构、开源免费、跨端、可视化</h3>

</div></div>12345678910111213141516

效果: 

实现原理:由于top、left偏移了父对象的50%高度宽度,所以需要利用margin反向偏移居中块的50%宽高。而margin中不能使用百分比,因为百分比是针对父对象的,所以需要手动计算定值指定margin值。这个方案需要固定尺寸值,以此来计算margin反向偏向值,所以方案2比方案1稍差!

3、绝对定位+transform反向偏移

<style type="text/css">

.wrp3 { position: relative}

.box3 {        margin: auto       position: absolute       top: 50%left: 50%       -webkit-transform: translate(-50%, -50%)       -ms-transform: translate(-50%, -50%)       transform: translate(-50%, -50%)   }</style><div class="wrp wrp3">

<div class="box box3">

<h3>完全居中方案三:</h3>

<h3>开发工具 【 WeX5 】: 高性能轻架构、开源免费、跨端、可视化</h3></div>12345678910111213141516

效果: 

实现原理:方案3与方案2原理一样!不同点是使用了transform来代替margin做反向偏移,由于transform的计算基准是元素本身,所以这里可以用50%来做反向偏移。这个方案也需要固定尺寸值,浏览器以此为基准来计算定位!

4、display:tabel

<style type="text/css">

.wrp4 { display: table}

.subwrp4 {        display: table-cell       vertical-align: middle   }

.box4 {        margin: auto       overflow-wrap: break-word       height: auto       max-height: 80%       max-width: 80%   }</style><div class="wrp wrp4">

<div class="subwrp4">

<div class="box box4">

<h3>完全居中方案四:</h3>

</div>

</div></div>123456789101112131415161718192021

效果: 

实现原理:方案4是实现效果比较好的,居中块的尺寸可以做包裹性,缺点是增加了一层table-cell层来实现垂直居中。方案4的居中块可以设置 max-height、max-width,而且居中块是可以具有垂直方向的包裹性的。水平方向由于是在table-cell里面的,所以会直接显示max-width,也就是宽度趋大。

5、display: inline-block

<style type="text/css">

.wrp5 {        text-align: center       overflow: auto   }

.box5 {        display: inline-block       vertical-align: middle       width: auto       height: auto       max-width: 90%       max-height: 90%   }

.wrp5:after {        content: ''       display: inline-block       vertical-align: middle       height: 100%       margin-left: -0.25em       /* To offset spacing. May vary by font */

}</style><div class="wrp wrp5">

<div class="box box5">

<h3>完全居中方案五:</h3>

<h3>开发工具 【 WeX5 】: 高性能轻架构、开源免费、跨端、可视化</h3>

</div></div>12345678910111213141516171819202122232425262728

效果: 

实现原理:原理:利用inline-block的vertical-align: middle去对齐after伪元素,after伪元素的高度与父对象一样,就实现了高度方向的对齐。方案5实现效果更加好,居中块的尺寸可以做包裹性、自适应内容,兼容性也相当好。缺点是水平居中需要考虑inline-block间隔中的留白(代码换行符遗留问题。)。方案4的居中块可以设置 max-height、max-width,而且居中块是可以具有水平垂直两个方向的自适应。

6、display: flex-box

<style type="text/css">

.wrp6 {        display: -webkit-flex       display: -moz-box       display: -ms-flexbox       display: -webkit-box       display: flex       -webkit-box-align: center       -moz-box-align: center       -ms-flex-align: center       -webkit-align-items: center       align-items: center       -webkit-box-pack: center       -moz-box-pack: center       -ms-flex-pack: center       -webkit-justify-content: center       justify-content: center   }

.box6 {        width: auto       height: auto       max-width: 90%       max-height: 90%   }</style><div class="wrp wrp6">

<div class="box box6">

<h3>完全居中方案六:</h3>

<h3>开发工具 【 WeX5 】: 高性能轻架构、开源免费、跨端、可视化</h3>

</div></div>1234567891011121314151617181920212223242526272829303132

效果: 

实现原理: flexbox布局。此乃布局终极大法,专治各种布局定位难题!优点:能解决各种排列布局问题,实现方式符合人类认知。缺点:PC端某些旧浏览器支持度不高。