標籤:
這是我在一家公司面試時遇到的問題,當時沒有答上來!!
所以看到的小夥伴一定要注意了!!
變化瀏覽器寬度可看到效果:
左右中
然後我們來看看代碼:
第一種方法:(浮動)
<style type="text/css"> .left,.right,.center{ border:1px solid; height:100px; text-align: center; line-height:100px; font-size:50px; } .left{ float:left; width:100px; } .right{ float:right; width:100px; } .center{ margin:0 100px; }</style><div> <div class="left">左</div> <div class="right">右</div> <div class="center">中</div></div>
第二種方法:(絕對位置)
<style type="text/css"> .container{ position: relative; } .left,.right,.center{ position:absolute;/*增加絕對位置*/ top:0; border:1px solid; height:100px; text-align: center; line-height:100px; font-size:50px; } .left{ left:0; width:100px; } .right{ right:0; width:100px; } .center{ width:auto; left:100px; right:100px; }</style><div class="container"> <div class="left">左</div> <div class="right">右</div> <div class="center">中</div></div>
當然還有很多方法,如果你有什麼更好的方法,希望你能在下面評論出來,我們大家一起學習
【css面試題】三個DIV要求水平對齊,左右兩個DIV寬度固定為100px,中間那個DIV充滿剩餘的寬度(至少2種方法)