A5下载文章资讯

分类分类

CSS中使用table-cell法来达到居中效果

2015-07-16 09:05作者:fang

这种可能是最好的方法,因为高度可以随内容改变,浏览器支持也不差。主要缺陷是会产生额外的标签,每一个需要居中的元素需要三个额外的HTML标签。

CSS中使用table-cell法来达到居中效果

HTML:

XML/HTML Code

<div class="Center-Container is-Table">

<div class="Table-Cell">

<div class="Center-Block">

<!-- CONTENT -->

</div>

</div>

</div>

CSS:

CSS Code

.Center-Container.is-Table { display: table; }

.is-Table .Table-Cell {

display: table-cell;

vertical-align: middle;

}

.is-Table .Center-Block {

width: 50%;

margin: 0 <span style="width: auto; height: auto; float: none;" id="11_nwp"><a style="text-decoration: none;" mpid="11" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=2d6719abf210fdd5&k=auto&k0=auto&kdi0=0&luki=9&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=d5fd10f2ab19672d&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3058%2Ehtml&urlid=0" id="11_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">auto</span></a></span>;

}

好处:

内容高度可变

内容溢出则能自动撑开父元素高度

浏览器兼容性好

同时注意:

需要额外的HTML标签

相关

说两句网友评论
    我要跟贴
    取消