两栏布局
两栏布局:一侧宽度固定,另一侧自适应
1、使用浮动
思路
- 左侧盒子使用float,左浮动
- 右侧盒子宽度为100%
代码
<div class="box">
<div class="left">left</div>
<div class="right">right</div>
</div>
.left {
float: left;
width: 200px;
background-color: red;
height: 400px;
}
.right {
width: 100%;
background-color: green;
height: 400px;
}
效果
2、定位
思路
- 父元素定位position:relative
- 左侧盒子position:absolute
- 右侧盒子宽度100%,margin-left:左侧盒子宽度
代码
.box {
position: relative;
}
.left {
position: absolute;
width: 200px;
height: 200px;
background-color: red;
}
.right {
width: 100%;
height: 200px;
background-color: green;
margin-left: 200px;
}
3、flex布局
思路
- 父元素display:flex
- justify-content: space-between
代码
.box {
display: flex;
justify-content: space-between;
}
.left {
width: 200px;
height: 200px;
background-color: red;
}
.right {
width: 100%;
height: 200px;
background-color: green;
}
三栏布局
两边定宽,中间自适应。
1、position
思路
- 父元素position:relative
- 左右两侧position:absolute
- 中间内容margin设置边距
代码
<div class="box">
<div class="left">left</div>
<div class="middle">middle</div>
<div class="right">right</div>
</div>
.box {
position: relative;
}
.left {
position: absolute;
top: 0;
left: 0;
width: 200px;
height: 200px;
background-color: red;
}
.right {
position: absolute;
top: 0;
right: 0;
width: 200px;
height: 200px;
background-color: red;
}
.middle {
margin-left: 200px;
margin-right: 200px;
background-color: green;
height: 200px;
}
效果
2、float
思路
- 左侧float:left
- 右侧float:right
- 中间内容margin
代码
.box {
overflow: hidden;
}
.left {
float: left;
background-color: gray;
width: 200px;
height: 200px;
}
.right {
float: right;
background-color: gray;
width: 200px;
height: 200px;
}
.middle {
height: 200px;
background-color: lightgray;
margin-left: 210px;
margin-right: 210px;
}
3、flex
思路
- 父元素display:flex
- 中间盒子flex:1
代码
.box {
display: flex;
}
.middle {
height: 100px;
background-color: red;
flex: 1;
}
.left {
height: 100px;
width: 100px;
background: green;
}
.right {
height: 100px;
width: 100px;
background: yellow;
}