假设高度已知,请写出三栏布局,其中左栏、右栏各为300px,中间自适应的方法:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>三栏布局</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style media="screen">
html * {
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 media="screen">
.layout.absolute .left-center-right>div{
position: absolute;
}
.layout.absolute .left{
left: 0;
width: 300px;
background-color: red;
}
.layout.absolute .right{
right: 0;
width: 300px;
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>
<!-- flexbox解决方案 -->
<section class="layout flexbox">
<style media="screen">
.layout.flexbox{
margin-top: 140px;
}
.layout.flexbox .left-center-right{
display: flex;
}
.layout.flexbox .left{
width: 300px;
background-color: red;
}
.layout.flexbox .right{
width: 300px;
background-color: blue;
}
.layout.flexbox .center{
flex: 1;
background-color: yellow;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>flexbox解决方案</h2>
1.这是三栏布局flex中间部分 2.这是三栏布局flex中间部分
</div>
<div class="right"></div>
</article>
</section>
<!-- 表格布局解决方案 -->
<section class="layout table">
<style media="screen">
.layout.table .left-center-right{
display: table;
height: 100px;
width: 100%;
}
.layout.table .left-center-right>div{
display: table-cell;
}
.layout.table .left{
background-color: red;
width: 300px;
}
.layout.table .right{
background-color: blue;
width: 300px;
}
.layout.table .center{
background-color: yellow;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>表格布局</h2>
1.这是三栏布局表格布局的中间部分 2.这是三栏布局表格布局的中间部分
</div>
<div class="right"></div>
</article>
</section>
<!-- 网格布局 -->
<section class="layout grid">
<style media="screen">
.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">
<h2>网格布局解决方案</h2>
1.这是三栏布局网格布局的中间部分 2.这是三栏布局网格布局的中间部分
</div>
<div class="right"></div>
</article>
</section>
</body>
</html>
1、以上各种布局的优缺点,以及比较推荐哪一种方案
(1)浮动 :
- 缺点 :浮动是脱离文档流的,有些时候需要清除浮动,需要很好的处理浮动周边元素的关系
- 优点 :兼容性比较好
(2)绝对定位 :
- 缺点 :该布局脱离文档流,所以子元素也必须脱离文档流,因此可使用性比较差
- 优点 :快捷,比较不容易出问题
(3)flex :
- 缺点 :兼容性比较差(css3的属性),不兼容IE8及以下
- 优点 :非常有效的解决了浮动和绝对定位的问题
(4)表格布局 :
- 缺点 :操作繁琐,当三栏中其中某一栏高度超出时,其他两栏的高度也会自动跟着调整(不符合某些场景)
- 优点 :兼容性非常好,补缺了flex布局兼容的问题
(5)网格布局 :
- 新技术
2、如果去掉"高度已知", 以上哪种方案同样适用?
只有 flex布局 和 表格布局 同样适用