批改状态:合格
老师批语:对于第一次接触的同学, 是会感觉到好奇和吃力的, 趁双休日 无课, 好好回看一下课程, 再找一些资料阅读一下, 加深理解就好了
| 序号 | 简记 | 术语 |
|---|---|---|
| 1 | 二成员 | 容器和项目(container / item) |
| 2 | 二根轴 | 主轴与交叉轴(main-axis / cross-axis) |
| 3 | 二根线 | 起始线与结束线(flex-start / flex-end) |
| 序号 | 属性 | 描述 |
|---|---|---|
| 1 | flex-direction |
设置容器中的主轴方向: 行/水平方向, 列/垂直方向 |
| 2 | flex-wrap |
是否允许创建多行容器,即 flex 项目一行排列不下时, 是否允许换行 |
| 3 | flex-flow |
简化 flex-direction, flex-wrap 属性 |
| 4 | justify-content |
设置 flex 项目在主轴上对齐方式 |
| 5 | align-items |
设置 flex 项目在交叉轴上对齐方式 |
| 6 | align-content |
多行容器中,项目在交叉轴上的对齐方式 |
| 序号 | 属性 | 描述 |
|---|---|---|
| 1 | flex-basis |
项目宽度: 项目分配主轴剩余空间之前, 项目所占据的主轴空间宽度 |
| 2 | flex-grow |
项目的宽度扩展: 将主轴上的剩余空间按比例分配给指定项目 |
| 3 | flex-shrink |
项目的宽度收缩: 将项目上多出空间按比例在项目间进行缩减 |
| 4 | flex |
是上面三个属性的简化缩写: flex: flex-grow flex-shrink flex-basis |
| 5 | align-self |
单独自定义某个项目在交叉轴上的对齐方式 |
| 6 | order |
自定义项目在主轴上的排列顺序,默认为 0,书写顺序,值越小位置越靠前 |
display属性| 序号 | 属性值 | 描述 | 备注 |
|---|---|---|---|
| 1 | flex; |
创建 flex 块级容器 | 内部子元素自动成为 flex 项目 |
| 2 | inline-flex; |
创建 flex 行内容器 | 内部子元素自动成为 flex 项目 |
| 序号 | 容器/项目 | 默认行为 |
|---|---|---|
| 1 | 容器主轴 | 水平方向 |
| 2 | 项目排列 | 沿主轴起始线排列(当前起始线居左) |
| 3 | 项目类型 | 自动转换”行内块级”元素,不管之前是什么类型 |
| 4 | 容器主轴空间不足时 | 项目自动收缩尺寸以适应空间变化,不会换行显示 |
| 5 | 容器主轴存在未分配空间时 | 项目保持自身大小不会放大并充满空间 |
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title><style>/* 容器 */.container {width: 300px;height: 150px;display: flex;/* display: inline-flex; */}/* 子元素 */.item {width: 100px;height: 50px;background-color: fuchsia;font-size: 1.5rem;}</style></head><body><div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div></div><div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div></div></body></html>

flex-direction属性| 序号 | 属性值 | 描述 |
|---|---|---|
| 1 | row默认值 |
主轴水平: 起始线居中,项目从左到右显示 |
| 2 | row-reverse |
主轴水平:起始线居右, 项目从右向左显示 |
| 3 | column |
主轴垂直: 起始线居上,项目从上向下显示 |
| 4 | column-reverse |
主轴垂直: 起始线居下,项目从下向上显示 |
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>主轴方向</title><style>/* 容器 */.container {width: 300px;height: 150px;display: flex;/* display: inline-flex; */}.container {flex-direction: row;flex-direction: row-reverse;flex-direction: column;flex-direction: column-reverse;}/* 子元素 */.item {width: 100px;height: 50px;background-color: fuchsia;font-size: 1.5rem;}</style></head><body><div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div></div></body></html>

