flex 容器与项目
1. display
属性
序号 | 属性值 | 描述 | 备注 |
---|---|---|---|
1 | flex; |
创建 flex 块级容器 | 内部子元素自动成为 flex 项目 |
2 | inline-flex; |
创建 flex 行内容器 | 内部子元素自动成为 flex 项目 |
2. flex 容器与项目特征
序号 | 容器/项目 | 默认行为 |
---|---|---|
1 | 容器主轴 | 水平方向 |
2 | 项目排列 | 沿主轴起始线排列(当前起始线居左) |
3 | 项目类型 | 自动转换”行内块级”元素,不管之前是什么类型 |
4 | 容器主轴空间不足时 | 项目自动收缩尺寸以适应空间变化,不会换行显示 |
5 | 容器主轴存在未分配空间时 | 项目保持自身大小不会放大并充满空间 |
3.flex布局代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
display: flex;
background-color: cornflowerblue;
}
.flex-item {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
</div>
</body>
</html>
4.flex布局截图
5.容器属性
- 定义在flex容器中的属性,容器属性定义了容器中所有的项目应有的行为及表现
5.1.
flex-direction
- 定义项目在容器中排列的方向,有4种属性值:
row | row-reverse | column | column-reverse
item
在容器中默认是横向从左到右排列的。除此之外,我们还可以将它设置为横向从右到左(row-reverse
)、纵向从上到下(column
)、纵向从下到上(column-reverse
)5.1.1
row
(默认值) ,项目从左到右排列flex-direction:row
5.1.2
row
(默认值)代码实例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
display: flex;
background-color: cornflowerblue;
}
.flex-item {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
.flex-content {
flex-direction: row;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
</div>
</body>
</html>
5.1.3 row
(默认值)实例截图
- 可以看到和默认没有变化
5.2.row-reverse
5.2.1row-reverse
项目反向排列(从右到左)
flex-direction:row-reverse
5.2.2代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
display: flex;
background-color: cornflowerblue;
}
.flex-item {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
/* 默认 */
/* .flex-content {
flex-direction: row;
} */
/* 重右到左 */
.flex-content {
flex-direction: row-reverse;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
</div>
</body>
</html>
5.2.3实列截图
5.3.column
5.3.1.项目纵向排列,从上到下)
flex-direction:column
5.3.2代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
display: flex;
background-color: cornflowerblue;
}
.flex-item {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
/* 默认 */
/* .flex-content {
flex-direction: row;
} */
/* 重右到左 */
/* .flex-content {
flex-direction: row-reverse;
} */
/* 重上往下 */
.flex-content {
flex-direction: column;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
</div>
</body>
</html>
5.3.3实列截图
5.4.column-reverse
5.4.1.项目纵向排列,从上到下)
flex-direction:column-reverse
5.4.2代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
display: flex;
background-color: cornflowerblue;
}
.flex-item {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
/* 默认 */
/* .flex-content {
flex-direction: row;
} */
/* 重右到左 */
/* .flex-content {
flex-direction: row-reverse;
} */
/* 重上往下 */
/* .flex-content {
flex-direction: column;
} */
/* 重下往上 */
.flex-content {
flex-direction: column-reverse;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
</div>
</body>
</html>
5.4.3实列截图
6.flex-wrap
- 定义了容器中的项目在容器宽度不足以容纳项目时是否换换行。
6.1.nowrap
(默认值),item在容器中不换行,但item所设置的width可能失效
6.1.1.项目不换行)
flex-wrap:nowrap
6.1.2代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
height: 100px;
display: flex;
background-color: cornflowerblue;
}
.flex-item {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
/* 默认 */
/* .flex-content {
flex-direction: row;
} */
/* 重右到左 */
/* .flex-content {
flex-direction: row-reverse;
} */
/* 重上往下 */
/* .flex-content {
flex-direction: column;
} */
/* 重下往上 */
/* .flex-content {
flex-direction: column-reverse;
} */
/* 项目不换行,我们总宽度为620px,现在五个项目是650px */
.flex-content {
flex-wrap: nowrap;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
<div class="flex-item">4</div>
<div class="flex-item">5</div>
</div>
</body>
</html>
6.1.3实列截图
- 由此我们可以看到它是默认还在一行的
6.2.wrap
,item
宽度不改变,容纳不下新增item时,自动换行
6.2.1.项目自动换行换行)
flex-wrap:wrap
6.2.2代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
height: 100px;
display: flex;
background-color: cornflowerblue;
}
.flex-item {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
/* 默认 */
/* .flex-content {
flex-direction: row;
} */
/* 重右到左 */
/* .flex-content {
flex-direction: row-reverse;
} */
/* 重上往下 */
/* .flex-content {
flex-direction: column;
} */
/* 重下往上 */
/* .flex-content {
flex-direction: column-reverse;
} */
/* 项目不换行,我们总宽度为620px,现在五个项目是650px */
/* .flex-content {
flex-wrap: nowrap;
} */
/* 现在项目自动换行了 */
.flex-content {
flex-wrap: wrap;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
<div class="flex-item">4</div>
<div class="flex-item">5</div>
</div>
</body>
</html>
6.2.3实列截图
6.3.wrap-reverse
,反向换行,正常情况下换行是空间不够时向下换行,而wrap-reverse
是空间不够时向上换行
6.3.1.项目反向)
flex-wrap:wrap-reverse
6.3.2代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
height: 180px;
display: flex;
background-color: cornflowerblue;
}
.flex-item {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
/* 默认 */
/* .flex-content {
flex-direction: row;
} */
/* 重右到左 */
/* .flex-content {
flex-direction: row-reverse;
} */
/* 重上往下 */
/* .flex-content {
flex-direction: column;
} */
/* 重下往上 */
/* .flex-content {
flex-direction: column-reverse;
} */
/* 项目不换行,我们总宽度为620px,现在五个项目是650px */
/* .flex-content {
flex-wrap: nowrap;
} */
/* 现在项目自动换行了 */
/* .flex-content {
flex-wrap: wrap;
} */
/* 项目反向 */
.flex-content {
flex-wrap: wrap-reverse;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
<div class="flex-item">4</div>
<div class="flex-item">5</div>
</div>
</body>
</html>
6.3.3实列截图
7.flex-flow
- 是flex-direction与flex-wrap的简写形式
flex-flow:<flex-direction> <flex-wrap>
7.1代码实列
.flex-content{
flex-flow:row wrap;
}
//相当于
.flex-content{
flex-direction:row;
flex-wrap:wrap;
}
8.1justify-content
- 定义了项目的水平方向对齐方式,有5种属性值
flex-start | flex-end | center | space-between | space-around
8.1.1flex-start
项目左对齐
justify-content: flex-start
8.1.2代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
height: 180px;
display: flex;
background-color: cornflowerblue;
/* 项目左对齐 */
justify-content: flex-start;
}
.flex-item {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
</div>
</body>
</html>
8.1.3截图实列
8.2.1flex-end
项目右对齐
justify-content: flex-end
8.2.2代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
height: 180px;
display: flex;
background-color: cornflowerblue;
/* 项目左对齐 */
/* justify-content: flex-start; */
/* 项目右对齐 */
justify-content: flex-end;
}
.flex-item {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
</div>
</body>
</html>
8.2.3截图实列
8.3.1center
项目居中
justify-content: center
8.3.2代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
height: 180px;
display: flex;
background-color: cornflowerblue;
/* 项目左对齐 */
/* justify-content: flex-start; */
/* 项目右对齐 */
/* justify-content: flex-end; */
/* 项目居中 */
justify-content: center;
}
.flex-item {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
</div>
</body>
</html>
8.3.3截图实列
8.4.1space-between
项目两端对齐
justify-content: space-between
8.4.2代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
height: 180px;
display: flex;
background-color: cornflowerblue;
/* 项目左对齐 */
/* justify-content: flex-start; */
/* 项目右对齐 */
/* justify-content: flex-end; */
/* 项目居中 */
/* justify-content: center; */
/* 项目两端对齐 */
justify-content: space-between;
}
.flex-item {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
</div>
</body>
</html>
8.4.3截图实列
8.5.1space-around
每个项目间隔相同
justify-content: space-around
8.5.2代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
height: 180px;
display: flex;
background-color: cornflowerblue;
/* 项目左对齐 */
/* justify-content: flex-start; */
/* 项目右对齐 */
/* justify-content: flex-end; */
/* 项目居中 */
/* justify-content: center; */
/* 项目两端对齐 */
/* justify-content: space-between; */
/* 项目间隔相同 */
justify-content: space-around;
}
.flex-item {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
</div>
</body>
</html>
8.5.3截图实列
9.1align-items
- 这个属性与
justify-content
恰好相反,align-items
定义了项目的竖直方向对齐方式。其属性值有flex-start | flex-end | center | baseline | strectch
9.1.1
flex-start
项目顶端对齐align-items: flex-start
9.1.2代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
height: 180px;
display: flex;
background-color: cornflowerblue;
/* 项目左对齐 */
/* justify-content: flex-start; */
/* 项目右对齐 */
/* justify-content: flex-end; */
/* 项目居中 */
justify-content: center;
/* 项目两端对齐 */
/* justify-content: space-between; */
/* 项目间隔相同 */
/* justify-content: space-around; */
align-items: flex-start;
}
.flex-item1 {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
.flex-item2 {
width: 130px;
height: 50px;
background-color: rgb(255, 0, 212);
font-size: 1.5rem;
}
.flex-item3 {
width: 130px;
height: 120px;
background-color: rgb(4, 0, 255);
font-size: 1.5rem;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item1">1</div>
<div class="flex-item2">2</div>
<div class="flex-item3">3</div>
</div>
</body>
</html>
9.1.3截图实列
9.2.1flex-end
项目底端对齐
align-items: flex-end
9.2.2代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
height: 180px;
display: flex;
background-color: cornflowerblue;
/* 项目左对齐 */
/* justify-content: flex-start; */
/* 项目右对齐 */
/* justify-content: flex-end; */
/* 项目居中 */
justify-content: center;
/* 项目两端对齐 */
/* justify-content: space-between; */
/* 项目间隔相同 */
/* justify-content: space-around; */
/* 项目顶端对齐 */
/* align-items: flex-start; */
/* 项目底端对齐 */
align-items: flex-end;
}
.flex-item1 {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
.flex-item2 {
width: 130px;
height: 50px;
background-color: rgb(255, 0, 212);
font-size: 1.5rem;
}
.flex-item3 {
width: 130px;
height: 120px;
background-color: rgb(4, 0, 255);
font-size: 1.5rem;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item1">1</div>
<div class="flex-item2">2</div>
<div class="flex-item3">3</div>
</div>
</body>
</html>
9.2.3截图实列
9.3.1center
项目中间对齐
align-items: center
9.3.2代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
height: 180px;
display: flex;
background-color: cornflowerblue;
/* 项目左对齐 */
/* justify-content: flex-start; */
/* 项目右对齐 */
/* justify-content: flex-end; */
/* 项目居中 */
justify-content: center;
/* 项目两端对齐 */
/* justify-content: space-between; */
/* 项目间隔相同 */
/* justify-content: space-around; */
/* 项目顶端对齐 */
/* align-items: flex-start; */
/* 项目底端对齐 */
/* align-items: flex-end; */
/* 项目中间对齐 */
align-items: center;
}
.flex-item1 {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
.flex-item2 {
width: 130px;
height: 50px;
background-color: rgb(255, 0, 212);
font-size: 1.5rem;
}
.flex-item3 {
width: 130px;
height: 120px;
background-color: rgb(4, 0, 255);
font-size: 1.5rem;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item1">1</div>
<div class="flex-item2">2</div>
<div class="flex-item3">3</div>
</div>
</body>
</html>
9.3.3截图实列
10.order
定义了项目的排列顺列,order
的值越小,项目的排列位置越靠前。默认值为0
10.1代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
height: 180px;
display: flex;
background-color: cornflowerblue;
justify-content: center;
}
.flex-item1 {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
.flex-item2 {
width: 130px;
height: 50px;
background-color: rgb(255, 0, 212);
font-size: 1.5rem;
}
.flex-item3 {
width: 130px;
height: 120px;
background-color: rgb(4, 0, 255);
font-size: 1.5rem;
}
.flex-item1 {
order: 3;
}
.flex-item2 {
order: 1;
}
.flex-item3 {
order: 2;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item1">1</div>
<div class="flex-item2">2</div>
<div class="flex-item3">3</div>
</div>
</body>
</html>
10.2截图实列
11.flex-grow
定义了项目的放大比例,默认值为0,表示不放大
在这里依次给
.flex-item
的flex-grow
的值设置为0、2、3。其中0表示.flex-item
的默认宽度,并没有放大。2、3表示将剩余的空间分为2+3=5份,其中item2占2/5份,.flex-item
3占3/5份。11.1代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 620px;
height: 180px;
display: flex;
background-color: cornflowerblue;
justify-content: center;
}
.flex-item1 {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
.flex-item2 {
width: 130px;
height: 50px;
background-color: rgb(255, 0, 212);
font-size: 1.5rem;
}
.flex-item3 {
width: 130px;
height: 120px;
background-color: rgb(4, 0, 255);
font-size: 1.5rem;
}
/* .flex-item1 {
order: 3;
}
.flex-item2 {
order: 1;
}
.flex-item3 {
order: 2;
} */
.flex-item1 {
flex-grow: 0;
}
.flex-item2 {
flex-grow: 2;
}
.flex-item3 {
flex-grow: 3;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item1">1</div>
<div class="flex-item2">2</div>
<div class="flex-item3">3</div>
</div>
</body>
</html>
11.2截图实列
12.flex-shrink
定义了项目的缩小比例。默认值为1,表示当空间不足时将项目缩小至能被空间容纳
flex-shrink
的值必须大于或等于0。当值为负数时,此值无效,相当于默认值。当值为0时,表示项目不缩放。值为1时,项目缩放。在这里,flex-item1、2、3
的宽都被设置为130px,然而总的宽为320px,显然是不够宽的,但是因为flex-item1
的flex-shrink
值为0,所以不会被缩放,flex-item2
、flex-item3
的flex-shrink
值为1,flex
容器容纳不下时缩小了,所以它又能装下了。12.1代码实列
<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 320px;
height: 180px;
display: flex;
background-color: cornflowerblue;
justify-content: center;
}
.flex-item1 {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
.flex-item2 {
width: 130px;
height: 50px;
background-color: rgb(255, 0, 212);
font-size: 1.5rem;
}
.flex-item3 {
width: 130px;
height: 120px;
background-color: rgb(4, 0, 255);
font-size: 1.5rem;
}
/* .flex-item1 {
order: 3;
}
.flex-item2 {
order: 1;
}
.flex-item3 {
order: 2;
} */
/* .flex-item1 {
flex-grow: 0;
}
.flex-item2 {
flex-grow: 2;
}
.flex-item3 {
flex-grow: 3;
} */
.flex-item1 {
flex-shrink: 0;
}
.flex-item2 {
flex-shrink: 1;
}
.flex-item3 {
flex-shrink: 1;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item1">1</div>
<div class="flex-item2">2</div>
<div class="flex-item3">3</div>
</div>
</body>
</html>
12.2截图实列
13.flex-basis
定义了flex items
在被放进一个flex
容器之前的大小(flex-basis
含义)。默认值为auto
通过
flex-basis
的含义可以很直观的感觉到flex-basis
的作用跟width
似乎一样。但是两者是有区别的,尽管两者都可以设置flex-item3
的宽,但在flex
容器中flex-basis
的优先级高于width
,即两者同时存在时,width
会被屏蔽,无论代码中两者的先后顺序如何。13.1代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 720px;
height: 180px;
display: flex;
background-color: cornflowerblue;
justify-content: center;
}
.flex-item1 {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
.flex-item2 {
width: 130px;
height: 80px;
background-color: darkkhaki;
font-size: 1.5rem;
}
.flex-item3 {
height: 80px;
background-color: darkorange;
font-size: 1.5rem;
}
.flex-item3 {
flex-basis: 200px;
width: 130px;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item1">1</div>
<div class="flex-item2">2</div>
<div class="flex-item3">3</div>
</div>
</body>
</html>
13.2截图实列
14flex
是flex-grow
, flex-shrink
和 flex-basis
的简写,默认值为0 1 auto
。flex-shrink
和flex-basis
是可选属性。
15.align-self
定义了单个项目的竖直方向对齐方式,默认值为auto
,表示继承父元素的align-items
属性,如果没有父元素,则等同于stretch
flex-item1
、flex-item3
未设置align-self
,其默认值为auto
,所以继承了父元素的flex-start
保持顶端对齐。而flex-item2
设置了align-self
为flex-end
,固flex-item2
是底端对齐的。15.1代码实列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.flex-content {
width: 720px;
height: 180px;
display: flex;
background-color: cornflowerblue;
justify-content: center;
}
.flex-item1 {
width: 130px;
height: 80px;
background-color: cyan;
font-size: 1.5rem;
}
.flex-item2 {
width: 130px;
height: 80px;
background-color: darkkhaki;
font-size: 1.5rem;
}
.flex-item3 {
width: 130px;
height: 80px;
background-color: darkorange;
font-size: 1.5rem;
}
.flex-content {
align-items: flex-start;
}
.flex-item2 {
align-self: flex-end;
}
</style>
<body>
<div class="flex-content">
<div class="flex-item1">1</div>
<div class="flex-item2">2</div>
<div class="flex-item3">3</div>
</div>
</body>
</html>
15.2截图实列
学习总结:
一、容器属性:
1.flex-direction(方向)
2.flex-wrap(换行)
3.flex-flow(方向、换行的简写)
4.justify-content(水平方向对齐)
5.align-items(竖直方向对齐)
6.align-content(多行对齐)
二、项目属性:
1.order(顺序)
2.flex-grow(放大)
3.flex-shrink(缩小)
4.flex-basis(宽)
5.flex(放大、缩小、宽的简写)
6.align-self (单个竖直方向对齐)