五种解决办法
一、 通过浮动解决
二、 通过绝对定位解决
三、 通过Flexbox解决
四、 通过表格解决
五、 通过网格布局解决
下面贴代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{ margin: 0; padding: 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: red; } .layout.float .center{ background: yellow; } .layout.float .right{ float: right; width: 300px; background: blue; } </style> <article class="left-right-center"> <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: red; } .layout.absolute .center{ left: 300px; right: 300px; background: yellow; } .layout.absolute .right{ right: 0; width: 300px; background: blue; } </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: red; } .layout.flexbox .center{ flex: 1; background: yellow; } .layout.flexbox .right{ width: 300px; background: blue; } </style> <article class="left-center-right"> <div class="left"></div> <div class="center"> <h1>flexbox解决方案</h1> 1. 这是三栏布局中间部分 2. 这是三栏布局中间部分 </div> <div class="right"></div> </article> </section> <!-- 表格布局解决方案 --> <section class="layout table"> <style media="screen"> .layout.table .left-center-right{ width: 100%; display: table; height: 100px; } .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> <article class="left-center-right"> <div class="left"></div> <div class="center"> <h1>表格布局解决方案</h1> 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: red; } .layout.grid .center{ background: yellow; } .layout.grid .right{ background: blue; } </style> <article class="left-center-right"> <div class="left"></div> <div class="center"> <h1>网格布局解决方案</h1> 1. 这是三栏布局中间部分 2. 这是三栏布局中间部分 </div> <div class="right"></div> </article> </section> </body> </html>
问题拓展
一、五种方案各自优缺点
1.缺点 清除浮动 脱离文档流 优点 兼容性好
2.缺点 脱离文档流可使用性较差 优点 快捷
3.比较完美
4.缺点 单元格高度问题 优点 兼容性好
5.新技术
二、高度去掉 五种方案哪个还可以适配
第三种和第四种
三、兼容性 业务中最优使用是哪个
第三种