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

Bootstrap5 分页

核对 Bootstrap 5 分页列表结构、激活/禁用页码、三档尺寸、Flex 对齐及面包屑导航分隔符渲染。

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

学完这一章,你能够

分页基础结构与页码状态

面对海量列表数据时通常需要分页导航。Bootstrap 5 在 `<ul>` 上添加 `.pagination`(推荐外包 `<nav aria-label="Page navigation">`),在 `<li>` 上添加 `.page-item`,并在 `<a>` 上添加 `.page-link` 即可生成水平相连的页码条。本站离线 Chromium 实测 `.pagination` 为 `display: flex` 且去除列表圆点(`list-style-type: none`),相邻 `.page-link` 通过 `margin-left: -1px` 消除双线边框。

为当前所在页的 `.page-item` 添加 `.active`(推荐补充 `aria-current="page"`)即可高亮显示;实测 `.page-item.active .page-link` 背景为 `rgb(13, 110, 253)`、文字为白色。

为首尾不可用的上一页/下一页 `.page-item` 添加 `.disabled` 可禁用点击;实测对应 `.page-link` 计算样式为 `pointer-events: none`、文字呈灰态 `rgb(108, 117, 125)`。

分页尺寸与水平对齐

在 `.pagination` 上叠加 `.pagination-lg` 或 `.pagination-sm` 可统一缩放页码字号与内边距。实测三档字号/内边距分别为 `.pagination-lg`(`20px`、`12px 24px`)、默认(`16px`、`6px 12px`)和 `.pagination-sm`(`14px`、`4px 8px`),高度严格递减。

依托 Flexbox 主轴对齐工具类,分页条默认左对齐,叠加 `.justify-content-center` 居中,叠加 `.justify-content-end` 靠右。实测在 `600px` 容器内左对齐左偏移 `0px`、居中左右留白对称误差不超过 `1px`、右对齐右缘间隙 `0px`。

面包屑导航

面包屑(Breadcrumb)通过 `.breadcrumb` 与 `.breadcrumb-item` 展示当前页面在站点层级中的路径位置,末级当前页附加 `.active`。实测 `.breadcrumb` 为弹性水平列表,除首项外每个 `.breadcrumb-item` 均由 `::before` 伪元素自动插入 `"/"` 分隔符,且 `.breadcrumb-item.active` 显示为弱化灰色 `rgb(108, 117, 125)`。

在离线 Chromium 中验证分页激活/禁用态、三档尺寸、Flex 对齐及面包屑斜杠分隔符

python · 阅读源码
"""Offline Playwright Chromium verification for Bootstrap 5 Pagination & Breadcrumb (Ch 16)."""
import json
from playwright.sync_api import sync_playwright

