“固比固”布局也叫“圣杯”布局,它们实现的都是三栏布局,两边的盒子宽度固定,中间盒子自适应。本章用了5种方式来实现。
浮动布局,绝对定位,flex-box布局,table布局,grid布局
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
*{
padding: 0;
margin: 0;
}
.layout{
margin-top: 20px;
}
.layout article div{
min-height: 100px;
}
</style>
</head>
<body>
<section class="layout float">
<style media='screen'>
.layout.float .left{
float: left;
width: 300px;
background-color: red;
}
.layout.float .right{
float: right;
width: 300px;
background-color: blue;
}
.layout.float .center{
background-color: yellow;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="right"></div>
<div class="center">
<h1>浮动解决方案</h1>
1.这是三栏布局
2.这是三栏布局中间部分
</div>
</article>
</section>
<section class="layout absolute">
<style>
.layout.absolute .left-center-right>div{
position: absolute;
}
.layout.absolute .left{
left: 0;
width: 300px;
background-color: red;
}
.layout.absolute .center{
left: 300px;
right: 300px;
background-color: yellow;
}
.layout.absolute .right{
right: 0;
width: 300px;
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>绝对定位解决方案</h1>
1.这是三栏布局绝对定位中间部分
2.这是三栏布局绝对定位中间部分
</div>
<div class="right"></div>
</article>
</section>
<section class="layout flexbox">
<style>
.layout.flexbox{
margin-top: 140px;
}
.layout.flexbox .left-center-right{
display: flex;
}
.layout.flexbox .left{
width: 300px;
background-color: red;
}
.layout.flexbox .center{
flex: 1;
background-color: yellow;
}
.layout.flexbox .right{
width: 300px;
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>flecbox解决方案</h1>
1.这是三栏布局flecbox间部分
2.这是三栏布局flecbox中间部分
</div>
<div class="right"></div>
</article>
</section>
<section class="layout table">
<style>
.layout.table .left-center-right{
width: 100%;
display: table;
height: 100px;
}
.layout.table .left-center-right>div{
display: table-cell;
}
.layout.table .left{
width: 300px;
background-color: red;
}
.layout.table .center{
background-color: yellow;
}
.layout.table .right{
width: 300px;
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>table解决方案</h1>
1.这是三栏布局table间部分
2.这是三栏布局table中间部分
</div>
<div class="right"></div>
</article>
</section>
<section class="layout grid">
<style>
.layout.grid .left-center-right{
display: grid;
width: 100%;
grid-template-rows: 100px;
grid-template-columns: 300px auto 300px;
}
.layout.grid .left{
background-color: red;
}
.layout.grid .center{
background-color: yellow;
}
.layout.grid .right{
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>grid解决方案</h1>
1.这是三栏布局grid间部分
2.这是三栏布局grid中间部分
</div>
<div class="right"></div>
</article>
</section>
</body>
</html>