一、三个元素处于同一级别
<style>
.container {
height: 100%;
padding: 0 200px;
}
.left,
.right {
width: 200px;
min-height: 200px;
background-color: rgb(127, 224, 192);
}
.center {
width: 100%;
min-height: 200px;
background-color: rgb(177, 135, 143);
}
.left,
.center,
.right {
float: left;
}
.left {
margin-left: -100%;
position: relative;
/*相对于正常定位时的位置*/
left: -200px;
}
.right {
margin-right: -200px;
}
</style>
</head>
<body>
<div class="container">
<div class="center"></div>
<div class="left"></div>
<div class="right"></div>
</div>
</body>
结果如图所示
二、中间元素自称一级,左右两边为一级
<style>
.left,
.container,
.right {
float: left;
}
.container {
width: 100%;
}
.container .center {
margin: 0 200px;
min-height: 200px;
background-color: rgb(177, 135, 143);
}
.left,
.right {
width: 200px;
min-height: 200px;
background-color: rgb(127, 224, 192);
}
.left {
margin-left: -100%;
}
.right {
margin-left: -200px;
}
</style>
</head>
<body>
<div class="container">
<div class="center"></div>
</div>
<div class="left"></div>
<div class="right"></div>
</body>
结果如图
三、利用CSS计算实现(不支持,对渲染性能有影响)
<style>
.left,
.center,
.right {
float: left;
}
.left,
.right {
width: 200px;
height: 200px;
background-color: rgb(133, 228, 228);
}
.center {
width: calc(100% - 400px);
min-height: 200px;
background-color: rgb(177, 135, 143);
}
</style>
</head>
<body>
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
四、使用flex布局实现
<style>
.container {
display: flex;
justify-content: space-between;
height: 100%;
}
.left,
.right {
flex: 0 0 200px;
height: 200px;
background-color: rgb(113, 156, 156);
}
.center {
flex: 1;
min-height: 200px;
background-color: rgb(199, 168, 199);
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
</body>
效果如图所示
五、利用子绝父相的定位来实现
<style>
.container {
position: relative;
height: 100%;
}
.left,
.right {
position: absolute;
top: 0;
width: 200px;
min-height: 200px;
background-color: rgb(113, 156, 156);
}
.left {
left: 0;
}
.right {
right: 0;
}
.center {
margin: 0 200px;
min-height: 400px;
background-color: rgb(199, 168, 199);
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
</body>
效果如图