CSS兩列布局,css兩列
在布局中,我們常常需要出現兩列的情況,其中左邊一列固定寬度,右邊一列隨著瀏覽器寬度變化而變化。如何?呢?
傳統的方法是利用float將左部分浮動起來,再通過margin將右部分右移,得到所需效果,代碼如下:
<!--兩列布局,其中左側固定,右側自適應--><div class="left background-color-red height-60 width-300"></div><div class=" background-color-black height-60 margin-left-301 "></div>
css代碼如下:
.color-blue {color:blue;}.color-yellow {color:yellow;}.background-color-blue {background-color: blue;}.background-color-black {background-color:black;}.background-color-red {background-color:red;}.background-color-yellow {background-color:yellow;}.height-60 {height:60px;}.border-color-red {border: 2px solid #ff0000;}.left {float:left;}.right {float:right;}.font-size-20{font-size: 20px;}.line-height-1_5{line-height: 1.5;}.width-1 {width:100%;}.width-auto {width:auto;}.width-300 {width:300px;}.width-960 {width:960px;}.width-100 {width:100%;}.inline-block {display: inline-block;}/*第一種水平置中方式*/.center-1 {margin: 0 auto;}/*第二種水平置中方式*/.center-2 {position:absolute;left:50%;margin-left:-480px;}.margin-left-301 {margin-left:301px;} 這樣就可以得到所想要的效果,
在CSS3推出的盒布局中,可以使用更加簡便的方法來更加靈活的控制這樣的布局需求,就是利用了box-flex屬性,代碼如下:
<!--兩列布局,其中左側固定,右側自適應--><div class="box"><!--盒布局--><div class="background-color-black height-60 width-300"></div><div class="background-color-red height-60 flex"></div></div>
其中css代碼如下:
.box {display: box;display: -webkit-box;}.flex {-webkit-box-flex: 1;}
該程式完整代碼位置:http://runjs.cn/code/xyhyg7tv