CSS兩列布局,css兩列

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.