文章目录
前言
Flexbox 是 flexible box 的简称(注:意思是“灵活的盒子容器”),是 CSS3 引入的新的布局模式。它决定了元素如何在页面上排列,使它们能在不同的屏幕尺寸和设备下可预测地展现出来。 它之所以被称为 Flexbox ,是因为它能够扩展和收缩 flex 容器内的元素,以最大限度地填充可用空间。与以前布局方式(如 table 布局和浮动元素内嵌块元素)相比,Flexbox 是一个更强大的方式。它的作用有在不同方向排列元素,重新排列元素的显示顺序,更改元素的对齐方式,动态地将元素装入容器。优点:操作方便,布局简单,移动端应用广泛。

一、弹性盒子的基本概念
采用 Flex 布局的元素,称为 Flex 容器(flex container),简称"容器"。它的所有子元素自动成为容器成员,称为 Flex 项目(flex item),简称"项目"。

在 Flexbox 模型中,有三个核心概念:
– flex 项(注:也称 flex 子元素),需要布局的元素
– flex 容器,采用flex布局的块级标签(div),其包含 flex 项
– 排列方向(direction),这决定了 flex 项的布局方向,它可以横向排列也可以纵向排列。
总结:flex布局就是通过给父盒子添加flex属性,来控制子盒子的位置和排列方式。
举例如下:未加flex前,盒子a1和a2布局如下所示:
<!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>003</title>
</head>
<style>
.a {
width: 400px;
height: 200px;
}
.a1 {
width: 200px;
height: 200px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
}
.a2 {
width: 200px;
height: 200px;
background-color: yellow;
margin-top: 7px;
margin-left: 5px;
}
</style>
<body>
<div class="a">
<div class="a1">a1</div>
<div class="a2">a2</div>
</div>
</body>
</html>

当我们给盒子a设置了flex属性后,它的变化如下:
...
.a {
width: 400px;
height: 200px;
display: flex;
}
...
注意:
1、当我们为父盒子设定flex布局后,子元素的float、clear、vertical-align属性将失效,因为它此时会自动实现float后才能实现的功能,让元素在一行显示。
2、任何一个容器都可以指定为flex布局,不只是块级元素,行内元素也可以,比如:
<!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>003</title>
</head>
<style>
.a {
width: 400px;
height: 200px;
}
.a1 {
width: 200px;
height: 200px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
}
.a2 {
width: 200px;
height: 200px;
background-color: yellow;
margin-top: 7px;
margin-left: 5px;
}
</style>
<body>
<div class="a">
<span class="a1">1</span>
<span class="a2">2</span>
</div>
</body>
</html>
此时它在浏览器中呈现的效果为:
如果此时给父元素加了flex属性:
...
.a {
width: 400px;
height: 200px;
display: flex;
}
...

二、弹性盒子的容器属性
默认主轴方向:x轴方向,水平向右;
默认侧轴方向:y轴方向,水平向下。
1、flex-direction:布局方向(主轴方向)
flex-direction属性决定主轴的方向(即项目的排列方向)
注意:主轴和侧轴是会变化的,就看flex-direction设置谁为主轴,而我们的子元素是跟着主轴来排列的。
| 属性值 | 说明 |
|---|---|
| row(默认值) | 主轴为水平方向,起点在左端 |
| row-reverse | 主轴为水平方向(水平布局),起点在右端 |
| column | 主轴为垂直方向(垂直布局),起点在上沿 |
| column-reverse | 主轴为垂直方向(垂直布局),起点在下沿 |
我们可以看一下row-reverse的效果:
<!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>003</title>
</head>
<style>
.a {
width: 400px;
height: 200px;
display: flex;
flex-direction: row-reverse;
}
.a1 {
width: 200px;
height: 200px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
}
.a2 {
width: 200px;
height: 200px;
background-color: yellow;
margin-top: 7px;
margin-left: 5px;
}
</style>
<body>
<div class="a">
<div class="a1">a1</div>
<div class="a2">a2</div>
</div>
</body>
</html>

