方法1:margin + 双absolute
<style>
html, body {
margin: 0;
height: 100%;
box-sizing: border-box;
}
div {
height: 100%;
}
.box {
position: relative;
}
// tip:这里其实left不用定位用左浮动也是可以的
.left {
position: absolute;
top: 0;
left: 0;
width: 200px;
background-color: lightblue;
}
.right {
position: absolute;
top: 0;
right: 0;
width: 200px;
background-color: lightcoral;
}
.middle {
margin-right: 200px;
margin-left: 200px;
background-color: lightgoldenrodyellow;
}
</style>
<div class="box">
<div class="left">
</div>
<div class="middle">
</div>
<div class="right"></div>
</div>
方法2:双float,中间margin
<style>
html,
body {
margin: 0;
height: 100%;
box-sizing: border-box;
}
div {
height: 100%;
}
.box {
position: relative;
}
.left {
float: left;
width: 200px;
height: 200px;
background: green;
}
.right {
float: right;
width: 200px;
height: 200px;
background: red;
}
.middle {
margin-left: 210px;
margin-right: 210px;
background: black;
height: 200px;
}
</style>
<div class="box">
这里必须是left-right-middle这个顺序
left-middle-right这个顺序会出错
<div class="left"></div>
<div class="right"></div>
<div class="middle">
</div>
</div>
方法3:圣杯布局?
https://mp.weixin.qq.com/s/Im3uNOOWdElFvPFAsCOfZA
方法4:双飞翼?
https://mp.weixin.qq.com/s/Im3uNOOWdElFvPFAsCOfZA
方法5:Flex
<style>
html,
body {
height: 100%;
margin: 0;
}
.container {
display: flex;
height: 100%;
}
.middle {
flex: 1 0;
background-color: lightblue;
}
.left {
flex: 0 0 250px;
background-color: lightcoral;
}
.right {
flex: 0 0 250px;
background-color: lightgoldenrodyellow;
}
</style>
<body>
<div class="container">
<div class="left">left</div>
<div class="middle">middle</div>
<div class="right">right</div>
</div>
</body>