蓝色天空网mbsky.com · 知识 · 实践 · 创造Bootstrap5 · 本地阅读预览
教程 / 前端开发 / Bootstrap5

Bootstrap5 Flex

系统梳理 Bootstrap 5 弹性盒子(Flexbox)布局工具类,涵盖容器显示、主轴方向、内容对齐、等宽扩展收缩、排序、自动外边距、多行包裹、交叉轴对齐与响应式断点类,并附本地无头 Chromium 实测布局数据。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

弹性容器与主轴方向

Bootstrap 5 全面采用 CSS3 弹性盒子(Flexbox)构建网格与页面布局,彻底取代了 Bootstrap 3 时代的浮动方案,更利于一维对齐与响应式自适应。需要注意的是,IE9 及更早版本不支持 Flexbox,且 Bootstrap 5 官方已停止兼容旧版 IE;如需系统掌握底层模型,可对照 CSS 弹性盒规范学习。

通过 `.d-flex` 可将元素声明为块级弹性容器(独占整行宽度),而 `.d-inline-flex` 则创建行内级弹性容器(宽度默认收缩包裹子项)。本站离线无头 Chromium 在 600px 外框内实测三个 100px 子项:`.d-flex` 容器计算属性 `display: flex`、宽度 `600px`,`.d-inline-flex` 计算属性 `display: inline-flex`、宽度收缩为 `300px`。

主轴方向由 `.flex-row`(水平从左到右,默认值)、`.flex-row-reverse`(水平反向排列,子项贴右端起始)、`.flex-column`(垂直自上而下)与 `.flex-column-reverse`(垂直自下而上翻转)控制。Chromium 实测边界坐标确认:`.flex-row` 下子项 1 位于子项 3 左侧,`.flex-row-reverse` 反转至右侧;`.flex-column` 下子项 1 位于子项 3 上方,`.flex-column-reverse` 则翻至下方。

主轴排列、等宽、扩展与收缩

`.justify-content-*` 控制弹性子项沿主轴的分布方式,常用取值包括 `start`(默认起点对齐)、`end`(终点对齐)、`center`(居中)、`between`(两端对齐、项间等距)与 `around`(每项两侧均分留白)。在 600px 容器中放置 100px 子项实测:`.justify-content-end` 起点横坐标为 `500px`,`.justify-content-center` 为 `250px`,两个子项在 `.justify-content-between` 下间距 `400px`,在 `.justify-content-around` 下首项左距 `100px`。

尺寸分配类包括 `.flex-fill`(`flex: 1 1 auto`,在内容尺寸基础上均分剩余空间,等长内容下三项在 600px 容器中各得 `200px`)、`.flex-grow-1`(吸收全部剩余主轴空间,前两项各占 `100px` 时第三项实测扩展至 `400px`)以及收缩控制 `.flex-shrink-0` / `.flex-shrink-1`。在 200px 窄容器中放入两个预设 `150px` 的子项,实测 `.flex-shrink-0` 保持 `150px` 不压缩,`.flex-shrink-1` 压缩至 `50px`。

排序与自动外边距

`.order-*` 通过修改 CSS `order` 整数值重排子项视觉顺序,数值越小越靠前(例如 `.order-1` 排在 `.order-2` 之前;Bootstrap 5 默认工具类内置 `.order-0` 至 `.order-5` 及 `.order-first`/`.order-last`,若需 1–12 档可在 Sass `$utilities` 中扩展)。Chromium 对依次声明 `order-3`、`order-2`、`order-1` 的三项实测横坐标分别为 `200px`、`100px`、`0px`,视觉顺序完全按数值升序呈现。

弹性布局中自动外边距可实现局部推挤:Bootstrap 5 采用逻辑方向命名,在默认从左到右(LTR)书写模式下,`.ms-auto` 对应 `margin-left: auto !important`(将自身及后续兄弟项整体推向右侧),`.me-auto` 对应 `margin-right: auto !important`(把紧随其后的兄弟项推向右侧)。实测 600px 容器内两个 100px 子项:首项加 `.ms-auto` 后两项横坐标为 `400px` 与 `500px`;首项加 `.me-auto` 后两项横坐标分别为 `0px` 与 `500px`。

包裹、交叉轴对齐与响应式断点类

