## 4.5 Flexbox 工具类 (详细展开见布局章节) --- ## Bootstrap 4 工具类:4.5 Flexbox 工具类详解与实践 Bootstrap 4 引入了强大的 Flexbox 工具类,极大地简化了 CSS Flexbox 的应用,使得开发者能够更快速、更便捷地构建灵活且响应式的网页布局。Flexbox (Flexible Box Layout Module) 是一种一维的布局模型,它提供了一种高效的方式来对容器内的项目进行排列、对齐和分布空间,尤其擅长处理复杂的应用界面和组件布局。 在 Bootstrap 4 的工具类体系中,Flexbox 工具类被归类在 **Utilities (工具类)** 之下,并在 **Layout (布局)** 章节中进行了更详细的展开。这意味着 Flexbox 工具类是 Bootstrap 布局系统的重要组成部分,但同时也可以作为独立的工具类来使用,为元素赋予 Flexbox 的行为。 ### 4.5.1 Flexbox 基础概念回顾 在深入 Bootstrap 4 Flexbox 工具类之前,我们先简要回顾一下 Flexbox 的核心概念,这有助于更好地理解工具类的作用和使用方法。 Flexbox 布局主要围绕 **Flex 容器 (Flex Container)** 和 **Flex 项目 (Flex Items)** 两个概念展开: * **Flex 容器 (Flex Container):** 应用了 `display: flex` 或 `display: inline-flex` 的 HTML 元素。它是 Flexbox 布局的父元素,负责控制其直接子元素(Flex 项目)的布局行为。 * **Flex 项目 (Flex Items):** Flex 容器的直接子元素。它们会受到 Flex 容器的布局规则的影响。 Flexbox 布局主要沿着两个轴线进行: * **主轴 (Main Axis):** 由 `flex-direction` 属性决定,默认为水平方向 (row)。 * **交叉轴 (Cross Axis):** 垂直于主轴的轴线。 以下 Mermaid 图表可以帮助理解主轴和交叉轴的概念: ```mermaid graph TD subgraph Flex Container direction-- 主轴 (Main Axis) -->items direction-- 交叉轴 (Cross Axis) -->items items[Flex Items] end direction[flex-direction] style Flex Container fill:#f9f,stroke:#333,stroke-width:2px style items fill:#ccf,stroke:#333,stroke-width:1px ``` **常用的 Flexbox 属性包括:** * **容器属性 (Container Properties):** * `display: flex;` 或 `display: inline-flex;`:将元素设置为 Flex 容器。 * `flex-direction`:定义主轴方向(`row`, `row-reverse`, `column`, `column-reverse`)。 * `justify-content`:定义项目在主轴上的对齐方式(`flex-start`, `flex-end`, `center`, `space-between`, `space-around`, `space-evenly`)。 * `align-items`:定义项目在交叉轴上的对齐方式(`flex-start`, `flex-end`, `center`, `baseline`, `stretch`)。 * `align-content`:当交叉轴有多行时,定义行与行之间的对齐方式(`flex-start`, `flex-end`, `center`, `space-between`, `space-around`, `stretch`)。 * `flex-wrap`:定义项目是否换行(`nowrap`, `wrap`, `wrap-reverse`)。 * **项目属性 (Item Properties):** * `order`:定义项目的排列顺序。 * `flex-grow`:定义项目的放大比例。 * `flex-shrink`:定义项目的缩小比例。 * `flex-basis`:定义项目在主轴上的初始大小。 * `align-self`:覆盖容器的 `align-items` 属性,单独定义项目在交叉轴上的对齐方式(`auto`, `flex-start`, `flex-end`, `center`, `baseline`, `stretch`)。 Bootstrap 4 的 Flexbox 工具类正是对这些常用的 Flexbox 属性进行了封装,通过预定义的 CSS 类名,让开发者无需编写大量的自定义 CSS 就能快速实现 Flexbox 布局效果。 ### 4.5.2 Bootstrap 4 Flexbox 工具类详解 Bootstrap 4 提供了一系列预定义的 CSS 类,用于快速设置元素的 Flexbox 属性。这些工具类主要分为以下几个方面: #### 4.5.2.1 启用 Flexbox * **`.d-flex`**: 将元素设置为 `display: flex;`,创建一个 Flex 容器。 * **`.d-inline-flex`**: 将元素设置为 `display: inline-flex;`,创建一个行内 Flex 容器。 **代码实践:** ```html
Flex item 1
Flex item 2
Flex item 3
Inline Flex item 1 Inline Flex item 2 ``` **内容详解:** * `.d-flex` 和 `.d-inline-flex` 是 Bootstrap Flexbox 工具类的基础,它们分别对应 CSS 的 `display: flex` 和 `display: inline-flex` 属性。 * `.d-flex` 将元素设置为块级 Flex 容器,占据父元素的整行宽度。 * `.d-inline-flex` 将元素设置为行内 Flex 容器,只占据内容所需的宽度,可以与其他行内元素并排显示。 **响应式变体:** Bootstrap 4 还提供了响应式变体,可以根据不同的屏幕尺寸应用 Flexbox 属性: * `.d-sm-flex` * `.d-md-flex` * `.d-lg-flex` * `.d-xl-flex` * `.d-sm-inline-flex` * `.d-md-inline-flex` * `.d-lg-inline-flex` * `.d-xl-inline-flex` 例如,`.d-md-flex` 表示在 **中等 (md) 及以上** 屏幕尺寸时应用 `display: flex;`,在较小屏幕尺寸时则不应用。 #### 4.5.2.2 Flex 容器方向 * **`.flex-row`**: 设置主轴方向为水平方向 (从左到右),即 `flex-direction: row;` (默认值,通常无需显式添加)。 * **`.flex-column`**: 设置主轴方向为垂直方向 (从上到下),即 `flex-direction: column;`。 * **`.flex-row-reverse`**: 设置主轴方向为水平方向 (从右到左),即 `flex-direction: row-reverse;`。 * **`.flex-column-reverse`**: 设置主轴方向为垂直方向 (从下到上),即 `flex-direction: column-reverse;`。 **代码实践:** ```html
Column Item 1
Column Item 2
Row Reverse Item 1
Row Reverse Item 2
Column Reverse Item 1
Column Reverse Item 2
``` **内容详解:** * 这些工具类对应 CSS 的 `flex-direction` 属性,用于控制 Flex 容器的主轴方向,从而影响 Flex 项目的排列方向。 * `.flex-row` 是默认值,通常用于水平排列项目。 * `.flex-column` 用于垂直排列项目,常用于侧边栏、导航菜单等垂直布局。 * `.flex-row-reverse` 和 `.flex-column-reverse` 分别反转水平和垂直排列顺序。 **响应式变体:** 同样,Flex 容器方向工具类也提供了响应式变体: * `.flex-sm-row`, `.flex-md-row`, `.flex-lg-row`, `.flex-xl-row` * `.flex-sm-column`, `.flex-md-column`, `.flex-lg-column`, `.flex-xl-column` * `.flex-sm-row-reverse`, `.flex-md-row-reverse`, `.flex-lg-row-reverse`, `.flex-xl-row-reverse` * `.flex-sm-column-reverse`, `.flex-md-column-reverse`, `.flex-lg-column-reverse`, `.flex-xl-column-reverse` 例如,`.flex-md-column` 可以实现在中等及以上屏幕尺寸时垂直排列,在小屏幕尺寸时保持默认的水平排列,实现响应式布局。 #### 4.5.2.3 内容对齐 (Justify Content) `justify-content` 属性定义了项目在 **主轴** 上的对齐方式。Bootstrap 4 提供了以下工具类: * **`.justify-content-start`**: 项目在主轴的起始位置对齐 (默认值)。 * **`.justify-content-end`**: 项目在主轴的结束位置对齐。 * **`.justify-content-center`**: 项目在主轴的居中位置对齐。 * **`.justify-content-between`**: 项目之间均匀分布,首尾项目分别在主轴的起始和结束位置。 * **`.justify-content-around`**: 项目之间均匀分布,每个项目两侧的间隔相等。 * **`.justify-content-evenly`**: 项目之间以及首尾项目与容器边缘的间隔都相等。 以下 Mermaid 图表展示了 `justify-content` 的不同对齐方式 (假设主轴为水平方向): ```mermaid graph TD subgraph justify-content-start a[Item 1] --> b[Item 2] b --> c[Item 3] direction="主轴起点" --> a end subgraph justify-content-end a1[Item 1] --> b1[Item 2] b1 --> c1[Item 3] c1 --> direction1="主轴终点" end subgraph justify-content-center direction2="主轴起点" --> a2[Item 1] a2 --> b2[Item 2] b2 --> c2[Item 3] c2 --> direction3="主轴终点" direction2 -- 居中 --> direction3 end subgraph justify-content-between direction4="主轴起点" --> a3[Item 1] a3 -- 空格均匀分布 --> b3[Item 2] b3 -- 空格均匀分布 --> c3[Item 3] c3 --> direction5="主轴终点" end subgraph justify-content-around direction6="主轴起点" -- 半空格 --> a4[Item 1] a4 -- 空格 --> b4[Item 2] b4 -- 空格 --> c4[Item 3] c4 -- 半空格 --> direction7="主轴终点" end subgraph justify-content-evenly direction8="主轴起点" -- 空格 --> a5[Item 1] a5 -- 空格 --> b5[Item 2] b5 -- 空格 --> c5[Item 3] c5 --> direction9="主轴终点" end style justify-content-start fill:#f0f0f0,stroke:#333,stroke-width:1px style justify-content-end fill:#f0f0f0,stroke:#333,stroke-width:1px style justify-content-center fill:#f0f0f0,stroke:#333,stroke-width:1px style justify-content-between fill:#f0f0f0,stroke:#333,stroke-width:1px style justify-content-around fill:#f0f0f0,stroke:#333,stroke-width:1px style justify-content-evenly fill:#f0f0f0,stroke:#333,stroke-width:1px ``` **代码实践:** ```html
``` **内容详解:** * 这些工具类对应 CSS 的 `justify-content` 属性,用于控制 Flex 项目在主轴上的对齐和分布。 * 根据不同的对齐需求选择合适的工具类,例如: * 导航栏菜单项居中可以使用 `.justify-content-center`。 * 页眉页脚的左右两侧内容对齐可以使用 `.justify-content-between`。 * 在卡片布局中均匀分布卡片可以使用 `.justify-content-around` 或 `.justify-content-evenly`。 **响应式变体:** `justify-content` 工具类同样提供了响应式变体: * `.justify-content-sm-start`, `.justify-content-md-start`, `.justify-content-lg-start`, `.justify-content-xl-start` * `.justify-content-sm-end`, `.justify-content-md-end`, `.justify-content-lg-end`, `.justify-content-xl-end` * `.justify-content-sm-center`, `.justify-content-md-center`, `.justify-content-lg-center`, `.justify-content-xl-center` * `.justify-content-sm-between`, `.justify-content-md-between`, `.justify-content-lg-between`, `.justify-content-xl-between` * `.justify-content-sm-around`, `.justify-content-md-around`, `.justify-content-lg-around`, `.justify-content-xl-around` * `.justify-content-sm-evenly`, `.justify-content-md-evenly`, `.justify-content-lg-evenly`, `.justify-content-xl-evenly` #### 4.5.2.4 项目对齐 (Align Items) `align-items` 属性定义了项目在 **交叉轴** 上的对齐方式。Bootstrap 4 提供了以下工具类: * **`.align-items-start`**: 项目在交叉轴的起始位置对齐。 * **`.align-items-end`**: 项目在交叉轴的结束位置对齐。 * **`.align-items-center`**: 项目在交叉轴的居中位置对齐。 * **`.align-items-baseline`**: 项目基线对齐。 * **`.align-items-stretch`**: 项目在交叉轴上拉伸以填充容器 (默认值)。 以下 Mermaid 图表展示了 `align-items` 的不同对齐方式 (假设交叉轴为垂直方向): ```mermaid graph TD subgraph align-items-start direction="交叉轴起点" --> a[Item 1] direction --> b[Item 2] direction --> c[Item 3] end subgraph align-items-end a1[Item 1] --> direction1="交叉轴终点" b1[Item 2] --> direction1 c1[Item 3] --> direction1 end subgraph align-items-center direction2="交叉轴起点" -- 垂直居中 --> direction3="交叉轴终点" direction2 --> a2[Item 1] direction2 --> b2[Item 2] direction2 --> c2[Item 3] end subgraph align-items-baseline direction4="基线" --> a3[Item 1] direction4 --> b3[Item 2] direction4 --> c3[Item 3] end subgraph align-items-stretch direction5="交叉轴起点" --> a4[Item 1] direction5 --> b4[Item 2] direction5 --> c4[Item 3] a4 --> direction6="交叉轴终点" b4 --> direction6 c4 --> direction6 end style align-items-start fill:#f0f0f0,stroke:#333,stroke-width:1px style align-items-end fill:#f0f0f0,stroke:#333,stroke-width:1px style align-items-center fill:#f0f0f0,stroke:#333,stroke-width:1px style align-items-baseline fill:#f0f0f0,stroke:#333,stroke-width:1px style align-items-stretch fill:#f0f0f0,stroke:#333,stroke-width:1px ``` **代码实践:** ```html
``` **内容详解:** * 这些工具类对应 CSS 的 `align-items` 属性,用于控制 Flex 项目在交叉轴上的对齐方式。 * 需要注意的是,要使 `align-items` 生效,Flex 容器在交叉轴方向上必须有足够的空间 (例如,设置了 `height` 或容器内容的高度超过了项目的高度)。 * `.align-items-stretch` 是默认值,项目会拉伸以填充容器的高度。如果希望项目高度自适应内容,可以考虑使用其他对齐方式,例如 `.align-items-start` 或 `.align-items-center`。 **响应式变体:** `align-items` 工具类也提供了响应式变体: * `.align-items-sm-start`, `.align-items-md-start`, `.align-items-lg-start`, `.align-items-xl-start` * `.align-items-sm-end`, `.align-items-md-end`, `.align-items-lg-end`, `.align-items-xl-end` * `.align-items-sm-center`, `.align-items-md-center`, `.align-items-lg-center`, `.align-items-xl-center` * `.align-items-sm-baseline`, `.align-items-md-baseline`, `.align-items-lg-baseline`, `.align-items-xl-baseline` * `.align-items-sm-stretch`, `.align-items-md-stretch`, `.align-items-lg-stretch`, `.align-items-xl-stretch` #### 4.5.2.5 单个项目对齐 (Align Self) `align-self` 属性允许我们覆盖容器的 `align-items` 设置,单独定义 **某个 Flex 项目** 在交叉轴上的对齐方式。Bootstrap 4 提供了以下工具类: * **`.align-self-auto`**: 项目继承容器的 `align-items` 属性 (默认值)。 * **`.align-self-start`**: 项目在交叉轴的起始位置对齐。 * **`.align-self-end`**: 项目在交叉轴的结束位置对齐。 * **`.align-self-center`**: 项目在交叉轴的居中位置对齐。 * **`.align-self-baseline`**: 项目基线对齐。 * **`.align-self-stretch`**: 项目在交叉轴上拉伸以填充容器。 **代码实践:** ```html
Item 1
Align Self Start Item 2
Item 3
Item 1
Align Self End Item 2
Item 3
Item 1
Align Self Center Item 2
Item 3
Item 1
Align Self Baseline Item 2
Item 3
Item 1
Align Self Stretch Item 2
Item 3
``` **内容详解:** * 这些工具类对应 CSS 的 `align-self` 属性,用于单独控制某个 Flex 项目在交叉轴上的对齐方式,优先级高于容器的 `align-items` 设置。 * `align-self` 属性非常灵活,可以用于调整特定项目在布局中的位置,实现更精细的控制。 **响应式变体:** `align-self` 工具类同样提供了响应式变体: * `.align-self-sm-auto`, `.align-self-md-auto`, `.align-self-lg-auto`, `.align-self-xl-auto` * `.align-self-sm-start`, `.align-self-md-start`, `.align-self-lg-start`, `.align-self-xl-start` * `.align-self-sm-end`, `.align-self-md-end`, `.align-self-lg-end`, `.align-self-xl-end` * `.align-self-sm-center`, `.align-self-md-center`, `.align-self-lg-center`, `.align-self-xl-center` * `.align-self-sm-baseline`, `.align-self-md-baseline`, `.align-self-lg-baseline`, `.align-self-xl-baseline` * `.align-self-sm-stretch`, `.align-self-md-stretch`, `.align-self-lg-stretch`, `.align-self-xl-stretch` #### 4.5.2.6 填充 (Fill) * **`.flex-fill`**: 强制 Flex 项目在可用空间内均匀填充,同时忽略 `flex-grow`。 **代码实践:** ```html
Flex item with fill
Flex item with fill
Flex item with fill
``` **内容详解:** * `.flex-fill` 工具类相当于设置了 `flex: 1 1 auto;`,使 Flex 项目在可用空间内均匀分布,并且宽度相等。 * 适用于创建等宽的导航菜单、按钮组等场景。 **响应式变体:** `.flex-fill` 工具类也提供了响应式变体: * `.flex-sm-fill`, `.flex-md-fill`, `.flex-lg-fill`, `.flex-xl-fill` #### 4.5.2.7 增长和收缩 (Grow and Shrink) * **`.flex-grow-0`**: 阻止 Flex 项目增长,相当于 `flex-grow: 0;`。 * **`.flex-grow-1`**: 允许 Flex 项目增长以占据可用空间,相当于 `flex-grow: 1;` (默认值)。 * **`.flex-shrink-0`**: 阻止 Flex 项目收缩,相当于 `flex-shrink: 0;`。 * **`.flex-shrink-1`**: 允许 Flex 项目在空间不足时收缩,相当于 `flex-shrink: 1;` (默认值)。 **代码实践:** ```html
Flex grow 1
Default Flex
Default Flex
Fixed Width
Flex shrink 0, No Shrink
Flex grow 1
``` **内容详解:** * `.flex-grow-*` 和 `.flex-shrink-*` 工具类用于控制 Flex 项目在主轴上的尺寸伸缩行为。 * `.flex-grow-1` 常用于使某个项目占据剩余空间,例如在侧边栏布局中,使内容区域占据剩余宽度。 * `.flex-shrink-0` 常用于阻止某些项目在空间不足时被压缩,例如导航栏的品牌 Logo 或固定宽度的按钮。 **响应式变体:** `.flex-grow-*` 和 `.flex-shrink-*` 工具类也提供了响应式变体: * `.flex-sm-grow-0`, `.flex-md-grow-0`, `.flex-lg-grow-0`, `.flex-xl-grow-0`