1、单列布局
主要使用max-width属性样式实现,使用max-width是为了当屏幕小于2000px时不出现滚动条
<div class="header"></div>
<div class="content"></div>
<div class="footer"></div>
<style>
.header,.footer,.content{
margin: 10px auto;
max-width: 2000px;
height: 100px;
background-color: blue;
}
.content{
height: 400px;
background-color: aquamarine;
}
.footer{
background-color: aqua;
}
</style>
2、两列自适应布局
两列自适应布局是指一列由内容撑开,另一列撑满剩余宽度的布局方式,为了触发IE浏览器的haslayout属性,需设置zoom:1
2.1、使用float+overflow:hidden 实现(注意渲染顺序,如果侧边栏在右边,应在html标签中先写侧边栏,在写主内容)
<div class="parent" style='background-color: antiquewhite'>
<div class="left" style='background-color: red;'>
<p>left</p>
</div>
<div class="right" style='background-color: yellow;'>
<p>right</p>
<p>right</p>
</div>
</div>
<style>
.parent{
overflow: hidden;
zoom: 1;
}
.left{
float: left;
margin-right: 20px;
}
.right{
overflow: hidden;
zoom: 1;
}
</style>
2.2、flex布局实现(不兼容IE9及IE9-)
<style>
.parent{
display: flex;
}
.right{
margin-left: 20px;
flex: 1;
}
</style>
2.3、grid栅格布局
<style>
.parent{
display: grid;
grid-template-columns: auto 2fr;
grid-gap: 20px
}
</style>
3、三栏布局
中间自适应宽度,旁边固定宽度
3.1、圣杯布局(注意,DOM结构必须是先写中间列,这样实现中间列可以优先加载)
<div class="container">
<div class="center">
<h2>圣杯布局</h2>
</div>
<div class="left">left</div>
<div class="right">right</div>
</div>
<style>
.left{
float: left;
width: 200px;
height: 400px;
background-color: 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-color: blue;
margin-left: -200px;
position: relative;
right: -220px
}
.center,.left,.right{
padding-bottom: 100000px;
margin-bottom: -100000px;
}
.container{
padding-left: 220px; /*给左右侧边栏移上去留下位置*/
padding-right: 220px;
overflow: hidden; /*溢出背景隐藏*/
}
</style>
三个部分都设定为左浮动,否则左右两边内容上不去,就不可能与中间列同一行,设置center的宽度为100%(实现中间列内容自适应),会导致left与right挤到下一行,设置margin-left为负值让left和right部分回到与center部分同一行,设置父容器的padding-left和padding-right,让左右两边留出间隙,设置相对定位,让left和right部分移动到两边,设置一个大数值的padding-bottom,再设置相同数值的负的margin-bottom,以及设置overflow:hidden把溢出背景切掉,实现等高布局。
3.2、双飞翼布局
解决了圣杯布局错乱问题,实现了内容与布局的分离
<article class="container">
<div class="center">
<div class="inner">双飞翼布局</div>
</div>
<div class="left">left</div>
<div class="right">right</div>
</article>
<style>
.container{
min-width: 600px;
}
.left{
float: left; /*关键代码 1*/
width: 200px;
height: 400px;
background-color: red;
margin-left: -100%; /*关键代码 2*/
}
.center{
float: left;
width: 100%;
height: 500px;
background-color: yellow;
}
.center .inner{
margin: 0 200px;
}
.right{
float: left;
width: 200px;
height: 400px;
background-color: blue;
margin-left: -200px; /*关键代码 3*/
}
.center,.left,.right{ /*关键代码 4*/
padding-bottom:10000px;
margin-bottom: -10000px;
}
.container{
overflow: hidden;
}
</style>
三个部分都设定为左浮动,然后设置center的宽度为100%,此时,left和right部分会跳到下一行;设置margin-left为负值让left和right部分回到与center部分同一行,center部分增加一个内层div,并设margin: 0 200px;设置一个大数值的padding-bottom,再设置相同数值的负的margin-bottom,以及设置overflow:hidden把溢出背景切掉,实现等高布局。
4、粘连布局
有一块内容<main>,当<main>的高度足够长的时候,紧跟在<main>后面的元素<footer>会跟在<main>元素的后面。当<main>元素比较短的时候(比如小于屏幕的高度),这个<footer>元素能够“粘连”在屏幕的底部
<div id="wrap">
<div class="main">
粘连布局<br>
粘连布局<br>
</div>
</div>
<div id="footer">footer</div>
<style>
*{
margin: 0;
padding: 0;
}
html,body{
height: 100%;
}
#wrap{
min-height: 100%; /*关键代码 1*/
background-color: red;
text-align: center;
overflow: hidden;
}
#wrap .main{
padding-bottom: 50px; /*避免高度不够时与footer重叠*/
}
#footer{
height: 50px;
line-height: 50px;
background-color: deeppink;
text-align: center;
margin-top: -50px; /*关键代码 2*/
}
</style>
footer要使用margin为负来确定自己的位置,在main区域需要设置 padding-bottom。这也是为了防止负 margin 导致 footer 覆盖任何实际内容。