1、高度已知,做三栏布局,左右宽300px,中间自适应
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Layout</title>
<style media="screen">
html *{
padding: 0;
margin: 0;
}
.layout article div{
min-height: 100px;
}
</style>
</head>
<body>
<!--浮动布局 -->
<!-- 优点:兼容性好
缺点:脱离文档流,处理不好与周边的关系,就会有问题。
当页面宽度太小,浮动会挤到下一行
由于DOM结构限制左-右-中,主要内容无法最先加载
高度中间高度超出且没有margin的情况下会出问题(文字环绕):解决方案为清除浮动或创建BFC(实质也是清除浮动)
-->
<section class="layout float">
<style media="screen">
.layout.float .left{
float:left;
width:300px;
background: red;
}
.layout.float .center{
background: yellow;
}
.layout.float .right{
float:right;
width:300px;
background: blue;
}
</style>
<h1>三栏布局</h1>
<article class="left-right-center">
<div class="left"></div>
<div class="right"></div>
<div class="center">
<h2>浮动解决方案</h2>
</div>
</article>
</section>
<!-- 绝对布局
优点:很方便快捷,主要内容可以优先加载。
缺点:由于容器脱离了文档流,导致子元素必须脱离文档流
高度未知的情况下会出问题
-->
<section class="layout absolute">
<style>
.layout.absolute .left-center-right>div{
position: absolute;
}
.layout.absolute .left{
left:0;
width: 300px;
background: red;
}
.layout.absolute .center{
left: 300px;
right: 300px;
background: yellow;
}
.layout.absolute .right{
right:0;
width: 300px;
background: blue;
}
</style>
<h1>三栏布局</h1>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>绝对定位解决方案</h2>
</div>
<div class="right"></div>
</article>
</section>
<!-- flexbox布局
优点:比较完美,解决浮动和定位缺点
缺点:低版本浏览器不太兼容-->
<section class="layout flexbox">
<style>
.layout.flexbox{
margin-top: 110px;
}
.layout.flexbox .left-center-right{
display: flex;
}
.layout.flexbox .left{
width: 300px;
background: red;
}
.layout.flexbox .center{
flex:1;
background: yellow;
}
.layout.flexbox .right{
width: 300px;
background: blue;
}
</style>
<h1>三栏布局</h1>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>flexbox解决方案</h2>
</div>
<div class="right"></div>
</article>
</section>
<!-- 表格布局
优点:兼容性很好
缺点:无法设置栏边距,左中右模块间无间隔
对SEO不够友好
当一个格高度增加,其余的格也会被动增加高度
-->
<section class="layout table">
<style>
.layout.table .left-center-right{
width:100%;
height: 100px;
display: table;
}
.layout.table .left-center-right>div{
display: table-cell;
}
.layout.table .left{
width: 300px;
background: red;
}
.layout.table .center{
background: yellow;
}
.layout.table .right{
width: 300px;
/*height:400px;*/
background: blue;
}
</style>
<h1>三栏布局</h1>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>表格布局解决方案</h2>
</div>
<div class="right"></div>
</article>
</section>
<!-- 网格布局
缺点:兼容性不强。高度未知的情况下会出问题。。
优点:非常简单布局-->
<section class="layout grid">
<style>
.layout.grid .left-center-right{
width:100%;
display: grid;
grid-template-rows: 100px;
grid-template-columns: 300px auto 300px;
}
.layout.grid .left-center-right>div{
}
.layout.grid .left{
width: 300px;
background: red;
}
.layout.grid .center{
background: yellow;
}
.layout.grid .right{
background: blue;
}
</style>
<h1>三栏布局</h1>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>网格布局解决方案</h2>
</div>
<div class="right"></div>
</article>
</section>
</body>
</html>
问题1:当中间内容高度超出本身的高度,纯float布局会出现文字环绕,absolute布局中间会多出一块,grid布局的文字或超出。
问题2:当没有固定高度的时候,只有flex和table布局可用。flex和table布局均可根据内容撑开盒子,grid布局在没有固定高度,会自动有个高度,但是当文字过多,也遮不住文字,文字会溢出。absolute布局,没有高度时,在没有文字的部分不显示。
拓展:上下布局,上方固定,下方自适应。关键点是
html,body{height:100%}
//html,绝对定位
<div class="outer">
<div class="A"></div>
<div class="B">
</div>
</div>
//css
<style>
html,
body {
height: 100%;
padding: 0;
margin: 0;
}
.outer {
height: 100%;
position: relative;
}
.A {
height: 100px;
background: #BBE8F2;
}
.B {
background: #D9C666;
width: 100%;
position: absolute;
top: 100px;
left: 0;
bottom: 0;
}
</style>
//html flex布局
<section class="flex">
<div class="top"></div>
<div class="bot"></div>
</section>
//css
<style>
html,
body {
height: 100%;
padding: 0;
margin: 0;
}
.flex{
display: flex;
flex-direction: column;
height: 100%;
}
.top{
background: #D9C666;
height: 200px;
}
.bot{
flex:1;
background: #BBE8F2;
}
</style>
//html grid布局
<section class="grid">
<div class="top"></div>
<div class="bot"></div>
</section>
//css
<style>
html,
body {
height: 100%;
padding: 0;
margin: 0;
}
.grid{
height: 100%;
display: grid;
grid-template-rows: 100px 100%;
grid-template-columns: 100%;
}
.top{
background: #D9C666;
}
.bot{
background: #BBE8F2;
}
</style>
两栏布局
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Layout</title>
<style media="screen">
html *{
padding: 0;
margin: 0;
}
.layout{
/* width: 1000px;
border:1px solid; */
}
.layout article div{
height: 100px;
}
</style>
</head>
<body>
<!--浮动布局 -->
<!-- 优点:兼容性好
缺点:脱离文档流,处理不好与周边的关系,就会有问题。
当页面宽度太小,浮动会挤到下一行
由于DOM结构限制左-右-中,主要内容无法最先加载
高度中间高度超出且没有margin的情况下会出问题(文字环绕):解决方案为清除浮动或创建BFC(实质也是清除浮动)
-->
<section class="layout float">
<style media="screen">
.layout.float .left{
float:left;
width:300px;
background: red;
}
.layout.float .center{
background: yellow;
}
.layout.float .right{
overflow: hidden;
background: blue;
/* border:1px solid; */
}
</style>
<h1>三栏布局</h1>
<article class="left-right-center">
<div class="left"></div>
<div class="right">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: red;
}
.layout.absolute .center{
left: 300px;
right: 300px;
background: yellow;
}
.layout.absolute .right{
right:0;
width: calc(100% - 300px);
background: blue;
}
</style>
<h1>三栏布局</h1>
<article class="left-center-right">
<div class="left"></div>
<div class="right">2</div>
</article>
</section>
<!-- flexbox布局
优点:比较完美,解决浮动和定位缺点
缺点:低版本浏览器不太兼容-->
<section class="layout flexbox">
<style>
.layout.flexbox{
margin-top: 110px;
}
.layout.flexbox .left-center-right{
display: flex;
}
.layout.flexbox .left{
width: 300px;
background: red;
}
.layout.flexbox .center{
flex:1;
background: yellow;
}
.layout.flexbox .right{
/* width: 300px; */
flex:1;
/* height:100%; */
background: blue;
}
</style>
<h1>三栏布局</h1>
<article class="left-center-right">
<div class="left"></div>
<div class="right">2</div>
</article>
</section>
<!-- 表格布局
优点:兼容性很好
缺点:无法设置栏边距,左中右模块间无间隔
对SEO不够友好
当一个格高度增加,其余的格也会被动增加高度
-->
<section class="layout table">
<style>
.layout.table .left-center-right{
width:100%;
height: 100px;
display: table;
}
.layout.table .left-center-right>div{
display: table-cell;
}
.layout.table .left{
width: 300px;
background: red;
}
.layout.table .center{
background: yellow;
}
.layout.table .right{
/* width: 300px; */
/*height:400px;*/
background: blue;
}
</style>
<h1>三栏布局</h1>
<article class="left-center-right">
<div class="left"></div>
<div class="right">2</div>
</article>
</section>
<!-- 网格布局
缺点:兼容性不强。高度未知的情况下会出问题。。
优点:非常简单布局-->
<section class="layout grid">
<style>
.layout.grid .left-center-right{
width:100%;
display: grid;
grid-template-columns: 300px 1fr;
}
.layout.grid .left-center-right>div{
}
.layout.grid .left{
width: 300px;
background: red;
}
.layout.grid .center{
background: yellow;
}
.layout.grid .right{
background: blue;
}
</style>
<h1>三栏布局</h1>
<article class="left-center-right">
<div class="left"></div>
<div class="right">2</div>
</article>
</section>
</body>
</html>