CSS vertical-align 居中,元素垂直居中的方法

一般常用的水平居中:

我们可以通过 text-align:center; 来设置

垂直居中:vertical-align:middle; //有时发现设置后没有任何样式的改变。因为 vertical-align 只对 依赖性的元素生效,即 display:inline-block;或display:table-cell; 有效。

另外还可以通过 : line-height 属性来设置行高 ,控制元素的垂直高度。

或者通过定位控制元素的位置:

#parent {position: relative;}

#child {position: absolute;top:50%;left:50%}

也可以通过 #child {padding:20% 0;}来实现元居中。

最后元素的浮动也可以实现元素垂直居中:

<div id="parent"><div id="floater"></div><div id="child">Content here</div></div>

#parent {height: 250px;}#floater {float: left;height: 50%;width: 100%;margin-bottom: -50px;}#child {clear: both;height: 100px;}



你可能感兴趣的:(CSS vertical-align 居中,元素垂直居中的方法)