CSS中使用table-cell法來達到居中效果
WEB開發(fā)者 發(fā)布時間:2015-07-15 17:02:52 作者:佚名
我要評論

這篇文章主要介紹了CSS中使用table-cell法來達到居中效果,文中的示例給出了與之配合的HTML代碼,需要的朋友可以參考下
這種可能是最好的方法,因為高度可以隨內(nèi)容改變,瀏覽器支持也不差。主要缺陷是會產(chǎn)生額外的標(biāo)簽,每一個需要居中的元素需要三個額外的HTML標(biāo)簽。
HTML:
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- <div class="Center-Container is-Table">
- <div class="Table-Cell">
- <div class="Center-Block">
- <!-- CONTENT -->
- </div>
- </div>
- </div>
CSS:
CSS Code復(fù)制內(nèi)容到剪貼板
- .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>;
- }
好處:
內(nèi)容高度可變
內(nèi)容溢出則能自動撐開父元素高度
瀏覽器兼容性好
同時注意:
需要額外的HTML標(biāo)簽
相關(guān)文章
- 這篇文章主要介紹了CSS中使用transform達到布局居中的效果的方法,注意其和其他transform樣式之間的沖突問題,需要的朋友可以參考下2015-07-15
- 這篇文章主要介紹了CSS中使用負margin值來調(diào)整居中位置的方法,文中同時提到了這種常用方法的一些值得注意的地方,需要的朋友可以參考下2015-07-15
- 這篇文章主要介紹了僅使用CSS做到完全居中效果的方法,強烈推薦!需要的朋友可以參考下2015-07-15