三栏布局
三栏布局,顾名思义就是两边固定宽度,中间随浏览器宽度自适应。
比如:
直观图:
普通布局:
可以看出如果浏览器窗口发生改变时,右边的模块会遮盖中间部分
<!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>
.left{
width: 100px;
height: 200px;
background: pink;
float: left;
margin-right: 20px;
}
.right{
width: 200px;
height: 200px;
background: yellow;
float: right;
margin-left: 20px;
}
.main{
width: 900px;
height: 200px;
background: green;
}
</style>
</head>
<body>
<div class="container">
<div class="left">left</div>
<div class="right">right</div>
<div class="main">main</div>
</div>
</body>
</html>
1.流体布局
就像放在容器中的水流一样,内容区域会随着margin, padding, border的出现自动填满剩余空间,这就是块状元素的流体特性。
左右模块各自向左右浮动,设置中间模块的 margin 值使中间模块撑开。
要把中间模块放在最下边
<!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>
.left{
width: 100px;
height: 200px;
background: pink;
float: left;
}
.right{
width: 200px;
height: 200px;
background: green;
float: right;
}
.main{
height: 200px;
margin-left: 120px;
margin-right: 220px;
background: yellow;
}
</style>
</head>
<body>
<div class="container">
<div class="left">left</div>
<div class="right">right</div>
<div class="main">main</div>
</div>
</body>
</html>
缺点就是主要内容无法最先加载,当页面内容较多时会影响用户体验。
2.BFC三栏布局
块格式化上下文(Block Formatting Context,BFC) 是Web页面的可视化CSS渲染的一部分,是块盒子的布局过程发生的区域,也是浮动元素与其他元素交互的区域。
BFC 规则:BFC 区域,不会与给了浮动的元素重叠。
可以任意调动左右模块的宽度,但是不会与给了浮动的模块重叠
<!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>
.left{
width: 100px;
height: 200px;
background: pink;
float: left;
margin-right: 20px;
}
.right{
width: 200px;
height: 200px;
background: yellow;
float: right;
margin-left: 20px;
}
.main{
height: 200px;
overflow: hidden;
background: green;
}
</style>
</head>
<body>
<div class="container">
<div class="left">left</div>
<div class="right">right</div>
<div class="main">main</div>
</div>
</body>
</html>
3.双飞翼布局
主要为了解决中间模块最先加载
利用的是元素浮动+margin 负值的应用。
理解负边距的作用,left的margin-left:-100%
使它上移一行,同时right向左移占据left原先位置,同理,right的margin-left:-200px
使它上移并靠右
<!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>
.container {
float: left;
width: 100%;
}
.main {
height: 200px;
margin-left: 110px;
margin-right: 220px;
background-color: green;
}
/* 用于清除浮动 */
.main::after {
display: block;
content: '';
clear: both;
}
.left {
float: left;
height: 200px;
width: 100px;
margin-left: -100%;
background-color: pink;
}
.right {
width: 200px;
height: 200px;
float: right;
margin-left: -200px; //宽度是多少它的margin-left就给多少
background-color: yellow;
}
</style>
</head>
<body>
<div class="container">
<div class="main">main</div>
</div>
<div class="left">left</div>
<div class="right">right</div>
</body>
</html>
4.圣杯布局
主要运用相对定位+left/right负值
<!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>
.container{
padding:0 150px;
}
.main{
width: 100%;
height: 200px;
float: left;
background: green;
}
.left{
width: 100px;
height: 200px;
background: pink;
float: left;
position: relative;
left: -120px;
margin-left: -100%;
}
.right{
width: 200px;
height: 200px;
background: yellow;
float: left;
position: relative;
right: -220px;
margin-left: -200px;
}
</style>
</head>
<body>
<div class="container">
<div class="main">main</div>
<div class="left">left</div>
<div class="right">right</div>
</div>
</body>
</html>
这里需要注意:
1: 先写main,然后是left和right,因为需要先渲染main;
2: left、right需设置position:relative
以及相应的left、right负值
3:理解负边距的作用,left的margin-left:-100%
使它上移一行,同时right向左移占据left原先位置,同理,right的margin-left:-200px
使它上移并靠右
5.Flex布局
order—元素的排列顺序,下标从0开始
<!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>
.container{
display: flex;
}
.main{
flex: 3;
height: 200px;
background: green;
}
.left{
order: -1; //元素的排列顺序,下标从0开始
flex: 0 1 100px;
height: 200px;
background: pink;
margin-right: 20px;
}
.right{
flex:0 1 200px;
height: 200px;
margin-left: 20px;
background: yellow;
}
</style>
</head>
<body>
<div class="container">
<div class="left">left</div>
<div class="main">main</div>
<div class="right">right</div>
</div>
</body>
</html>
6.table布局
display:table-cell—子元素会平分table的宽度,
不能设置栏间距
<!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>
.container{
display: table;
width: 100%;
}
.left,.right,.main{
display: table-cell;
}
.left{
width: 100px;
height: 200px;
background: pink;
}
.right{
width: 200px;
height: 200px;
background: yellow;
}
.main{
background: green;
}
</style>
</head>
<body>
<div class="container">
<div class="left">left</div>
<div class="main">main</div>
<div class="right">right</div>
</div>
</body>
</html>
7.绝对定位布局
中间模块可以优先加载
给left、right绝对定位,main不用设置宽度
<!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>
.container{
position: relative;
}
.main{
height: 200px;
margin: 0 120px;
background: yellow;
}
.left{
width: 100px;
height: 200px;
position: absolute;
left: 0;
top: 0;
background: pink;
}
.right{
width: 100px;
height: 200px;
position: absolute;
right: 0;
top: 0;
background: green;
}
</style>
</head>
<body>
<div class="container">
<div class="left">left</div>
<div class="main">main</div>
<div class="right">right</div>
</div>
</body>
</html>