1. 左边定宽,右边自适应
<div class="left"></div>
<div class="right"></div>
方案一:float + margin
.left {
height: 100px;
width: 120px;
float: left;
background-color: green;
}
.right {
height: 100px;
margin-left: 120px;
background-color: yellow;
}
方案二:float + calc
.left {
height: 100px;
width: 120px;
background-color: red;
float: left;
}
.right {
height: 100px;
width: calc(100% - 120px);
float: left;
background-color: purple;
}
2. 左右两边定宽,中间自适应
- 方案一:float
- 方案二:float + calc
- 方案三: 定位
- 方案四: flex
- 方案五: 圣杯布局(设置BFC,margin负值法) ===》待定
<div class="wrap">
<div class="left"></div>
<div class="right"></div>
<div class="center"></div>
</div>
方案一
.wrap {
width: 100%;
height: 200px;
}
.wrap > div {
height: 100%;
}
.left {
width: 120px;
float: left;
background-color: red;
}
.right {
width: 120px;
float: right;
background-color: greenyellow;
}
.center {
margin: 0 120px;
background-color: green;
}
方案二
.left {
width: 120px;
float: left;
background-color: orange;
}
.right {
float: right;
width: 120px;
background-color: red;
}
.center {
width: calc(100% - 240px);
margin-left: 120px;
background-color: purple;
}
方案三
.wrap{
height: 100px;
position: relative;
padding-left: 100px;
padding-right: 200px;
}
.left {
width: 100px;
height: 100px;
background: red;
position: absolute;
left: 0;
top: 0;
}
.center {
background: green;
height: 100px;
}
.right {
width: 200px;
height: 100px;
background: yellow;
position: absolute;
right: 0;
top: 0;
}
方案四
.wrap {
display: flex;
}
.left {
width: 120px;
background-color: yellowgreen;
}
.right {
width: 120px;
background-color: hotpink;
}
.center {
flex: 1;
background-color: gold;
}