主题
两栏布局
最终效果
- 左边宽度固定,右边⾃适应
HTML
以下 css 均适用
html
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
左边脱离文档流(display:absolute
、float
),右边需要 margin-left
- 绝对定位
css
.container{ position: relative; }
.left{
position: absolute;
width: 150px;
height: 300px;
}
.right{
height: 300px;
margin-left: 150px;
}
- 浮动
css
.left{
float: left;
width: 150px;
height: 300px;
}
.right{
height: 300px;
margin-left: 150px;
}
右边脱离文档流(display:absolute
),需要right:0
css
.container{ position: relative; }
.left{
width: 150px;
height: 300px;
}
.right{
position: absolute;
top: 0;
right: 0;
left: 150px;
bottom: 0;
height: 300px;
}
flex
css
.container{ display: flex; }
.left{
width:150px;
height:300px;
}
.right{
flex: 1;
height: 300px;
}