阅读背景:

五大布局总结

来源:互联网 
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、高度已知,做三栏布局,左右宽300px,中间自适应 <!DOCTYPE html>



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: