css三栏布局之左右宽度固定中间自适应
1.利用浮动解决三栏布局问题
<style lang="">
html *{
padding: 0;
margin:0;
}
.layout .left-center-right>div {
height: 100px;
}
section.layout {
margin-top: 20px;
}
<section class="layout float">
<style lang="">
.layout.float .left-center-right {
position: relative;
}
.layout.float .left {
width: 300px;
float: left;
background-color: red;
}
.layout.float .center {
background-color: yellow;
}
.layout.float .right {
width: 300px;
float: right;
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="right"></div>
<div class="center">
<h1>浮动解决</h1>
1.左右三栏布局,浮动解决方法
2.左右三栏布局,浮动解决方法
</div>
</article>
</section>
2.利用绝对定位解决
<style lang="">
html *{
padding: 0;
margin:0;
}
.layout .left-center-right>div {
height: 100px;
}
section.layout {
margin-top: 20px;
}
<section class="layout absolute">
<style lang="">
.layout.absolute .left-center-right {
position: relative;
}
.layout.absolute .left-center-right>div {
position: absolute;
}
.layout.absolute .left {
width: 300px;
left:0;
background-color: red;
}
.layout.absolute .right {
width: 300px;
right:0;
background-color: blue;
}
.layout.absolute .center {
left:300px;
right:300px;
background-color:yellow;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>定位解决</h1>
1.左右三栏布局,定位解决方法
2.左右三栏布局,定位解决方法
</div>
<div class="right"></div>
</article>
</section>
3.利用flexbox解决
<style lang="">
html *{
padding: 0;
margin:0;
}
.layout .left-center-right>div {
height: 100px;
}
section.layout {
margin-top: 20px;
}
</style>
<section class="layout flexbox">
<style lang="">
.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>flexbox解决</h1>
1.左右三栏布局,flexbox解决方法
2.左右三栏布局,flexbox解决方法
</div>
<div class="right"></div>
</article>
</section>
4.利用table表格布局
<style lang="">
html *{
padding: 0;
margin:0;
}
.layout .left-center-right>div {
height: 100px;
}
section.layout {
margin-top: 20px;
}
</style>
<section class="layout table">
<style lang="">
.layout.table .left-center-right {
display: table;
width: 100%;
/* 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>
5.利用grid网格布局
<style lang="">
html *{
padding: 0;
margin:0;
}
.layout .left-center-right>div {
height: 100px;
}
section.layout {
margin-top: 20px;
}
</style>
<section class="layout grid">
<style lang="">
.layout.grid .left-center-right {
display: grid;
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>