此时,a1在a2的右边,证明此时布局的方向是从右边开始。
下面我们修改上面的代码,把主轴设置为y轴,看一看元素的排列情况:
...
.a {
width: 400px;
height: 200px;
display: flex;
flex-direction:column;
}
....

2、flex-wrap:环绕效果(子元素是否换行)
默认情况下,项目都排在一条线(又称轴线)上,flex-wrap布局中默认是不换行的。如果子元素的宽度加一起超过父元素,会缩小每一个子元素的宽度。
| 属性值 | 说明 |
|---|---|
| nowrap | 默认值,表示不换行 |
| wrap | 换行,第一行在上方 |
| wrap-reverse | 换行,第一行在下方 |
我们可以看一下wrap-reverse的效果:
<!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>003</title>
</head>
<style>
.a {
display: flex;
flex-wrap: wrap-reverse;
}
.a1,.a2,.a3,.a4,.a5 {
width: 400px;
height: 200px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
}
</style>
<body>
<div class="a">
<div class="a1">a1</div>
<div class="a2">a2</div>
<div class="a3">a3</div>
<div class="a4">a4</div>
<div class="a5">a5</div>
</div>
</body>
</html>

可以发现,此时,当盒子变多时会自动换行,且第一行在下方。
若是我们不使用wrap-reverse的话效果就会如下图:

3、flex-flow(综合属性)
是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。基本语法如下:
flex-flow: row nowrap;
4、justify-content:对齐方式(主轴上的子元素排列方式)
justify-content属性定义了项目在主轴上的对齐方式。
注意:使用这个属性之前一定要确定好主轴是哪个。
| 属性值 | 说明 |
|---|---|
| flex-start | 默认值,左对齐 |
| flex-end | 右对齐 |
| center | 居中 |
| space-between | 先两边贴边,再平分剩余空间。即两端对齐,项目之间的间隔相等 |
| space-around | 平分剩余空间,即项目两侧的间距相同,项目间的间距 比两侧的间距大一倍 |
首先看一下center的效果:
<!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>003</title>
</head>
<style>
.a {
display: flex;
justify-content: center;
}
.a1,.a2,.a3{
width:200px;
height: 200px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
}
</style>
<body>
<div class="a">
<div class="a1">a1</div>
<div class="a2">a2</div>
<div class="a3">a3</div>
</div>
</body>
</html>

此时盒子们在页面中居中布局。
然后看一下space-around的效果:
<!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>003</title>
</head>
<style>
.a {
display: flex;
justify-content:space-around;
}
.a1,.a2,.a3{
width:200px;
height: 200px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
}
</style>
<body>
<div class="a">
<div class="a1">a1</div>
<div class="a2">a2</div>
<div class="a3">a3</div>
</div>
</body>
</html>

