在工作中, 经常需要使用到中间两栏布局或者左右布局, 对最常见的五种布局方式(float, absolute, flex, table, 栅格布局)等研究以后, 发现float和absolute是比较常用和有效的布局方式。
-
float布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>layout</title> <style> html * { padding: 0; margin: 0; } .layout article div { min-height: 100px; } </style> </head> <body> <section class="layout float"> <article class="left-right-center"> <style media="screen"> .layout.float .left { float: left; width: 300px; background: red; } .layout.float .right{ float: right; width: 300px; background: blue; } .layout.float .center{ background: yellow; } </style> <div class="left"></div> <div class="right"></div> <div class="center"> <h1> 浮动解决方案 </h1> 1. this is the center 1. this is the center 1. this is the center </div>center </article> </section> </body> </html>
缺点: 中间高度如果溢出, 会从左侧开始,造成布局问题。 解决方案,需要创建一个BFC, 加上
overflow: hidden
-
flex布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .flex .left-right-center{ display: flex; direction: row; } .flex .left-right-center>div{ min-height: 100px; } .flex .left-right-center .left{ background: red; width: 300px; } .flex .left-right-center .right{ background: yellow; width: 300px; } .flex .left-right-center .center{ flex: 1; background: purple; } </style> </head> <body> <section class="flex"> <article class="left-right-center"> <div class="left"></div> <div class="right"></div> <div class="center"> 1111111 </div> </article> </section> </body> </html>
网友评论