三行並列,透視表列標籤並列
1 <div class="content">2 <div class="left"></div>3 <div class="center"></div>4 <div class="right"></div>5 </div>
1 body{margin:0;padding:0;}2 .content div{3 width:100px;4 height:100px;5 background:rgb(147,172,213);6 margin:10px;7 }
方法一:float
1 .content div{float:left;}
三個div一起浮動,浮動元素無外邊距合并問題,故center左右margin都是20px。
方法二:絕對位置
1 .content {position:relative;}2 .content div{position:absolute;}3 div.center{top:0;left:110px;}4 div.right{top:0;left:220px;}
設定父元素為relative,被定位的子項目為absolute,絕對位置不佔位空間,所以此時三個元素會重疊於父元素的頂點,再用top,left布局左右margin10px。
方法三:inline-block
1 .content div{display:inline-block;}
將其變為行內塊元素,但是這樣div之間會有額外間距,且相容問題較多。
方法四:margin負值
1 div.center{2 margin-left:120px;/*100+10*2=120px 以下數值都以center左右margin為10px考慮*/3 margin-top:-110px;/*100+10=110px 要想用margin-top實現上移,則其值為負*/4 }5 div.right{6 margin-left:230px;/*100*2+10*3=230px*/7 margin-top:-110px;/*當center上去之後,原佔位空間不存在,right上移,故再移動需要的高度與center一致為100+10=110px。而如果先寫right,margin-top就應該上移(100+10)*2=220px*/8 }
在網速較慢或者高頻率重新整理時,使用margin負值會出現明顯的移動,故此法不作推薦。