`.flex-nowrap`(默认单行不换行)、`.flex-wrap`(空间不足时顺向换行)与 `.flex-wrap-reverse`(反向换行)控制是否折行。多行容器中,`.align-content-*`(支持 `start`、`end`、`center`、`between`、`around`、`stretch`)决定各行整体在交叉轴上的堆叠方式,但对单行弹性容器无效。250px×160px 容器内放置三个 100px×40px 子项实测:`.flex-wrap.align-content-center` 将两行居中至距顶 `40px` 处,`.flex-wrap-reverse` 使折行的第 3 项位于第 1 项上方。

单行子项的交叉轴对齐由容器类 `.align-items-*`(有效取值为 `start`、`end`、`center`、`baseline` 与默认 `stretch`,注意 CSS 规范中 `align-items` 并无 `around` 值)控制,单个子项可用 `.align-self-*` 局部覆盖。在 150px 高的 `.align-items-center` 容器中实测 40px 高子项:默认项纵坐标偏移 `55px`,`.align-self-start` 贴顶 `0px`,`.align-self-end` 贴底 `110px`,未限高的 `.align-self-stretch` 拉伸至 `150px`。

所有弹性工具类均支持响应式断点变体(将 `*` 替换为 `sm`、`md`、`lg`、`xl` 或 Bootstrap 5 新增的 `xxl`,采用移动优先 `min-width` 媒体查询生效)。本站实测同时带 `.d-md-flex.flex-column.flex-md-row.justify-content-md-between.align-items-md-center` 的容器:在 `500px` 视口下保持 `display: block` 与 `flex-direction: column`,切换至 `900px` 视口后立即变为水平 `flex` 行排布,`.order-md-1` 子项移至左侧且 `.align-self-md-end` 贴底(距顶 `80px` 对比居中项 `40px`)。完整响应式类目见下表。

响应式 Flex 工具类速查表(* 取 sm / md / lg / xl / xxl)

类名 / 分组作用说明对应 CSS 属性与断点生效规则
【分组】弹性容器按断点切换容器的 display 弹性上下文min-width 断点起生效
`.d-*-flex`在指定断点及以上创建块级弹性容器`display: flex !important`
`.d-*-inline-flex`在指定断点及以上创建行内级弹性容器`display: inline-flex !important`
【分组】主轴方向按断点控制子项水平或垂直排布修改 `flex-direction`
`.flex-*-row`指定断点起子项水平正向排列`flex-direction: row !important`
`.flex-*-row-reverse`指定断点起子项水平反向排列(贴终点)`flex-direction: row-reverse !important`
`.flex-*-column`指定断点起子项垂直正向堆叠`flex-direction: column !important`
`.flex-*-column-reverse`指定断点起子项垂直反向堆叠`flex-direction: column-reverse !important`
【分组】主轴内容对齐按断点调整子项在主轴上的分布修改 `justify-content`
`.justify-content-*-start`指定断点起沿主轴起点对齐(LTR 靠左)`justify-content: flex-start !important`
`.justify-content-*-end`指定断点起沿主轴终点对齐(LTR 靠右)`justify-content: flex-end !important`
`.justify-content-*-center`指定断点起沿主轴居中对齐`justify-content: center !important`
`.justify-content-*-between`指定断点起两端对齐、中间等距分布`justify-content: space-between !important`
`.justify-content-*-around`指定断点起为每个子项两侧均分留白`justify-content: space-around !important`
【分组】弹性等宽填充按断点令子项共同填充主轴剩余空间修改 `flex` 简写
`.flex-*-fill`指定断点起让子项按内容基准共同平分容器剩余宽度`flex: 1 1 auto !important`
【分组】弹性扩展按断点启用或关闭子项伸展权重修改 `flex-grow`
`.flex-*-grow-0`指定断点起禁止子项占用剩余空间`flex-grow: 0 !important`
`.flex-*-grow-1`指定断点起允许子项按比例扩展填充剩余空间`flex-grow: 1 !important`
【分组】弹性收缩按断点启用或禁止子项空间不足时压缩修改 `flex-shrink`
`.flex-*-shrink-0`指定断点起禁止子项在容器变窄时收缩`flex-shrink: 0 !important`
`.flex-*-shrink-1`指定断点起允许子项在容器变窄时按需收缩`flex-shrink: 1 !important`
【分组】弹性包裹按断点控制单行不折行或多行折行修改 `flex-wrap`
`.flex-*-nowrap`指定断点起强制单行不换行`flex-wrap: nowrap !important`
`.flex-*-wrap`指定断点起允许子项顺向折行`flex-wrap: wrap !important`
`.flex-*-wrap-reverse`指定断点起允许子项沿交叉轴反方向折行`flex-wrap: wrap-reverse !important`
【分组】多行交叉轴排列按断点控制多行折行后的行间堆叠方式修改 `align-content`(单行无效)
`.align-content-*-start`指定断点起多行紧凑堆叠在交叉轴起点`align-content: flex-start !important`
`.align-content-*-end`指定断点起多行紧凑堆叠在交叉轴终点`align-content: flex-end !important`
`.align-content-*-center`指定断点起多行在交叉轴居中堆叠`align-content: center !important`
`.align-content-*-around`指定断点起各行沿交叉轴两侧均分留白`align-content: space-around !important`
`.align-content-*-stretch`指定断点起拉伸各行以填满交叉轴空间`align-content: stretch !important`
【分组】子项排序按断点调整视觉展示次序而不改动 DOM修改 `order` 整数
`.order-*-0` 至 `.order-*-5`(可扩展至 12)在指定断点上按数字升序重排子项视觉顺序`order: <n> !important`(默认内置 0–5)
【分组】单行子项交叉轴对齐按断点统一控制容器内单行子项的交叉轴位置修改 `align-items`
`.align-items-*-start`指定断点起子项在交叉轴起点对齐`align-items: flex-start !important`
`.align-items-*-end`指定断点起子项在交叉轴终点对齐`align-items: flex-end !important`
`.align-items-*-center`指定断点起子项在交叉轴居中对齐`align-items: center !important`
`.align-items-*-baseline`指定断点起子项按首行文本基线对齐`align-items: baseline !important`
`.align-items-*-stretch`指定断点起拉伸未限高子项填满交叉轴`align-items: stretch !important`
【分组】单个子项交叉轴覆盖对齐按断点为某个子项单独覆写容器的 `align-items`修改 `align-self`
`.align-self-*-start`指定断点起仅该子项贴合交叉轴起点`align-self: flex-start !important`
`.align-self-*-end`指定断点起仅该子项贴合交叉轴终点`align-self: flex-end !important`
`.align-self-*-center`指定断点起仅该子项在交叉轴居中`align-self: center !important`
`.align-self-*-baseline`指定断点起仅该子项按文本基线对齐`align-self: baseline !important`
`.align-self-*-stretch`指定断点起仅该子项拉伸填满交叉轴`align-self: stretch !important`

