http://www.cnblogs.com/StormSpirit/archive/2012/10/24/2736453.htmljavascript
上集《CSS深刻研究:display的恐怖故事解密(1) - display-inline》已經把display的屬性列表拉出來溜了,發如今這個屬性恐怖面貌其實都是脆弱的假裝。除了部分經常使用屬性外,其餘的都是傳說哥。既然是傳說哥,請不要迷戀。就讓傳說繼續傳說着吧。此次咱們來搞定table-cell這個貨。(table-cell:IE六、7不支持)css
table-cell這個傢伙在國外的網站中偶有露頭,天朝因爲IE六、7這兩個貨氾濫成災,難有發揮,那麼,這個傢伙到底能幹些什麼呢?先讓咱們來研究下table,那些年曾經使用的table佈局爲什麼如此輝煌盪漾呢?她的特色有哪些呢?拋棄table的兼容性、seo、加載等與本文無關的內容不談,只看屬性,那麼就兩個特色:html
1.同行等高。java
2.寬度自動調節。瀏覽器
那麼table-cell是否是具有這個特色呢?答案是yes,爲何呢?css中有一個有意思的規則「建立匿名錶格元素」。拿table-cell來扯,就是,當某個元素被設置爲display:table-cell的時候,若是她的父節點不是display:table-row,爺爺節點不是display:table,那麼下面就是見證奇蹟的時候,這個兒子生出了他的爸爸和爺爺(瀏覽器會自動建立者兩個匿名盒對象)雖然你找不到你的father 和 grandfather,但這確實發生了,這真的是難以想象的事情,哪裏難以想象,沒有掌聲最難以想象。^_^佈局
那麼下面來看幾種狀況,幫助咱們瞭解這個難以想象的事情。我先編寫代碼以下:網站
<style type="text/css"> div{padding:10px 0;} .classtd, td{height:34px; padding:10px; margin:10px; border:1px solid #ccc; vertical-align:middle;} .classtd{display:table-cell; border-color:#cc0;} </style> <div class="classtd">tom</div> <div class="classtd">jack</div> <div>普通 div</div> <div class="classtd">angel</div> <div>======= 上面是div 下面是table ========</div> <table cellpadding="0" cellspacing="0"> <tr> <td class="dtc">tom</td> <td class="dtc">jack</td> </tr> </table> <table style="margin-top:10px;" cellpadding="0" cellspacing="0"> <tr> <td>angel</td> </tr> </table>
而後,我預測瀏覽器顯示以下:spa
好吧,我認可我先看了效果,而後就能夠下結論了:tom 和 jack 搞基,生出了父親和爺爺(瀏覽器會建立一個表格來包裹相鄰的display:table-cell元素),表現和第一個表格相同。angel本身生出了父親和爺爺表現和第二個表格相同。code
既然是這樣,那麼想了解table-cell,就是變相瞭解表格的td了。那就回到了前面所說的兩個特色:同行等高,寬度自動調節。orm
既然是這樣,那麼咱們就能夠拿這個貨來做等高佈局:
<style type="text/css"> .classtd{padding:10px; margin:10px; border:1px solid #ccc; vertical-align: top;} .classtd{display:table-cell; border-color:#cc0;} </style> <div class="classtd"> <p>大人。<br />其實我以爲你們別問元芳,元芳不是神人,<br />也不會武功,也許仍是個智障,<br />我就不信我在這裏黑元芳<br />他會忽然飛檐走壁來到我身後<br />把個人頭按在鍵盤上yu7jhklhgjkfgt;/.";. yujh bnujm798u7jrtb5 tq1qwsewrt5 </p> </div> <div class="classtd"><p>我和左邊等高</p></div>
把這貨和vertical-align:middle搞在一塊兒能夠進行大小不固定元素的垂直居中佈局(還有多行文本垂直居中):
<style type="text/css"> .classtd{ display: table-cell; padding:10px;margin:10px;border:1px solid #ccc;} .classtd div{ display: inline-block; vertical-align: middle;} </style> <div class="classtd"> <div style="padding:40px 80px 10px 10px; background: #639146; color:#fff;">div+css</div> <div style="padding:60px 80px 10px 10px; background: #2B82EE; color:#fff;">javascript</div> <div style="padding:70px 80px 10px 10px; background: #F57900; color:#fff;">HTML5</div> <div style="padding:80px 80px 10px 10px; background: #BC1D49; color:#fff;">CSS3</div> </div>
利用列寬度自動調節這個特色能夠做左固定右自適應佈局:
<style type="text/css"> .left{float:left; width:260px; padding:10px; margin-right:10px; border:1px solid #ccc;} .classtd{ display: table-cell; width:3000px; padding:10px; border:1px solid #ccc;} </style> <div class="left">我是左邊欄目</div> <div class="classtd"> 我是自適應的右邊 </div>
有童鞋可能會對這個佈局中的width:3000px感到迷惑。那麼下面就貼上這個佈局的原理
display:table-cell 元素生成的匿名table默認table-layout:auto。寬度將基於單元格內容自動調整。因此設置width:3000px的用途是儘量的寬的意思。這樣就能夠達到自適應的效果。
暴風烈酒說過:「任何恐怖的外表都掩飾不了簡單的實質。」(好吧是我狗血了^_^)原來這貨就這點料,至於display:table-cell的其餘應用,好比多行文本居中,以及以上應用的兼容考慮。你能夠伸展你的小粉拳or沙包同樣大的拳頭,敲擊鍵盤,碼一碼。瞭解原理,一切都是那麼so easy,是不?^_^