整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

学会这几种方法,css居中其实很简单

们在使用css来布局时经常需要进行居中,有时一个属性就能搞定,有时则需要一定的技巧才能兼容到所有浏览器,利用css来实现对象的垂直居中有许多不同的方法,比较难的是应该选择哪种正确的方法。比如我们都知道 margin:0 auto;的样式能让元素水平居中,而margin: auto;却不能做到垂直居中……下面就css居中的一些常用方法做个集中的介绍。

首先是水平居中,最简单的办法当然就是:

margin:0 auto;

文字的水平居中方法:

利用line-height设为height的一样即可:

eg:

.div {

width:200px;

height: 200px;

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

text-align:center;

font-size: 36px;

background-color: #ccc;

}

绝对定位居中

父容器元素:position: relative,子元素:position:absolute;

eg:

<div class="box">

<div class="content"></div>

</div>

<style>

.box{position:relative;width:200px;height:200px;background:#999;}

.content{

width: 50%;

height: 50%;

overflow: auto;

margin: auto;

position: absolute;

top: 0; left: 0; bottom: 0; right: 0;

background:#C9F;}

</style>

效果如下所示:

!注意:高度必须定义,建议加 overflow: auto,防止内容溢出。

flex居中

介绍一下CSS3中的display:flex来实现的水平垂直居中的方法。

eg:

<div class="parent">

<div class="children">我是通过flex的水平垂直居中噢!</div>

</div>

<style>

.parent {

display:flex;

align-items: center;/*垂直居中*/

justify-content: center;/*水平居中*/

width:200px;

height:200px;

background-color:green;

}

.children {

background-color:blue;

color:#FFF;

}

</style>

效果如下所示:

这种方式最为简便,就是兼容性不好,不过随着时间的前进,各大浏览器一定会都兼容的。

里是工作狂的聚集地

职场

学术

新媒体

设计

极客

专门治愈处女座强迫症。

本文为CSS入门

翻译 redman9

原载CSS-Trick

人们经常抱怨在 CSS 中居中元素的问题,其实这个问题并不复杂,只是因为方法众多,需要根据情况从众多方法中选取一个出来。接下来,我们做一个 "决定树" 来帮我们把问题变的简单一点。首先你需要居中:

—— 水平 ——

需要居中inline或者inline-*元素(如文字或者链接)?

需要居中block类的元素?

需要居中多个block元素?

—— 垂直 ——

需要居中inline或者inline-*元素(如文字或者链接)?

需要居中block类的元素?

——既水平又垂直 ——

固定宽高

不固定宽高

使用flexbox

● ● ●

水平居中

水平居中inline或者inline-*元素

你可以轻松的在一个block元素中水平居中一个inline元素,以下代码对inlineinline-blockinline-tableinline-flex等有效。

.parent {
text-align: center;
}

水平居中block类的元素

block元素被设定固定宽度的情况下,可以使用设置元素margin-leftmargin-right的值为auto的方法实现水平居中。

.child {
width: 400px;
margin: 0 auto;
}

水平居中多个block类的元素

通过inline-block实现

.parent {
text-align: center;
}
.child {
display: inline-block;
text-align: left;
}

通过flexbox实现

.parent {
display: flex;
justify-content: center;
}

● ● ●

垂直居中

垂直居中inline或者inline-*元素

【单行】

inline/text元素可以简单的用设置相同的上下padding值达到垂直居中的目的。

.center {
pading-top: 30px; padding-bottom: 30px;
}

如果因为某种原因不能使用padding的方法,你还可以设置line-height等于height来达到目的。

.center {
height: 100px; line-height: 100px; white-space: nowrap;
}

【多行】

相同的上下padding也可以适用于此种情况,如果不能生效,你可以尝试将该元素的父元素的display设置为table,同时该元素的display设置为table-sell,然后设置vertical-align

.parent {
display: table;
width: 200px; height: 400px;
} .child {
display: table-cell;
vertical-align: middle;
}

如果上述方法不能使用,你可以尝试使用flexbox,一个单独的flexbox子元素可以轻易的在其父元素中居中。谨记,这种方法需要父元素有固定的高度。

.parent {
display: flex; justify-content: center;
flex-direction: column; height: 400px;
}

如果上述两种方式均不能使用,你可以使用“幽灵元素”技术,这种方法采用伪元素::before撑开高度 ,文字垂直居中。

.parent {
position: relative;
} .parent::before {
content: " "; display: inline-block; height: 100%; width: 1%; vertical-align: middle;
} .child {
display: inline-block;
vertical-align: middle; }

垂直居中block类的元素

【已知元素高度】

.parent { 
position: relative; } .child {
position: absolute;
top: 50%;
height: 100px;
margin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */
}

【未知元素高度】

.parent {
position: relative; } .child {
position: absolute;
top: 50%;
transform: translateY(-50%); }

【使用flexbox

.parent {
display: flex;
flex-direction: column;
justify-content: center; }

● ● ●

既水平又垂直

【固定宽高】

.parent {
position: relative; } .child {
width: 300px;
height: 100px;
padding: 20px;
position: absolute;
top: 50%;
left: 50%;
margin: -70px 0 0 -170px; }

【不固定宽高】

.parent { position: relative; } .child { position: absolute; top: 50%; left: 50%;
transform: translate(-50%, -50%); }

【使用flexbox

.parent { display: flex; justify-content: center; align-items:center; }
<div id="parent">
<!-- 定义子级元素 -->
<div id="child">居中布局</div>
</div>

过以下CSS样式代码实现水平方向居中布局效果

.parent{position:relative;}
.child{position:absolute;left:50%;transform: translateX(-50%)}

优点:

父级元素是否脱离文档流, 不影响子集元素水平居中效果

缺点:transform属性是CSS3中新增属性, 浏览器支持情况不好