运行 Bootstrap 5 Flex 容器、主轴/交叉轴对齐与响应式断点实验

python · 阅读源码
"""Offline Playwright Chromium verification for Bootstrap 5 Flex utilities."""
import json
from playwright.sync_api import sync_playwright

CSS = """
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; font-size: 16px; }
.d-flex { display: flex !important; }
.d-inline-flex { display: inline-flex !important; }
.flex-row { flex-direction: row !important; }
.flex-row-reverse { flex-direction: row-reverse !important; }
.flex-column { flex-direction: column !important; }
.flex-column-reverse { flex-direction: column-reverse !important; }
.justify-content-start { justify-content: flex-start !important; }
.justify-content-end { justify-content: flex-end !important; }
.justify-content-center { justify-content: center !important; }
.justify-content-between { justify-content: space-between !important; }
.justify-content-around { justify-content: space-around !important; }
.flex-fill { flex: 1 1 auto !important; }
.flex-grow-0 { flex-grow: 0 !important; }
.flex-grow-1 { flex-grow: 1 !important; }
.flex-shrink-0 { flex-shrink: 0 !important; }
.flex-shrink-1 { flex-shrink: 1 !important; }
.order-0 { order: 0 !important; }
.order-1 { order: 1 !important; }
.order-2 { order: 2 !important; }
.order-3 { order: 3 !important; }
.ms-auto { margin-left: auto !important; }
.me-auto { margin-right: auto !important; }
.flex-nowrap { flex-wrap: nowrap !important; }
.flex-wrap { flex-wrap: wrap !important; }
.flex-wrap-reverse { flex-wrap: wrap-reverse !important; }
.align-content-start { align-content: flex-start !important; }
.align-content-end { align-content: flex-end !important; }
.align-content-center { align-content: center !important; }
.align-content-between { align-content: space-between !important; }
.align-content-around { align-content: space-around !important; }
.align-content-stretch { align-content: stretch !important; }
.align-items-start { align-items: flex-start !important; }
.align-items-end { align-items: flex-end !important; }
.align-items-center { align-items: center !important; }
.align-items-baseline { align-items: baseline !important; }
.align-items-stretch { align-items: stretch !important; }
.align-self-start { align-self: flex-start !important; }
.align-self-end { align-self: flex-end !important; }
.align-self-center { align-self: center !important; }
.align-self-baseline { align-self: baseline !important; }
.align-self-stretch { align-self: stretch !important; }
.p-2 { padding: 8px !important; }
.p-3 { padding: 16px !important; }
.box { width: 600px; }
.item-fixed { width: 100px; height: 40px; }
@media (min-width: 768px) {
  .d-md-flex { display: flex !important; }
  .flex-md-row { flex-direction: row !important; }
  .justify-content-md-between { justify-content: space-between !important; }
  .order-md-1 { order: 1 !important; }
  .order-md-2 { order: 2 !important; }
  .align-items-md-center { align-items: center !important; }
  .align-self-md-end { align-self: flex-end !important; }
}
"""