HTML = """<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<style>
  *, *::before, *::after { box-sizing: border-box; }
  html { font-size: 16px; }
  body { margin: 16px; font-family: system-ui, -apple-system, "Segoe UI", sans-serif; line-height: 1.5; }
  .box { width: 600px; }
  .pagination {
    display: flex; padding-left: 0; list-style: none; margin: 12px 0;
  }
  .page-link {
    position: relative; display: block; padding: 0.375rem 0.75rem; font-size: 1rem;
    color: #0d6efd; text-decoration: none; background-color: #fff;
    border: 1px solid #dee2e6;
  }
  .page-item:not(:first-child) .page-link { margin-left: -1px; }
  .page-item:first-child .page-link { border-top-left-radius: 0.375rem; border-bottom-left-radius: 0.375rem; }
  .page-item:last-child .page-link { border-top-right-radius: 0.375rem; border-bottom-right-radius: 0.375rem; }
  .page-item.active .page-link { z-index: 3; color: #fff; background-color: #0d6efd; border-color: #0d6efd; }
  .page-item.disabled .page-link { color: #6c757d; pointer-events: none; background-color: #fff; border-color: #dee2e6; }
  .pagination-lg .page-link { padding: 0.75rem 1.5rem; font-size: 1.25rem; }
  .pagination-sm .page-link { padding: 0.25rem 0.5rem; font-size: 0.875rem; }
  .justify-content-center { justify-content: center !important; }
  .justify-content-end { justify-content: flex-end !important; }

  .breadcrumb {
    display: flex; flex-wrap: wrap; padding: 0; margin-bottom: 1rem; list-style: none;
  }
  .breadcrumb-item + .breadcrumb-item { padding-left: 0.5rem; }
  .breadcrumb-item + .breadcrumb-item::before {
    float: left; padding-right: 0.5rem; color: #6c757d; content: "/";
  }
  .breadcrumb-item.active { color: #6c757d; }
</style>
</head>
<body>
<div id="wrap" class="box">
  <ul id="pg-states" class="pagination">
    <li class="page-item disabled"><a id="pg-prev" class="page-link" href="#">Previous</a></li>
    <li class="page-item"><a id="pg-1" class="page-link" href="#">1</a></li>
    <li class="page-item active"><a id="pg-2" class="page-link" href="#">2</a></li>
    <li class="page-item"><a class="page-link" href="#">3</a></li>
    <li class="page-item"><a class="page-link" href="#">Next</a></li>
  </ul>

  <ul id="pg-lg" class="pagination pagination-lg">
    <li class="page-item"><a id="lg-link" class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
  </ul>
  <ul id="pg-sm" class="pagination pagination-sm">
    <li class="page-item"><a id="sm-link" class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
  </ul>

  <ul id="al-start" class="pagination">
    <li class="page-item"><a class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
  </ul>
  <ul id="al-center" class="pagination justify-content-center">
    <li class="page-item"><a class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
  </ul>
  <ul id="al-end" class="pagination justify-content-end">
    <li class="page-item"><a class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
  </ul>

  <ul id="bc" class="breadcrumb">
    <li id="bc-1" class="breadcrumb-item"><a href="#">Photos</a></li>
    <li id="bc-2" class="breadcrumb-item"><a href="#">Summer 2017</a></li>
    <li id="bc-3" class="breadcrumb-item"><a href="#">Italy</a></li>
    <li id="bc-4" class="breadcrumb-item active">Rome</li>
  </ul>
</div>
</body>
</html>
"""


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


