本文将介绍以下几种常见的布局:css
其中实现三栏布局有多种方式,本文着重介绍圣杯布局和双飞翼布局。另外几种能够猛戳实现三栏布局的几种方法html
常见的单列布局有两种:git
对于第一种,先经过对header,content,footer统一设置width:1000px;或者max-width:1000px(这二者的区别是当屏幕小于1000px时,前者会出现滚动条,后者则不会,显示出实际宽度);而后设置margin:auto实现居中便可获得。github
<div class="header"></div> <div class="content"></div> <div class="footer"></div>
.header{ margin:0 auto; max-width: 960px; height:100px; background-color: blue; } .content{ margin: 0 auto; max-width: 960px; height: 400px; background-color: aquamarine; } .footer{ margin: 0 auto; max-width: 960px; height: 100px; background-color: aqua; }
对于第二种,header、footer的内容宽度不设置,块级元素充满整个屏幕,但header、content和footer的内容区设置同一个width,并经过margin:auto实现居中。浏览器
<div class="header"> <div class="nav"></div> </div> <div class="content"></div> <div class="footer"></div>
.header{ margin:0 auto; max-width: 960px; height:100px; background-color: blue; } .nav{ margin: 0 auto; max-width: 800px; background-color: darkgray; height: 50px; } .content{ margin: 0 auto; max-width: 800px; height: 400px; background-color: aquamarine; } .footer{ margin: 0 auto; max-width: 960px; height: 100px; background-color: aqua; }
两列自适应布局是指一列由内容撑开,另外一列撑满剩余宽度的布局方式app
若是是普通的两列布局,浮动+普通元素的margin即可以实现,但若是是自适应的两列布局,利用float+overflow:hidden即可以实现,这种办法主要经过overflow触发BFC,而BFC不会重叠浮动元素。因为设置overflow:hidden并不会触发IE6-浏览器的haslayout属性,因此须要设置zoom:1来兼容IE6-浏览器。具体代码以下:dom
<div class="parent" style="background-color: lightgrey;"> <div class="left" style="background-color: lightblue;"> <p>left</p> </div> <div class="right" style="background-color: lightgreen;"> <p>right</p> <p>right</p> </div> </div>
.parent { overflow: hidden; zoom: 1; } .left { float: left; margin-right: 20px; } .right { overflow: hidden; zoom: 1; }
注意点:若是侧边栏在右边时,注意渲染顺序。即在HTML中,先写侧边栏后写主内容ide
Flex布局,也叫弹性盒子布局,区区简单几行代码就能够实现各类页面的的布局。布局
//html部分同上 .parent { display:flex; } .right { margin-left:20px; flex:1; }
Grid布局,是一个基于网格的二维布局系统,目的是用来优化用户界面设计。flex
//html部分同上 .parent { display:grid; grid-template-columns:auto 1fr; grid-gap:20px }
特征:中间列自适应宽度,旁边两侧固定宽度
比较特殊的三栏布局,一样也是两边固定宽度,中间自适应,惟一区别是dom结构必须是先写中间列部分,这样实现中间列能够优先加载。
.container { padding-left: 220px;//为左右栏腾出空间 padding-right: 220px; } .left { float: left; width: 200px; height: 400px; background: red; margin-left: -100%; position: relative; left: -220px; } .center { float: left; width: 100%; height: 500px; background: yellow; } .right { float: left; width: 200px; height: 400px; background: blue; margin-left: -200px; position: relative; right: -220px; }
<article class="container"> <div class="center"> <h2>圣杯布局</h2> </div> <div class="left"></div> <div class="right"></div> </article>
一样也是三栏布局,在圣杯布局基础上进一步优化,解决了圣杯布局错乱问题,实现了内容与布局的分离。并且任何一栏均可以是最高栏,不会出问题。
.container { min-width: 600px;//确保中间内容能够显示出来,两倍left宽+right宽 } .left { float: left; width: 200px; height: 400px; background: red; margin-left: -100%; } .center { float: left; width: 100%; height: 500px; background: yellow; } .center .inner { margin: 0 200px; //新增部分 } .right { float: left; width: 200px; height: 400px; background: blue; margin-left: -200px; }
<article class="container"> <div class="center"> <div class="inner">双飞翼布局</div> </div> <div class="left"></div> <div class="right"></div> </article>
多加一层 dom 树节点,增长渲染树生成的计算量。
等高布局是指子元素在父元素中高度相等的布局方式。接下来咱们介绍常见几种实现方式:
咱们经过等布局即可解决圣杯布局的第二点缺点,由于背景是在 padding 区域显示的,设置一个大数值的 padding-bottom,再设置相同数值的负的 margin-bottom,并在全部列外面加上一个容器,并设置 overflow:hidden 把溢出背景切掉。这种可能实现多列等高布局,而且也能实现列与列之间分隔线效果,结构简单,兼容全部浏览器。新增代码以下:
.center, .left, .right { padding-bottom: 10000px; margin-bottom: -10000px; } .container { padding-left: 220px; padding-right: 220px; overflow: hidden;//把溢出背景切掉 }
这种方法是咱们实现等高列最先使用的一种方法,就是使用背景图片,在列的父元素上使用这个背景图进行Y轴的铺放,从而实现一种等高列的假象。实现方法简单,兼容性强,不须要太多的css样式就能够轻松实现,但此方法不适合流体布局等高列的布局。
在制做样式以前须要一张相似下面的背景图:
<div class=”container clearfix”> <div class=”left”></div> <div class=”content”></div> <div class=”right”></div> </div>
.container { background: url("column.png") repeat-y; width: 960px; margin: 0 auto; } .left { float: left; width: 220px; } .content { float: left; width: 480px; } .right { float: left; width: 220px; }
这是一种很是简单,易于实现的方法。不过兼容性很差,在ie6-7没法正常运行。
<div class="container table"> <div class="containerInner tableRow"> <div class="column tableCell cell1"> <div class="left aside"> .... </div> </div> <div class="column tableCell cell2"> <div class="content section"> ... </div> </div> <div class="column tableCell cell3"> <div class="right aside"> ... </div> </div> </div> </div>
.table { width: auto; min-width: 1000px; margin: 0 auto; padding: 0; display: table; } .tableRow { display: table-row; } .tableCell { display: table-cell; width: 33%; } .cell1 { background: #f00; height: 800px; } .cell2 { background: #0f0; } .cell3 { background: #00f; }
这种方法是使用边框和绝对定位来实现一个假的高度相等列的效果。结构简单,兼容各浏览器,容易掌握。假设你须要实现一个两列等高布局,侧栏高度要和主内容高度相等。
#wrapper { width: 960px; margin: 0 auto; } #mainContent { border-right: 220px solid #dfdfdf; position: absolute; width: 740px; height: 800px; background: green; } #sidebar { background: #dfdfdf; margin-left: 740px; position: absolute; height: 800px; width: 220px; }
<div id="wrapper"> <div id="mainContent">...</div> <div id="sidebar">...</div> </div>
<main>
,当<main>
的高康足够长的时候,紧跟在<main>
后面的元素<footer>
会跟在<main>
元素的后面。<main>
元素比较短的时候(好比小于屏幕的高度),咱们指望这个<footer>
元素可以“粘连”在屏幕的底部具体代码以下:
<div id="wrap"> <div class="main"> main <br /> main <br /> main <br /> </div> </div> <div id="footer">footer</div>
* { margin: 0; padding: 0; } html, body { height: 100%;//高度一层层继承下来 } #wrap { min-height: 100%; background: pink; text-align: center; overflow: hidden; } #wrap .main { padding-bottom: 50px; } #footer { height: 50px; line-height: 50px; background: deeppink; text-align: center; margin-top: -50px; }
于2019.1.2从新修改,若是以为文章对你有些许帮助,欢迎在个人GitHub博客点赞和关注,感激涕零!