def launch_browser(p):
    try:
        return p.chromium.launch(headless=True)
    except Exception:
        return p.chromium.launch(headless=True, channel="chrome")


def main():
    cases = []
    with sync_playwright() as p:
        browser = launch_browser(p)
        page = browser.new_page(viewport={"width": 900, "height": 800})

        # Case 1: d-flex, d-inline-flex, flex-row, flex-row-reverse, flex-column, flex-column-reverse
        page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div class="box">
          <div id="dflex" class="d-flex"><div class="item-fixed">1</div><div class="item-fixed">2</div><div class="item-fixed">3</div></div>
          <div id="diflex" class="d-inline-flex"><div class="item-fixed">1</div><div class="item-fixed">2</div><div class="item-fixed">3</div></div>
          <div id="frow" class="d-flex flex-row"><div id="r1" class="item-fixed">1</div><div id="r3" class="item-fixed">3</div></div>
          <div id="frowrev" class="d-flex flex-row-reverse"><div id="rr1" class="item-fixed">1</div><div id="rr3" class="item-fixed">3</div></div>
          <div id="fcol" class="d-flex flex-column"><div id="c1" class="item-fixed">1</div><div id="c3" class="item-fixed">3</div></div>
          <div id="fcolrev" class="d-flex flex-column-reverse"><div id="cr1" class="item-fixed">1</div><div id="cr3" class="item-fixed">3</div></div>
        </div></body></html>""")
        act1 = page.evaluate("""() => {
          const cs = id => getComputedStyle(document.getElementById(id));
          const rc = id => document.getElementById(id).getBoundingClientRect();
          return {
            dflex_display: cs('dflex').display,
            dflex_width: Math.round(rc('dflex').width),
            diflex_display: cs('diflex').display,
            diflex_width: Math.round(rc('diflex').width),
            row_r1_left_of_r3: rc('r1').left < rc('r3').left,
            row_rev_rr1_right_of_rr3: rc('rr1').left > rc('rr3').left,
            col_c1_above_c3: rc('c1').top < rc('c3').top,
            col_rev_cr1_below_cr3: rc('cr1').top > rc('cr3').top,
          };
        }""")
        exp1 = {
            "dflex_display": "flex",
            "dflex_width": 600,
            "diflex_display": "inline-flex",
            "diflex_width": 300,
            "row_r1_left_of_r3": True,
            "row_rev_rr1_right_of_rr3": True,
            "col_c1_above_c3": True,
            "col_rev_cr1_below_cr3": True,
        }
        cases.append({"name": "flex_container_and_direction", "expected": exp1, "actual": act1, "passed": act1 == exp1})

        # Case 2: justify-content, flex-fill, flex-grow-1, flex-shrink-0/1
        page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div id="jc_end" class="box d-flex justify-content-end"><div id="je1" class="item-fixed">1</div></div>
        <div id="jc_center" class="box d-flex justify-content-center"><div id="jct1" class="item-fixed">1</div></div>
        <div id="jc_between" class="box d-flex justify-content-between"><div id="jb1" class="item-fixed">1</div><div id="jb2" class="item-fixed">2</div></div>
        <div id="jc_around" class="box d-flex justify-content-around"><div id="ja1" class="item-fixed">1</div><div id="ja2" class="item-fixed">2</div></div>
        <div id="fill_box" class="box d-flex"><div id="f1" class="flex-fill">X</div><div id="f2" class="flex-fill">X</div><div id="f3" class="flex-fill">X</div></div>
        <div id="grow_box" class="box d-flex"><div id="g1" class="item-fixed">1</div><div id="g2" class="item-fixed">2</div><div id="g3" class="flex-grow-1">3</div></div>
        <div id="shrink_box" style="width:200px" class="d-flex"><div id="s0" style="width:150px" class="flex-shrink-0">NoShrink</div><div id="s1" style="width:150px" class="flex-shrink-1">Shrink</div></div>
        </body></html>""")
        act2 = page.evaluate("""() => {
          const rc = id => document.getElementById(id).getBoundingClientRect();
          return {
            end_x: Math.round(rc('je1').left),
            center_x: Math.round(rc('jct1').left),
            between_gap: Math.round(rc('jb2').left - rc('jb1').right),
            around_left_margin: Math.round(rc('ja1').left),
            fill_widths: [Math.round(rc('f1').width), Math.round(rc('f2').width), Math.round(rc('f3').width)],
            grow_g3_width: Math.round(rc('g3').width),
            shrink_widths: [Math.round(rc('s0').width), Math.round(rc('s1').width)],
          };
        }""")
        exp2 = {
            "end_x": 500,
            "center_x": 250,
            "between_gap": 400,
            "around_left_margin": 100,
            "fill_widths": [200, 200, 200],
            "grow_g3_width": 400,
            "shrink_widths": [150, 50],
        }
        cases.append({"name": "flex_justify_fill_grow_shrink", "expected": exp2, "actual": act2, "passed": act2 == exp2})

        # Case 3: order and ms-auto / me-auto
        page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div id="ord_box" class="box d-flex">
          <div id="o1" class="item-fixed order-3">Item 1</div>
          <div id="o2" class="item-fixed order-2">Item 2</div>
          <div id="o3" class="item-fixed order-1">Item 3</div>
        </div>
        <div id="ms_box" class="box d-flex">
          <div id="ms1" class="item-fixed ms-auto">Item 1</div>
          <div id="ms2" class="item-fixed">Item 2</div>
        </div>
        <div id="me_box" class="box d-flex">
          <div id="me1" class="item-fixed me-auto">Item 1</div>
          <div id="me2" class="item-fixed">Item 2</div>
        </div>
        </body></html>""")
        act3 = page.evaluate("""() => {
          const rc = id => document.getElementById(id).getBoundingClientRect();
          return {
            order_x: [Math.round(rc('o3').left), Math.round(rc('o2').left), Math.round(rc('o1').left)],
            ms_auto_x: [Math.round(rc('ms1').left), Math.round(rc('ms2').left)],
            me_auto_x: [Math.round(rc('me1').left), Math.round(rc('me2').left)],
          };
        }""")
        exp3 = {
            "order_x": [0, 100, 200],
            "ms_auto_x": [400, 500],
            "me_auto_x": [0, 500],
        }
        cases.append({"name": "flex_order_and_auto_margins", "expected": exp3, "actual": act3, "passed": act3 == exp3})

        # Case 4: flex-wrap, align-content, align-items, align-self
        page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div id="wrap_box" style="width:250px;height:160px" class="d-flex flex-wrap align-content-center">
          <div id="w1" class="item-fixed">1</div><div id="w2" class="item-fixed">2</div><div id="w3" class="item-fixed">3</div>
        </div>
        <div id="wrev_box" style="width:250px;height:160px" class="d-flex flex-wrap-reverse">
          <div id="wr1" class="item-fixed">1</div><div id="wr2" class="item-fixed">2</div><div id="wr3" class="item-fixed">3</div>
        </div>
        <div id="ai_box" style="width:400px;height:150px" class="d-flex align-items-center">
          <div id="ai1" class="item-fixed">1</div>
          <div id="ai2" class="item-fixed align-self-start">2</div>
          <div id="ai3" class="item-fixed align-self-end">3</div>
          <div id="ai4" style="width:100px" class="align-self-stretch">4</div>
        </div>
        </body></html>""")
        act4 = page.evaluate("""() => {
          const rc = id => document.getElementById(id).getBoundingClientRect();
          const baseW = rc('wrap_box').top;
          const baseAI = rc('ai_box').top;
          return {
            wrap_second_row: rc('w3').top > rc('w1').top,
            wrap_center_offset_y: Math.round(rc('w1').top - baseW),
            wrap_reverse_w3_above_w1: rc('wr3').top < rc('wr1').top,
            ai_center_offset_y: Math.round(rc('ai1').top - baseAI),
            self_start_offset_y: Math.round(rc('ai2').top - baseAI),
            self_end_offset_y: Math.round(rc('ai3').top - baseAI),
            self_stretch_height: Math.round(rc('ai4').height),
          };
        }""")
        exp4 = {
            "wrap_second_row": True,
            "wrap_center_offset_y": 40,
            "wrap_reverse_w3_above_w1": True,
            "ai_center_offset_y": 55,
            "self_start_offset_y": 0,
            "self_end_offset_y": 110,
            "self_stretch_height": 150,
        }
        cases.append({"name": "flex_wrap_and_align", "expected": exp4, "actual": act4, "passed": act4 == exp4})

        # Case 5: Responsive md breakpoints (500px vs 900px)
        html_resp = f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div id="resp" style="height:120px" class="d-md-flex flex-column flex-md-row justify-content-md-between align-items-md-center">
          <div id="rm1" class="item-fixed order-md-2">First</div>
          <div id="rm2" class="item-fixed order-md-1 align-self-md-end">Second</div>
        </div></body></html>"""
        page.set_viewport_size({"width": 500, "height": 600})
        page.set_content(html_resp)
        narrow = page.evaluate("""() => {
          const cs = getComputedStyle(document.getElementById('resp'));
          return { display: cs.display, direction: cs.flexDirection };
        }""")
        page.set_viewport_size({"width": 900, "height": 600})
        wide = page.evaluate("""() => {
          const resp = document.getElementById('resp');
          const cs = getComputedStyle(resp);
          const base = resp.getBoundingClientRect().top;
          const r1 = document.getElementById('rm1').getBoundingClientRect();
          const r2 = document.getElementById('rm2').getBoundingClientRect();
          return {
            display: cs.display,
            direction: cs.flexDirection,
            justify: cs.justifyContent,
            alignItems: cs.alignItems,
            rm1_offset_y: Math.round(r1.top - base),
            rm2_offset_y: Math.round(r2.top - base),
            rm2_left_of_rm1: r2.left < r1.left,
          };
        }""")
        act5 = {"narrow": narrow, "wide": wide}
        exp5 = {
            "narrow": {"display": "block", "direction": "column"},
            "wide": {
                "display": "flex",
                "direction": "row",
                "justify": "space-between",
                "alignItems": "center",
                "rm1_offset_y": 40,
                "rm2_offset_y": 80,
                "rm2_left_of_rm1": True,
            },
        }
        cases.append({"name": "flex_responsive_breakpoints", "expected": exp5, "actual": act5, "passed": act5 == exp5})

        browser.close()
    print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 在离线无头 Chromium 中核对 .d-flex(600px)/.d-inline-flex(300px)、方向反转、justify-content 定位、.flex-fill(200px×3)/.flex-grow-1(400px)/.flex-shrink-0(150px)、.order-*、.ms-auto/.me-auto 坐标、折行对齐及 500px/900px 视口断点切换。
运行 Bootstrap 5 Flex 容器、主轴/交叉轴对齐与响应式断点实验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

当前源码 SHA256:307f1b1aca03cd62a30bbb5f4ca9549991636f02d8a9d7488f84ba0d5196680c

下载本站示例源码

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载Bootstrap5 Flex实验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

ZIP SHA256:400099da3fd43365811ac938b4b176079ca853bfe0915eef375952d9880eeab0

常见错误与边界

  • 实际端口、文档资源与应用配置都应和测试条件一致。

练习与解答

在 LTR 页面中,`.ms-auto` 与 `.me-auto` 各自对应哪一侧的 `margin: auto`?为何单行 Flex 容器设置 `.align-content-center` 看不到效果?

展开参考解答

LTR 模式下 `.ms-auto` 设置 `margin-left: auto`(将元素及后续项推向右侧),`.me-auto` 设置 `margin-right: auto`(将后续兄弟项推向右侧);`align-content` 仅在多行折行(`flex-wrap: wrap` 且实际产生多行)时分配行间交叉轴空间,单行对齐应改用 `.align-items-*`。

规范与官方依据