写在前面的话css
较常见的布局问题 - 三栏布局,左右两侧固定宽度,中间内容自适应html
一篇相似的布局问题-两栏布局 详情请戳css3
【转自】程序员的程程序员
<!--浮动布局 --> <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> 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: 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> 1.这是三栏布局的绝对定位解决方案; 2.这是三栏布局的绝对定位解决方案; </div> <div class="right"></div> </article> </section>
绝对定位布局优势,很快捷,设置很方便,并且也不容易出问题,你能够很快的就能想出这种布局方式。
缺点就是,绝对定位是脱离文档流的,意味着下面的全部子元素也会脱离文档流,这就致使了这种方法的有效性和可以使用性是比较差的。布局
<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> 1.这是三栏布局的felx解决方案; 2.这是三栏布局的flex解决方案; </div> <div class="right"></div> </article> </section>
flexbox布局是css3里新出的一个,它就是为了解决上述两种方式的不足出现的,是比较完美的一个。目前移动端的布局也都是用flexbox。
flexbox的缺点就是不能兼容IE8及如下浏览器。学习
<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; background: blue; } </style> <h1>三栏布局</h1> <article class="left-center-right"> <div class="left"></div> <div class="center"> <h2>表格布局解决方案</h2> 1.这是三栏布局的表格解决方案; 2.这是三栏布局的表格解决方案; </div> <div class="right"></div> </article> </section>
表格布局在历史上遭到不少人的摒弃,说表格布局麻烦,操做比较繁琐,其实这是一种误解,在不少场景中,表格布局仍是很适用的,好比这个三栏布局,用表格布局就轻易写出来了。还有表格布局的兼容性很好,在flex布局不兼容的时候,能够尝试表格布局。
表格布局也是有缺陷的,当其中一个单元格高度超出的时候,两侧的单元格也是会跟着一块儿变高的,而有时候这种效果不是咱们想要的。flex
<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> 1.这是三栏布局的网格布局解决方案; 2.这是三栏布局的网格布局解决方案; </div> <div class="right"></div> </article> </section>
网格布局也是新出的一种布局方式,若是你答出这种方式,也就证实了你的实力,证实你对技术热点是有追求的,也说明你有很强的学习能力。flexbox