flex-wrap属性| 序号 | 属性值 | 描述 |
|---|---|---|
| 1 | nowrap默认值 |
项目不换行: 单行容器 |
| 2 | wrap |
项目换行: 多行容器,第一行在上方 |
| 3 | wrap-reverse |
项目换行: 多行容器,第一行在下方 |
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>主轴方向</title><style>/* 容器 */.container {width: 300px;height: 150px;display: flex;/* display: inline-flex; */}.container {flex-direction: row;/* flex-direction: row-reverse; *//* flex-direction: column; *//* flex-direction: column-reverse; */}.container {flex-wrap: wrap;flex-wrap: nowrap;flex-wrap: wrap-reverse;}/* 子元素 */.item {width: 100px;height: 50px;background-color: fuchsia;font-size: 1.5rem;}</style></head><body><div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div></div></body></html>

flex-flow属性flex-flow是属性flex-direction和flex-wrap的简写flex-flow: flex-direction flex-wrap| 属性值 | 描述 |
|---|---|
row nowrap默认值 |
主轴水平, 项目不换行 |
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>主轴方向是否换行的简写</title><style>/* 容器 */.container {width: 300px;height: 150px;display: flex;/* display: inline-flex; */}.container {flex-flow: row nowrap;flex-flow: row wrap;flex-flow: column nowrap;flex-flow: column wrap;}/* 子元素 */.item {width: 100px;height: 50px;background-color: fuchsia;font-size: 1.5rem;}</style></head><body><div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div></div></body></html>

justify-content属性当容器中主轴方向上存在剩余空间时, 该属性才有意义
| 序号 | 属性值 | 描述 |
|---|---|---|
| 1 | flex-start默认 |
所有项目与主轴起始线对齐 |
| 2 | flex-end |
所有项目与主轴终止线对齐 |
| 3 | center |
所有项目与主轴中间线对齐: 居中对齐 |
| 4 | space-between |
两端对齐: 剩余空间在头尾项目之外的项目间平均分配 |
| 5 | space-around |
分散对齐: 剩余空间在每个项目二侧平均分配 |
| 6 | space-evenly |
平均对齐: 剩余空间在每个项目之间平均分配 |
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>主轴方向对齐方式</title><style>/* 容器 */.container {width: 300px;height: 150px;display: flex;/* display: inline-flex; */}.container {justify-content: flex-start;justify-content: flex-end;justify-content: center;justify-content: space-around;/* justify-content: space-between; */justify-content: space-evenly;}/* 子元素 */.item {width: 50px;height: 50px;background-color: fuchsia;font-size: 1.5rem;}</style></head><body><div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div></div></body></html>

align-items属性| 序号 | 属性值 | 描述 |
|---|---|---|
| 1 | flex-start默认 |
与交叉轴起始线对齐 |
| 2 | flex-end |
与交叉轴终止线对齐 |
| 3 | center |
与交叉轴中间线对齐: 居中对齐 |
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>交叉轴上对齐方式</title><style>/* 容器 */.container {width: 300px;height: 150px;display: flex;/* display: inline-flex; */}.container {flex-flow: word nowrap;align-items: flex-start;align-items: flex-end;align-items: center;}/* 子元素 */.item {width: 50px;height: 50px;background-color: fuchsia;font-size: 1.5rem;}</style></head><body><div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div></div></body></html>

align-content属性| 序号 | 属性值 | 描述 |
|---|---|---|
| 1 | stretch默认 |
项目拉伸占据整个交叉轴 |
| 1 | flex-start |
所有项目与交叉轴起始线(顶部)对齐 |
| 2 | flex-end |
所有项目与交叉轴终止线对齐 |
| 3 | center |
所有项目与交叉轴中间线对齐: 居中对齐 |
| 4 | space-between |
两端对齐: 剩余空间在头尾项目之外的项目间平均分配 |
| 5 | space-around |
分散对齐: 剩余空间在每个项目二侧平均分配 |
| 6 | space-evenly |
平均对齐: 剩余空间在每个项目之间平均分配 |
提示: 多行容器中通过设置
flex-wrap: wrap | wrap-reverse实现
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>多行容器对齐方式</title><style>/* 容器 */.container {width: 300px;height: 150px;display: flex;/* display: inline-flex; */}.container {flex-flow: row wrap;/* 自动拉伸align-content: stretch; */align-content: stretch;align-content: space-evenly;align-content: space-between;align-content: space-around;}/* 子元素 */.item {width: 50px;height: 50px;background-color: fuchsia;font-size: 1.5rem;}</style></head><body><div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div><div class="item">5</div><div class="item">6</div><div class="item">7</div><div class="item">8</div><div class="item">9</div></div></body></html>

