天天看點

html兩欄 自适應布局,CSS中2種最基本的兩列自适應布局

CSS中最基本的兩列自适應布局,你掌握過沒呢,分享下呢。

左邊自适應寬度需要用.wrap包裹起來,設定.wrap的margin-right為負值,内部.left的margin-right為正值,兩個值相等,互相抵消。

方式一:.container{

width: 100%;

height: 50px;

border: 1px solid #000;

}

.wrap{

width: 100%;

line-height: 50px;

background-color: greenyellow;

float: left;

margin-right: -200px;  

}

.wrap > .left{

text-align: right;

margin-right: 200px;

}

.right{

width: 200px;

line-height: 50px;

background-color: plum;

float: right;

}

左邊自适應 右邊固定寬度200px

方式二:如果使用flex布局,就友善多了:.container{

width: 100%;

height: 50px;

border: 1px solid #000;

display: flex;

}

.left{

width: 100%;

line-height: 50px;

background-color: chocolate;

text-align: right;

flex-grow: 1;

}

.right{

width: 200px;

line-height: 50px;

background-color: plum;

float: right;

}

左邊自适應 右邊固定寬度200px

OK,大家可以試着弄下呢。