一、两列布局
1.左列定宽,右列自适应
利用float+margin:
html代码:
<div class="left">左边</div>
<div class="right">右边</div>
css代码:
.left{
width: 200px;
height: 200px;
background: red;
float: left;
}
.right{
height: 200px;
background: blue;
margin-left: 200px;
}
(2)利用float+margin负边距
html代码:
<div class="left">左边</div>
<div class="parent">
<div class="right">右边</div>
</div>
css代码:
.left{
width: 200px;
height: 200px;
background: red;
float: left;
}
.right{
height: 200px;
background: blue;
margin-left: 200px;
}
.parent{
width: 100%;
float: right;
margin-left: -200px;
}
这种方法为第二个div添加了父标签,因为浮动元素脱离了文档流,不占用空间,因此设置宽度为100%,使其自动填充剩余空间。margin-left: 200px;
是为了不覆盖住左列。margin-left: -200px;
这里利用了负边距,相当于整个div快往左上移动了200px,使左右两列同行显示。
(3)利用float+overflow
html代码:
<div class="left">左边</div>
<div class="right">右边</div>
css代码:
.left{
width: 200px;
height: 200px;
background: red;
float: left;
}
.right{
height: 200px;
background: blue;
overflow: hidden;
}
这里使用overflow是利用其触发BFC,无需关注宽度,达到自适应。
(4)使用display:table
html代码:
<div class="parent">
<div class="left">左边</div>
<div class="right">右边</div>
</div>
css代码:
.parent{
width: 100%;
display: table;
height: 500px;
}
.left{
width: 200px;
background: red;
display: table-cell;
}
.right{
background: blue;
display: table-cell;
}
利用display:table-cell自动分配表格宽度
(5)flex布局
html代码:
<div class="parent">
<div class="left">左边</div>
<div class="right">右边</div>
</div>
css代码:
.parent{
display: flex;
height: 100px;
}
.left{
width: 200px;
background-color: red;
}
.right{
flex: 1;
background-color: blue;
}
(6)绝对定位
html代码:
<div class="parent">
<div class="left"></div>
<div class="right"></div>
</div>
css代码:
.parent{
position: relative;
}
.left{
width: 200px;
height: 200px;
background-color: red;
position: absolute;
top: 0;
left: 0;
}
.right{
width: 100%;
height: 200px;
background-color: blue;
position: absolute;
left: 200px;
top: 0;
right: 0;
}
这里爬了一点小坑,是在绝对定位了没有设置right:0,属性值,元素脱离了文本流。主要原因是对position:absolute和css流体特性理解得不够。
(7)grid布局
html代码:
<div class="content">
<div class="left">左边</div>
<div class="right">右边</div>
</div>
css代码:
.content{
width:100%;
display: grid;
grid-template-columns: 100px auto;
grid-template-rows: 500px;
}
.left{
background-color: red;
}
.right{
background-color: blue;
}
2.一列不定,一列自适应
(1)使用float+overflow
html代码:
<div class="left">hellohellohello</div>
<div class="right"></div>
css代码:
.left{
height: 200px;
float: left;
background-color: red;
}
.right{
height: 200px;
background-color: blue;
overflow: hidden;
}
利用overflow触发BFC
(2)flex布局
html代码:
<div class="parent">
<div class="left">hellohellohello</div>
<div class="right"></div>
</div>
css代码:
.parent{
display: flex;
}
.left{
height: 200px;
background-color: red;
}
.right{
height: 200px;
background-color: blue;
flex: 1;/*自适应剩余宽度*/
}
(3)grid布局
html代码:
<div class="content">
<div class="left">左边</div>
<div class="right">右边</div>
</div>
css代码:
.content{
width:100%;
display: grid;
grid-template-columns: auto 1fr;
grid-template-rows: 500px;
}
.left{
background-color: red;
}
.right{
background-color: blue;
}
二、三列布局
(1)float+margin
html代码:
<div class="parent">
<div class="left">左定宽</div>
<div class="center">中间定宽</div>
<div class="right">右边自适应</div>
</div>
css代码:
.left{
width: 200px;
height: 200px;
background-color: red;
float: left;
}
.center{
width: 200px;
height: 200px;
background-color: blue;
float: left;
}
.right{
height: 200px;
background-color: yellow;
margin-left: 400px;
}
(2)float+overflow
只需将margin-left: 400px;
改为overflow: hidden;
即可。触发BFC
(3)双飞翼布局
html代码:
<div id="header"></div>
<div id="parent">
<div id="center">
<div id="inner">中间自适应</div>
</div>
<div id="left">左定宽</div>
<div id="right">右定宽</div>
</div>
<div id="footer"></div>
css代码:
#header{
width: 100%;
height: 50px;
background-color: gray;
}
#left{
width: 200px;
height: 500px;
background-color: red;
float: left;
margin-left: -100%;
}
#center{
width: 100%;
background-color: blue;
height: 500px;
float: left;
}
#inner{
margin: 0 200px;
}
#right{
width: 200px;
height: 500px;
float: left;
background-color: yellow;
margin-left: -200px;
}
#footer{
clear: both;
width: 100%;
height: 50px;
background-color: gray;
}
难点:margin负边距的应用。margin-left: -100%;
这里的百分百是相对于父元素计算的,使div位于左列,margin-left: -200px;
使div块位于右列。
(4)圣杯布局
css代码:
#header{
width: 100%;
height: 50px;
background-color: gray;
}
#parent{
padding: 0 200px;
}
#left{
width: 200px;
height: 500px;
background-color: red;
float: left;
margin-left: -100%;
position: relative;
left: -200px;
}
#center{
width: 100%;
background-color: blue;
height: 500px;
float: left;
}
#right{
width: 200px;
height: 500px;
float: left;
background-color: yellow;
margin-left: -200px;
position: relative;
right: -200px;
}
#footer{
clear: both;
width: 100%;
height: 50px;
background-color: gray;
}
html代码:
<div id="header"></div>
<div id="parent">
<div id="center">
<div id="inner">中间自适应</div>
</div>
<div id="left">左定宽</div>
<div id="right">右定宽</div>
</div>
<div id="footer"></div>
圣杯与双飞翼的相同之处是都使用了浮动和margin负值。不同之处在于对中间列被覆盖区域的处理不同,双飞翼是添加子标签,使用margin属性来显示被遮住的内容;圣杯中是通过在三列外包裹一层div,并设置padding值来显示被遮住的内容,然后使用相对定位是左列位于左侧,右列位于右侧。
(5)使用flex
html代码:
<div id="header"></div>
<div id="parent">
<div id="left">左定宽</div>
<div id="center">中间自适应</div>
<div id="right">右定宽</div>
</div>
<div id="footer"></div>
css代码:
#header{
width: 100%;
height: 50px;
background-color: gray;
}
#parent{
display: flex;
}
#left{
width: 200px;
height: 200px;
background-color:red;
}
#center{
flex: 1;
height: 200px;
background-color:yellow;
}
#right{
width: 200px;
height: 200px;
background-color:blue;
}
#footer{
width: 100%;
height: 50px;
background-color: gray;
}
三、多列布局
1.多列等宽
(1)使用浮动
html代码:
<div class="content">
<div class="column"></div>
<div class="column"></div>
<div class="column"></div>
<div class="column"></div>
<div class="column"></div>
</div>
css代码:
.column{
float: left;
width: 20%;
height: 500px;
}
.column:nth-child(odd){
background-color: red;
}
.column:nth-child(even){
background-color: blue;
}
(2)使用table
css代码:
.content{
width: 100%;
height: 200px;
display: table;
}
.column{
display: table-cell;
}
.column:nth-child(odd){
background-color: red;
}
.column:nth-child(even){
background-color: blue;
}
优点:不用在意列数,根据列数自适应平分宽度
(3)使用flex
css代码:
.content{
display: flex;
height: 200px;
}
.column{
flex: 1;
}
.column:nth-child(odd){
background-color: red;
}
.column:nth-child(even){
background-color: blue;
}
优点:不用在意列数,根据列数自适应平分宽度。缺点:要考虑兼容性
2.多列等宽
1.使用padding和margin正负相抵
在所有列中使用正的上、下padding和负的上、下margin,并在所有列外面加上一个容器,并设置overflow:hiden把溢出背景切掉。当一列的高度变化时,其他列的高度也会跟着变化
html代码:
<div class="container">
<div class="left column">
<div>sss</div>
</div>
<div class="center column">
<div>sss</div>
</div>
<div class="right column">
<div>sss</div>
<div>sss</div>
<div>sss</div>
</div>
</div>
css代码:
.container{
width: 960px;
overflow: hidden;
}
.column{
float: left;
width: 200px;
padding-bottom: 99999px;
margin-bottom: -99999px;
background-color: blue;
}
.left{
background-color: red;
}
.center{
background-color: blue;
}
.right{
background-color: yellow;
}
2.使用flex布局:
html代码:
<div class="container">
<div class="left column">
<div>sss</div>
</div>
<div class="center column">
<div>sss</div>
</div>
<div class="right column">
<div>sss</div>
<div>sss</div>
<div>sss</div>
</div>
</div>
css代码:
.container{
display: flex;
flex-direction: row;
}
.column{
width: 200px;
}
.left{
background-color: red;
}
.center{
background-color: blue;
}
.right{
background-color: yellow;
}
3.table布局:
html代码:
<div class="container">
<div class="left column">
<div>sss</div>
</div>
<div class="center column">
<div>sss</div>
</div>
<div class="right column">
<div>sss</div>
<div>sss</div>
<div>sss</div>
</div>
</div>
css代码:
.container{
display: table;
}
.column{
width: 200px;
display: table-cell;
}
.left{
background-color: red;
}
.center{
background-color: blue;
}
.right{
background-color: yellow;
}
四、两列全屏布局,高度跟浏览器高度一致
html代码:
<div class="left"></div>
<div class="right"></div>
css代码:
html, body{
height: 100%;
}
body{
display:flex;
flex-flow: row nowrap;
}
.left{
flex: 0 0 300px;
background: red;
opacity: .5;
}
.right{
flex: 1 1 100%;
background: black;
}
注意设置html的高度为100%,使html的高度起作用