实现方式:
- float
- 绝对定位
- flexbox布局
- table
- grid网格布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>三栏自适应布局</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.layout .left-center-right>div {
height: 100px;
}
.layout {
margin-top: 20px;
}
</style>
</head>
<body>
<!-- 浮动布局 -->
<section class="layout float">
<style type="text/css">
.float .left {
float: left;
width: 300px;
background: red;
}
.float .right {
float: right;
width: 300px;
background: blue;
}
.float .center {
background: yellow;
}
</style>
<article class="left-center-right">
<div class="left">left</div>
<div class="right">right</div>
<div class="center">
<h3>浮动布局</h3>
center
</div>
</article>
</section>
<!-- 绝对定位布局 -->
<section class="layout absolute">
<style type="text/css">
.absolute .left-center-right>div {
position: absolute;
}
.absolute .left {
left: 0;
width: 300px;
background: red;
}
.absolute .center {
left: 300px;
right: 300px;
background: yellow;
}
.absolute .right {
right: 0;
width: 300px;
background: blue;
}
</style>
<arcticle class="left-center-right">
<div class="left">left</div>
<div class="center">
<h3>绝对定位布局</h3>
center
</div>
<div class="right">right</div>
</arcticle>
</section>
<!-- flexbox布局 -->
<section class="layout flexbox">
<style type="text/css">
.flexbox {
margin-top: 140px;
}
.flexbox .left-center-right {
display: flex;
}
.flexbox .left {
width: 300px;
background: red;
}
.flexbox .center {
flex: 1;
background: yellow;
}
.flexbox .right {
width: 300px;
background: blue;
}
</style>
<arcticle class="left-center-right">
<div class="left">left</div>
<div class="center">
<h3>flexbox布局</h3>
center
</div>
<div class="right">right</div>
</arcticle>
</section>
<!-- table布局 -->
<section class="layout table">
<style type="text/css">
.table .left-center-right {
display: table;
width: 100%;
}
.table .left-center-right>div {
display: table-cell;
}
.table .left {
width: 300px;
background: red;
}
.table .center {
background: yellow;
}
.table .right {
width: 300px;
background: blue;
}
</style>
<article class="left-center-right">
<div class="left">left</div>
<div class="center">
<h3>表格布局</h3>
center
</div>
<div class="right">right</div>
</article>
</section>
<!-- grid布局 -->
<section class="layout grid">
<style type="text/css">
.grid .left-center-right {
display: grid;
grid-template-rows: 100px;
grid-template-columns: 300px auto 300px;
}
.grid .left {
background: red;
}
.grid .center {
background: yellow;
}
.grid .right {
background: blue;
}
</style>
<article class="left-center-right">
<div class="left">left</div>
<div class="center">
<h3>grid布局</h3>
center
</div>
<div class="right">right</div>
</article>
</section>
</body>
</html>