align-self属性align-items, 用以自定义某个项目的对齐方式| 序号 | 属性值 | 描述 |
|---|---|---|
| 1 | auto默认值 |
继承 align-items 属性值 |
| 2 | flex-start |
与交叉轴起始线对齐 |
| 3 | flex-end |
与交叉轴终止线对齐 |
| 4 | center |
与交叉轴中间线对齐: 居中对齐 |
| 5 | stretch |
在交叉轴方向上拉伸 |
| 6 | baseline |
与基线对齐(与内容相关用得极少) |
```h<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>xiangmushun项目顺序对齐方式</title>
<style>
/ 容器 /
.container {
width: 300px;
height: 150px;
display: flex;
/ display: inline-flex; /
}
.container {
}
/ 子元素 /
.item {
width: 50px;
height: 50px;
background-color: fuchsia;
font-size: 1.5rem;
order: 0;
}
.item:first-of-type {
order: 4;
background-color: greenyellow;
}
.item:nth-of-type(2) {
order: 3;
background-color: rgb(6, 221, 236);
}
.item:nth-of-type(3) {
order: 2;
background-color: mediumblue;
}
.item:last-of-type {
background-color: mediumturquoise;
}
</style>
</head>
<body>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</body>
</html>
tml
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>xiangmushun项目顺序对齐方式</title>
<style>
/ 容器 /
.container {
width: 300px;
height: 150px;
display: flex;
/ display: inline-flex; /
}
.container {
}
/ 子元素 /
.item {
width: 50px;
height: 50px;
background-color: fuchsia;
font-size: 1.5rem;
order: 0;
}
.item:first-of-type {
order: 4;
background-color: greenyellow;
}
.item:nth-of-type(2) {
order: 3;
background-color: rgb(6, 221, 236);
}
.item:nth-of-type(3) {
order: 2;
background-color: mediumblue;
}
.item:last-of-type {
background-color: mediumturquoise;
}
</style>
</head>
<body>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</body>
</html>
# flex 项目放大因子## 1. `flex-grow`属性- 在容器主轴上存在剩余空间时, `flex-grow`才有意义- 该属性的值,称为**放大因子**, 常见的属性值如下:| 序号 | 属性值 | 描述 || ---- | --------- | -------------------- || 1 | `0`默认值 | 不放大,保持初始值 || 2 | `initial` | 设置默认值,与`0`等效 || 3 | `n` | 放大因子: 正数 |### 9```html<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>xiangmushun项目独立对齐方式</title><style>/* 容器 */.container {width: 300px;height: 150px;display: flex;/* display: inline-flex; */}.container {}/* 子元素 */.item {width: 50px;height: 50px;background-color: fuchsia;font-size: 1.5rem;align-self: auto;}.item:first-of-type {height: inherit;align-self: stretch;background-color: greenyellow;}.item:nth-of-type(2) {align-self: flex-end;background-color: rgb(6, 221, 236);}.item:nth-of-type(3) {align-self: flex-start;background-color: mediumblue;}.item:last-of-type {align-self: center;background-color: red;}</style></head><body><div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div></div></body></html>

flex-shrink属性flex-shrink才有意义| 序号 | 属性值 | 描述 |
|---|---|---|
| 1 | 1默认值 |
允许项目收缩 |
| 2 | initial |
设置初始默认值,与 1 等效 |
| 3 | 0 |
禁止收缩,保持原始尺寸 |
| 4 | n |
收缩因子: 正数 |
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>xiangmushun项目放大因子</title><style>/* 容器 */.container {width: 300px;height: 150px;display: flex;/* display: inline-flex; */}.container {}/* 子元素 */.item {width: 50px;height: 50px;background-color: fuchsia;font-size: 1.5rem;flex-grow: 0;flex-grow: initial;/* flex-grow: 1; *//* flex-grow: 1; */}.item:first-of-type {flex-grow: 1;background-color: greenyellow;}.item:nth-of-type(2) {flex-grow: 2;background-color: rgb(6, 221, 236);}.item:last-of-type {flex-grow: 3;background-color: red;}</style></head><body><div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div></div></body></html>