def main():
    with sync_playwright() as p:
        browser = launch_browser(p)
        page = browser.new_page(viewport={"width": 800, "height": 600})
        page.set_content(HTML, wait_until="load")

        c1_actual = page.evaluate("""() => {
            const ul = getComputedStyle(document.getElementById("pg-states"));
            const prev = getComputedStyle(document.getElementById("pg-prev"));
            const p1 = getComputedStyle(document.getElementById("pg-1"));
            const p2 = getComputedStyle(document.getElementById("pg-2"));
            return {
                display: ul.display,
                list_style: ul.listStyleType,
                second_margin_left: p1.marginLeft,
                active_bg: p2.backgroundColor,
                active_color: p2.color,
                disabled_pointer_events: prev.pointerEvents,
                disabled_color: prev.color,
            };
        }""")
        c1_expected = {
            "display": "flex",
            "list_style": "none",
            "second_margin_left": "-1px",
            "active_bg": "rgb(13, 110, 253)",
            "active_color": "rgb(255, 255, 255)",
            "disabled_pointer_events": "none",
            "disabled_color": "rgb(108, 117, 125)",
        }

        c2_actual = page.evaluate("""() => {
            const m = id => {
                const el = document.getElementById(id);
                const s = getComputedStyle(el);
                return { fs: s.fontSize, pad: s.padding, h: Math.round(el.getBoundingClientRect().height) };
            };
            const lg = m("lg-link"), md = m("pg-1"), sm = m("sm-link");
            return {
                lg_fs: lg.fs, md_fs: md.fs, sm_fs: sm.fs,
                lg_pad: lg.pad, md_pad: md.pad, sm_pad: sm.pad,
                strict_height_order: lg.h > md.h && md.h > sm.h,
            };
        }""")
        c2_expected = {
            "lg_fs": "20px",
            "md_fs": "16px",
            "sm_fs": "14px",
            "lg_pad": "12px 24px",
            "md_pad": "6px 12px",
            "sm_pad": "4px 8px",
            "strict_height_order": True,
        }

        c3_actual = page.evaluate("""() => {
            const box = document.getElementById("wrap").getBoundingClientRect();
            const bounds = id => {
                const items = Array.from(document.querySelectorAll(`#${id} .page-item`));
                return {
                    left: Math.round(items[0].getBoundingClientRect().left - box.left),
                    rightGap: Math.round(box.right - items[items.length - 1].getBoundingClientRect().right),
                };
            };
            const s = bounds("al-start"), c = bounds("al-center"), e = bounds("al-end");
            return {
                start_left_offset: s.left,
                center_symmetric: Math.abs(c.left - c.rightGap) <= 1,
                end_right_gap: e.rightGap,
            };
        }""")
        c3_expected = {
            "start_left_offset": 0,
            "center_symmetric": True,
            "end_right_gap": 0,
        }

        c4_actual = page.evaluate("""() => {
            const bc = getComputedStyle(document.getElementById("bc"));
            const b1 = getComputedStyle(document.getElementById("bc-1"), "::before").content;
            const b2 = getComputedStyle(document.getElementById("bc-2"), "::before").content;
            const b4 = getComputedStyle(document.getElementById("bc-4"));
            return {
                display: bc.display,
                first_before: b1,
                second_before: b2,
                active_color: b4.color,
                item_count: document.querySelectorAll("#bc .breadcrumb-item").length,
            };
        }""")
        c4_expected = {
            "display": "flex",
            "first_before": "none",
            "second_before": '"/"',
            "active_color": "rgb(108, 117, 125)",
            "item_count": 4,
        }

        browser.close()

    cases = [
        dict(name="basic_active_disabled_pagination", description="核对 .pagination 弹性布局、-1px 边框重叠及 .active/.disabled 态", expected=c1_expected, actual=c1_actual, passed=c1_actual == c1_expected),
        dict(name="pagination_sizes", description="核对 .pagination-lg / 默认 / .pagination-sm 字号、内边距与高度梯度", expected=c2_expected, actual=c2_actual, passed=c2_actual == c2_expected),
        dict(name="pagination_alignment", description="核对左对齐、.justify-content-center 居中与 .justify-content-end 右对齐", expected=c3_expected, actual=c3_actual, passed=c3_actual == c3_expected),
        dict(name="breadcrumb_navigation", description="核对 .breadcrumb 弹性列表、::before 斜杠分隔符与 .active 当前层级样式", expected=c4_expected, actual=c4_actual, passed=c4_actual == c4_expected),
    ]
    print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 核对 flex 无圆点列表与 -1px 边框折叠、lg/默认/sm 尺寸梯度、600px 容器左中右对齐及面包屑 ::before 斜杠分隔符。
在离线 Chromium 中验证分页激活/禁用态、三档尺寸、Flex 对齐及面包屑斜杠分隔符

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

当前源码 SHA256:ec31d0a7f5a1d385b39c59f9bc6a2b1056bc2b62e0cef81b2829abe52f12a329

下载本站示例源码

本地运行

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

下载Bootstrap5 分页实验

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

ZIP SHA256:0cc56da051693632ce1c7f7cc1fedb0edff45a2194f5065d6accd8db17d32aa4

常见错误与边界

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

练习与解答

为什么 Bootstrap 5 的 `.active` 与 `.disabled` 类要添加在 `.page-item` 而不是 `.page-link` 上?

展开参考解答

Bootstrap 5 的状态选择器按 `.page-item.active .page-link` 与 `.page-item.disabled .page-link` 编写,将状态挂在 `<li>` 层级也能统一处理边框层级(`z-index`)和禁用态语义。

规范与官方依据