此时项目两侧的间距相同,且项目间的间距比两侧的间距大一倍。
5、align-items(设置侧轴上的子元素排列方式,单行)
该属性是控制子项在侧轴(默认是y轴)上的排列方式,在子项为单项的时候使用
| 属性值 | 说明 |
|---|---|
| flex-start | 交叉轴的起点对齐 |
| flex-end | 交叉轴的终点对齐 |
| center | 交叉轴的中点对齐 |
| baseline | 项目的第一行文字的基线对齐 |
| stretch(默认值 | 如果项目未设置高度或设为auto, 将占满整个容器的高度 |
首先看一下center的效果:
<!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>003</title>
</head>
<style>
.a {
display: flex;
align-items: center;
}
.a1{
width:200px;
height: 200px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
}
.a2{
width:200px;
height:500px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
}
.a3{
width:200px;
height: 100px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
}
</style>
<body>
<div class="a">
<div class="a1">a1</div>
<div class="a2">a2</div>
<div class="a3">a3</div>
</div>
</body>
</html>

然后看一下flex-end的效果,改动代码如下:
...
.a {
display: flex;
align-items: flex-end;
}
...

6、 align-content(设置侧轴上的子元素排列方式,多行)
定义了多根轴线的对齐方式,如果项目只有一根轴线,那么该属性将不起作用。即设置了子项在侧轴上的排列方式,且只能用于子项出现换行的情况(此时出现多行)。
| 属性值 | 说明 |
|---|---|
| flex-start | 默认值在侧轴的头部开始排列,与交叉轴的起点对齐 |
| flex-end | 在侧轴的尾部开始排列,与交叉轴的终点对齐 |
| center | 在侧轴中间显示,与交叉轴的中点对齐 |
| space-between | 子项在侧轴先分布在两头,再平分剩余空间,即与交叉轴两端对齐,轴线之间的间隔平均分布 |
| space-around | 子项在侧轴平分剩余空间,即每根轴线两侧的间隔都相等。所以, 轴线之间的间隔比轴线与边框的间隔大一倍 |
| stretch(默认值) | 设置子项元素高度平分父元素高度 |

(图来源于网上,如有侵权,请联系)
现在我们来看看align-content: space-between;的显示效果:
<!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>003</title>
</head>
<style>
.a {
width: 800px;
height: 600px;
background-color: pink;
display: flex;
flex-direction: row;/* 确定主轴 */
flex-wrap: wrap;/* 设置可以换行,所以此时会有多行侧轴,此时我们侧轴上控制子元素的对齐方式用align-content */
align-content: space-between;
}
.a1,.a2,.a3,.a4,.a5,.a6,.a7{
width:200px;
height: 100px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
}
</style>
<body>
<div class="a">
<div class="a1">a1</div>
<div class="a2">a2</div>
<div class="a3">a3</div>
<div class="a4">a4</div>
<div class="a5">a5</div>
<div class="a6">a6</div>
<div class="a7">a7</div>
</div>
</body>
</html>

align-content和align-items的区别
1、align-items适用于单行情况下,只有上对齐,下对齐,居中和拉伸;
2、align-content适用于换行(多行)的情况下(单行情况下无效),可以设置上对齐,下对齐,居中,拉伸,平均分配剩余空间等属性值;
3、总结:单行找align-items,多行找align-content。
三、弹性盒子的项目属性
1、flex属性
定义子项目分配剩余空间,用flex来表示占多少份数。它的属性值为数字,默认为0。
如:
<!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>003</title>
</head>
<style>
.a {
width: 800px;
height: 600px;
background-color: pink;
margin: 0 auto;
display: flex;
}
.a1,.a3{
width:200px;
background-color: red;
}
.a2{
flex: 1;/* 表示它占满所有的剩余空间 */
background-color: green;
}
</style>
<body>
<div class="a">
<div class="a1">a1</div>
<div class="a2">a2</div>
<div class="a3">a3</div>
</div>
</body>
</html>
如果我们想要实现三等分,也可以使用flex属性来完成:
<!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>003</title>
</head>
<style>
.a {
width: 800px;
height: 600px;
background-color: pink;
margin: 0 auto;
display: flex;
}
.a1,.a2,.a3{
flex: 1;/* 当不给子元素设置宽度时,会默认剩余空间是父盒子的所有,所以我们只要给每个子元素都设置flex为1,他们每个都占剩余空间的一份,即最后的结果就会平分父盒子的宽度 */
}
</style>
<body>
<div class="a">
<div class="a1">a1</div>
<div class="a2">a2</div>
<div class="a3">a3</div>
</div>
</body>
</html>

还可以通过flex值的设置使子项按比例分配剩余空间,如:
<!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>003</title>
</head>
<style>
.a {
width: 800px;
height: 600px;
background-color: pink;
margin: 0 auto;
display: flex;
}
.a1,.a3{
flex: 1;/* 当不给子元素设置宽度时,会默认剩余空间是父盒子的所有,所以我们只要给每个子元素都设置flex为1,他们每个都占剩余空间的一份,即最后的结果就会平分父盒子的宽度 */
}
div div:nth-child(2){
flex: 2;/* 此时会把父盒子分为四份,盒子a2占两份,其他盒子占一份 */
background-color: yellow;
}
</style>
<body>
<div class="a">
<div class="a1">a1</div>
<div class="a2">a2</div>
<div class="a3">a3</div>
</div>
</body>
</html>

2、order属性
定义项目的排列顺序,数字越小排列越靠前,默认为0,可以为负值。
示例如下:
<!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>003</title>
</head>
<style>
.a {
display: flex;
}
.a1{
width:200px;
height: 200px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
order: -2;
}
.a2{
width:200px;
height:500px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
order: 7;
}
.a3{
width:200px;
height: 100px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
order: -9;
}
</style>
<body>
<div class="a">
<div class="a1">a1</div>
<div class="a2">a2</div>
<div class="a3">a3</div>
</div>
</body>
</html>

此时a2的order值最大,a3最小,所以a2排列在最右边,a3排列在最左边。
3、flex-grow属性
flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为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>003</title>
</head>
<style>
.a {
display: flex;
}
.a1{
width:200px;
height: 200px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
order: -2;
flex-grow: 1;
}
.a2{
width:200px;
height:500px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
order: 7;
flex-grow: 1;
}
.a3{
width:200px;
height: 100px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
order: -9;
flex-grow: 1;
}
</style>
<body>
<div class="a">
<div class="a1">a1</div>
<div class="a2">a2</div>
<div class="a3">a3</div>
</div>
</body>
</html>

当三个盒子的flex-grow值都为1时,就算给他们设定了宽度,他们也依旧平分了所有空间。
改动代码如下:
.a1{
width:200px;
height: 200px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
order: -2;
flex-grow:2;
}

可以明显看出,当a1的flex-grow值为2,其余盒子值为1时,a1占据的剩余空间是a2和a3的两倍。
4、flex-shrink属性
flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为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>003</title>
</head>
<style>
.a {
display: flex;
}
.a1{
width:600px;
height: 200px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
flex-shrink: 0;
}
.a2{
width:600px;
height:200px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
flex-shrink: 1;
}
.a3{
width:600px;
height: 100px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
flex-shrink: 1;
}
</style>
<body>
<div class="a">
<div class="a1">a1</div>
<div class="a2">a2</div>
<div class="a3">a3</div>
</div>
</body>
</html>

此时a1的flex-shrink的值为0,其余为1,所以当空间不足时,a1不缩小,a2和a3缩小。
5、align-self属性
align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
语法格式:
align-self: auto | flex-start | flex-end | center | baseline | stretch;
示例如下:
<!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>003</title>
</head>
<style>
.a {
display: flex;
}
.a1{
width:300px;
height: 500px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
}
.a2{
width:300px;
height:300px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
align-self: flex-end;
}
.a3{
width:300px;
height: 400px;
background-color: red;
margin-top: 7px;
margin-left: 5px;
}
</style>
<body>
<div class="a">
<div class="a1">a1</div>
<div class="a2">a2</div>
<div class="a3">a3</div>
</div>
</body>
</html>
a2的对齐方式为从下开始,其余盒子从上开始。

注意:弹性布局默认不改变项目的宽度,但是它默认改变项目的高度。如果项目没有显式指定高度,就将占据容器的所有高度。
本文详细介绍了CSS弹性盒子布局,包括基本概念、容器属性和项目属性。通过实例展示了flex-direction、flex-wrap、justify-content、align-items等属性的用法,并解析了它们在不同场景下的效果,帮助开发者掌握flex布局技巧。
&spm=1001.2101.3001.5002&articleId=120828876&d=1&t=3&u=d391d2a8eda64f10a7ff3e30ce3a2887)
5万+

被折叠的 条评论
为什么被折叠?