flex-basis属性min-width/min-height值覆盖| 序号 | 属性值 | 描述 |
|---|---|---|
| 1 | auto |
默认值: 项目原来的大小 |
| 2 | px |
像素 |
| 3 | % |
百分比 |
优先级: 项目大小 <
flex-basis<min-width/height11
```html
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>项目收缩因子</title>
<style>
/ 容器 /
.container {
width: 180px;
height: 150px;
display: flex;
flex-flow: row nowrap;
/ display: inline-flex; /
}
.container {}/* 子元素 */.item {width: 100px;height: 50px;background-color: fuchsia;font-size: 1.5rem;flex-shrink: 0;}.item:first-of-type {background-color: greenyellow;flex-shrink: 1;}.item:nth-of-type(2) {flex-shrink: 2;background-color: rgb(6, 221, 236);}.item:last-of-type {flex-shrink: 3;background-color: red;}</style>
</head>
<body>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
</body>
</html>
# flex 项目缩放的简写## 1. `flex`属性- 项目放大,缩小与计算尺寸,对于项目非常重要,也很常用- 每次都要写这三个属性,非常的麻烦,且没有必要- `flex`属性,可以将以上三个属性进行简化:- 语法: `flex: flex-grow flex-shrink flex-basis`### 1.1 三值语法| 序号 | 属性值 | 描述 || ---- | ------------------ | ------------- || 1 | 第一个值: 整数 | `flex-grow` || 2 | 第二个值: 整数 | `flex-shrink` || 3 | 第三个值: 有效宽度 | `flex-basis` |举例:| 序号 | 案例 | 描述 || ---- | ----------------- | ------------------------------- || 1 | `flex: 0 1 auto` | 默认值: 不放大,可收缩, 初始宽度 || 2 | `flex: 1 1 auto` | 项目自动放大或收缩适应容器 || 3 | `flex: 0 0 100px` | 按计算大小填充到容器中 |### 1.2 双值语法| 序号 | 属性值 | 描述 || ---- | ------------------ | ------------ || 1 | 第一个值: 整数 | `flex-grow` || 3 | 第二个值: 有效宽度 | `flex-basis` |举例:| 案例 | 描述 || --------------- | ------------------------------- || `flex: 0 180px` | 禁止放大,按计算大小填充到容器中 |### 1.3 单值语法| 序号 | 属性值 | 描述 || ---- | -------- | ----------------------- || 1 | 整数 | `flex-grow` || 2 | 有效宽度 | `flex-basis` || 3 | 关键字 | `initial | auto | none` |举例:| 序号 | 案例 | 描述 || ---- | ------------- | ----------------- || 1 | `flex: 1` | `flex: 1 1 auto` || 2 | `flex: 180px` | `flex: 1 1 180px` || 3 | `initial` | `flex: 0 1 auto` || 4 | `auto` | `flex: 1 1 auto` || 5 | `none` | `flex: 0 0 auto` |> 推荐使用`flex`, 就像推荐使用`flex-grow`设置主轴与换行一样```html<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>项目收缩因子</title><style>/* 容器 */.container {width: 300px;height: 150px;display: flex;/* display: inline-flex; */}.container {}/* 子元素 */.item {width: 100px;height: 50px;background-color: fuchsia;font-size: 1.5rem;}.item:first-of-type {flex: 0 1 auto;flex: 1 1 auto;/* flex: 0 1 80px; */background-color: greenyellow;}.item:nth-of-type(2) {flex: 0 100px;background-color: rgb(6, 221, 236);}.item:last-of-type {flex: auto;/* flex: 1; */flex: 0;/* flex: none; *//* flex: 0 0 auto; */flex: 0 0 150px;background-color: red;}</style></head><body><div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div></div></body></html>

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号