一、BFC的理解和应用
1.BFC的概念?
Block Format Context 块级格式化上下⽂,⼀块独⽴的渲染区域,内部元素的渲染不会影响边界以外的元素。
形成BFC的常⻅条件:
- float的值不等于 none;
- position值是 absolute或 fixed;
- overflow不是 visible,( overflow:hidden);
- display的值为 inline-block、table-cell;
- body根元素
如何应用?
- (BFC可以包含浮动元素)清除浮动;
- 同⼀个BFC下,垂直外边距会发⽣重叠;
二、margin外边距重叠及解决⽅法
块的上外边距(margin-top)和下外边距(margin-bottom)有时合并(折叠)为单个边距,其⼤⼩为单个边距的最⼤值 (或如果它们相等,则仅为其中⼀个),这种⾏为称为边距重叠。
1.哪些元素会发⽣外边距重叠问题
(1)情况一:相邻兄弟元素的marin-bottom和margin-top的值发⽣重叠
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
div.bfcCls {
width: 300px;
height: 300px;
border: 1px solid black;
}
div.bfcCls div.first {
width: 100px;
height: 100px;
background-color:aqua;
margin-bottom: 50px;
}
div.bfcCls div.second {
width: 100px;
height: 100px;
background-color:orchid;
margin-top: 80px;
float: left;
}
</style>
</head>
<body>
<div class="bfcCls">
<div class="first">
</div>
<div class="second">
</div>
</div>
</body>
<script>
</script>
</html>
(2) 情况二:⽗级和第⼀个/最后⼀个⼦元素的margin合并
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
div.bfcCls {
width: 300px;
height: 300px;
background-color:blue;
margin-top: 50px;
padding: 1px;
box-sizing: border-box;
}
div.bfcCls div.first {
width: 100px;
height: 100px;
background-color:darkgreen;
margin-top: 50px;
}
</style>
</head>
<body>
<div class="bfcCls">
<div class="first">
</div>
</div>
</body>
<script>
</script>
</html>
(3)情况三:空的块级元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
div.bfcCls {
width: 500px;
height: 500px;
background-color:coral;
}
div.bfcCls div.first {
width: 100px;
height: 100px;
background-color:hotpink;
margin-bottom: 50px;
}
div.bfcCls div.second {
margin-top: 20px;
margin-bottom: 20px;
border:1px solid turquoise;
/* padding: 1px; */
box-sizing: border-box;
}
div.bfcCls div.third {
width: 100px;
height: 100px;
background-color: yellowgreen;
margin-top: 50px;
}
</style>
</head>
<body>
<div class="bfcCls">
<div class="first">
</div>
<div class="second">
</div>
<div class="third">
</div>
</div>
</body>
<script>
</script>
</html>
(4)情况四:⾼度为auto的⽗元素的margin-bottom和⼦元素的margin-bottom发⽣重叠
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
div.bfcCls {
width: 500px;
background-color:aqua;
margin-bottom: 100px;
/* height: 200px; */
border-bottom: 1px solid red;
/* padding-bottom: 1px; */
}
div.bfcCls div.first {
width: 100px;
height: 100px;
background-color: green;
margin-bottom: 50px;
}
</style>
</head>
<body>
<div class="bfcCls">
<div class="first">
</div>
</div>
</body>
<script>
</script>
</html>
2.BFC可以阻⽌元素被浮动元素覆盖
float布局:就是所说的浮动,浮动之后会让元素脱离⽂档流。
需求:不能让浮动元素遮挡未浮动的元素;
解决办法:让未浮动元素的左右两侧不能出现浮动元素,因此要清除浮动。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
.boxCls {
width: 500px;
height: 500px;
background-color: red;
}
.boxCls div.first{
width: 100px;
height: 100px;
background-color: yellow;
float: left;
}
.boxCls div.second{
width: 100px;
height: 100px;
background-color: black;
float: left;
}
.boxCls div.third{
width: 100px;
height: 100px;
background-color: blue;
float: right;
}
.boxCls div.fourth {
width: 300px;
height: 200px;
background-color: green;
clear: both;
}
</style>
</head>
<body>
<div class="boxCls">
<div class="first">
</div>
<div class="second">
</div>
<div class="third">
</div>
<div class="fourth">
无敌大铁锤 Look Here!
</div>
</div>
</body>
</html>
二、 用定位进行三栏布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
}
div.boxCls {
height: 100%;
width: 100%;
position: relative;
}
div.center {
background-color: red;
height: 100%;
position: absolute;
left: 200px;
right: 200px;
}
div.left {
background-color: blue;
width: 200px;
height: 100%;
position: absolute;
left: 0;
}
div.right {
background-color: green;
width: 200px;
height: 100%;
position: absolute;
right: 0;
}
</style>
</head>
<body>
<div class="boxCls">
<div class="column left">
</div>
<div class="column center">
<h1 style="display:flex;justify-content:center;">无敌大铁锤</h1>
</div>
<div class="column right">
</div>
</div>
</body>
</html>
三、如何实现圣杯布局和双⻜翼布局?
- 三栏布局,中间⼀栏最先加载和渲染;
- 两侧内容固定,中间⾃适应;
(1)圣杯布局:使⽤float,两侧使⽤ margin的负值,以便和中间内容横向重叠,防⽌中间内容被两侧覆盖;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>圣杯布局</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
}
body {
padding-left: 200px;
padding-right: 200px;
box-sizing: border-box;
min-width: 700px;
}
div.column {
text-align: center;
height: 100%;
float: left;
}
div.center {
background-color: red;
width: 100%;
}
div.left {
background-color: blue;
width: 200px;
margin-left: -100%;
position: relative;
left: -200px;
}
div.right {
background-color: green;
width: 200px;
margin-right: -200px;
}
</style>
</head>
<body>
<div class="column center">
</div>
<div class="column left">
</div>
<div class="column right">
</div>
</body>
</html>
(2)双⻜翼布局:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>圣杯布局</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
}
div.column {
text-align: center;
height: 100%;
float: left;
}
div.mainCls {
background-color: red;
width: 100%;
}
div.mainCls .center {
background-color: aqua;
margin-left: 200px;
margin-right: 200px;
height: 100%;
}
div.left {
background-color: blue;
width: 200px;
margin-left: -100%;
}
div.right {
background-color: green;
width: 200px;
margin-left: -200px;
}
div.footer {
background-color: gainsboro;
text-align: center;
}
div.clearfix::before {
content: "";
display: block;
clear: both;
}
</style>
</head>
<body>
<div class="column mainCls">
<div class="center">
</div>
</div>
<div class="column left">
</div>
<div class="column right">
</div>
<div class='clearfix footer'>
这是页脚
</div>
</body>
</html>
四、flex布局
Flex 是 Flexible Box 的缩写,意为“弹性布局”或者“弹性盒⼦”,是 CSS3 中的⼀种新的布局模式,可以简便、响应式地实现各种⻚⾯布局,当⻚⾯需要适应不同的屏幕⼤⼩以及设备类型时⾮常适⽤。⽬前,⼏乎所有的浏览器都⽀持 Flex 布局。
采⽤ Flex 布局的元素,称为 Flex 容器(flex container),简称“容器”。它的所有⼦元素⾃动成为容器成员,称为 Flex 项⽬(flex item),简称“项⽬”。容器默认存在两根轴,分别为⽔平的主轴(main axis)和垂直的交叉轴 (cross axis)。主轴的开始位置叫做 main start,结束位置叫做 main end;交叉轴的开始位置叫做 cross start,结束位置叫做 cross end。项⽬默认沿主轴排列。单个项⽬占据的主轴空间叫做 main size,占据的交叉轴 空间叫做 cross size。
1.对齐方式
(1)flex-direction:指定弹性盒⼦中⼦元素的排列⽅式
(2) flex-wrap: 属性⽤来设置当弹性盒⼦的⼦元素(项⽬)超出⽗容器时是否换⾏
默认值:no wrap,项⽬根据容器⾃动调整,如果想⾃适应,设置该值为wrap;
(3)justify-content:设置弹性盒⼦中项⽬在主轴(横轴)⽅向上的对⻬⽅式
(4)align-items:⽤来设置弹性盒⼦中项⽬在侧轴(纵轴)⽅向上的对⻬⽅式
注: 默认值:stetch,【不能为项⽬设置⾼度】;
项目属性:
1> order:number ⽤来设置项⽬在容器中出现的顺序,您可以通过具体的数值来定义项⽬在容器中的位置;
2>align-self:允许您为某个项⽬设置不同于其它项⽬的对⻬⽅式,该属性可以覆盖 align-items 属性的值;
3>flex:是 flex-grow、flex-shrink 和 flex-basis 三个属性的简写;
- flex-grow:(必填参数)⼀个数字,⽤来设置项⽬相对于其他项⽬的增⻓量,默认值为 0;
- flex-shrink:(选填参数)⼀个数字,⽤来设置项⽬相对于其他项⽬的收缩量,默认值为 1;
- flex-basis:(选填参数)项⽬的⻓度,合法值为 auto(默认值,表示⾃动)、inherit(表示从⽗元素继承该 属性的值) 或者以具体的值加 "%"、"px"、"em" 等单位的形式;
2.定位
absolute和relative分别依据什么来定位?
(1)相对定位
相对定位:相对于本身进⾏定位,原来的位置仍然存在;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
.parent{
height: 200px;
width: 100%;
background-color: rebeccapurple;
}
div.parent div.first {
width: 100px;
height: 100px;
display: inline-block;
background-color: yellow;
/*相对定位*/
position: relative;
left: 50px
}
div.parent div.second {
width: 100px;
height: 100px;
background-color: gray;
display: inline-block;
}
</style>
</head>
<body>
<div class="parent">
<div class="first"></div>
<div class="second"></div>
</div>
</body>
</html>
(2)绝对定位
绝对定位:想对于最近已定位的⽗元素,如果没有,相对于body进⾏定位,脱离⽂档流,形成BFC;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
*{
padding: 0;
margin:0;
}
.parent{
height: 200px;
width: 100%;
background-color: rebeccapurple;
}
div.parent div.first {
width: 100px;
height: 100px;
display: inline-block;
background-color: yellow;
}
div.parent div.second {
width: 100px;
height: 100px;
background-color: gray;
display: inline-block;
/* 绝对定位 */
position: absolute;
left:10px;
top:10px;
}
因为⽗元素没有定位,因此相对于body进⾏定位
</style>
</head>
<body>
<div class="parent">
<div class="first"></div>
<div class="second"></div>
</div>
</body>
</html>
因为⽗元素没有定位,因此相对于body进⾏定位
因为⽗元素进⾏了定位,因此⼦元素的绝对定位是相对于定位⽗元素。
注:⽗元素相对定位,⼦元素绝对定位
3.居中对齐方式
(1) ⽔平居中
- inline元素:text-align:center;
- block元素:margin:auto;
- absolute元素:left:50%+margin-left:负值;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
div.boxCls {
width: 100%;
height: 80px;
border: 1px solid black;
}
div.first {
text-align: center;
}
div.second p {
width: 500px;
background-color:aqua;
margin: 0 auto;
}
div.third {
position: relative;
}
div.third p {
position: absolute;
width: 500px;
background-color:blue;
left: 50%;
margin-left: -250px;
}
</style>
</head>
<body>
<div class="boxCls first">
<span>无敌大铁锤</span>
</div>
<div class="boxCls second">
<p>无敌大铁锤</p>
</div>
<div class="boxCls third">
<p>无敌大铁锤</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
div.boxCls {
width: 100%;
height: 80px;
border: 1px solid black;
}
div.first {
text-align: center;
line-height: 80px;
}
div.second,div.third,div.fourth {
position: relative;
}
div.second div {
width: 500px;
height: 50px;
background-color:aqua;
position: absolute;
top: 50%;
left:50%;
margin-top:-25px;
margin-left:-250px;
}
div.third div{
width: 500px;
height: 50px;
background-color:chocolate;
/* 转换 */
position: absolute;
top:50%;
left: 50%;
transform: translate(-50%,-50%);
}
div.fourth div{
width: 500px;
height: 50px;
background-color:deeppink;
position: absolute;
left: 0;
top:0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
</head>
<body>
<div class="boxCls first">
<span>无敌大铁锤</span>
</div>
<div class="boxCls second">
<div>无敌大铁锤</div>
</div>
<div class="boxCls third">
<div>无敌大铁锤</div>
</div>
<div class="boxCls fourth">
<div>无敌大铁锤</div>
</div>
</body>
</html>
(2)垂直居中
- inline元素:line-height:⾏⾼;
- absolute元素: top:50%+margin-top:负值;
- absolute元素:(CSS3)transform:translate(-50%,-50%);
- absolute元素:left, top,bottom.right都为0,margin:auto;