Bootstrap4 分页
核对 Bootstrap 4 分页栏基础结构、当前页激活与禁用状态、三档尺寸、Flex 对齐工具类及面包屑导航的离线 Chromium 实测结果。
学完这一章,你能够
- 核对 Bootstrap 4 分页栏基础结构、当前页激活与禁用状态、三档尺寸、Flex 对齐工具类及面包屑导航的离线 Chromium 实测结果。
基础分页结构、当前页与禁用态
列表或文章数据量较大时常通过分页组件分段浏览。Bootstrap 4 分页由三层语义类名构成:在 `<ul>` 上添加 `.pagination`,每个 `<li>` 添加 `.page-item`,内部 `<a>` 链接添加 `.page-link`。本站离线 Chromium 实测 `ul.pagination` 采用 `display: flex` 且清除列表圆点(`list-style-type: none`),各项水平单行衔接并合并相邻边框。
在对应 `<li>` 上添加 `.active` 可高亮标识当前所在页,添加 `.disabled` 则标记不可点击的翻页项(如首页时的 Previous)。实测 `.page-item.active .page-link` 呈现主蓝背景 `rgb(0, 123, 255)` 与白色文字 `rgb(255, 255, 255)`;`.page-item.disabled .page-link` 文字变为柔和灰 `rgb(108, 117, 125)` 且应用 `pointer-events: none` 屏蔽鼠标点击。
分页尺寸与水平对齐
在 `ul.pagination` 上追加 `.pagination-lg` 或 `.pagination-sm` 可整体放大或缩小分页条目。实测三档 `.page-link` 的计算字号与行高渲染高度依次为:`.pagination-sm` `14px` / `31px`,默认 `.pagination` `16px` / `38px`,`.pagination-lg` `20px` / `56px`。
借助 Flexbox 对齐工具类可灵活控制分页条在容器内的位置:默认靠左对齐,追加 `.justify-content-center` 居中,追加 `.justify-content-end` 靠右。实测在 `800px` 宽容器内,默认分页左边缘紧贴 `0px`,居中分页中心点精确落在 `400px`,右对齐分页右边缘精确贴合 `800px`。
面包屑导航
用于展示页面层级路径的面包屑导航使用 `.breadcrumb` 作为列表容器、`.breadcrumb-item` 标记每一级路径,并在当前末级添加 `.active`。实测 `.breadcrumb` 为浅灰圆角弹性条(`background-color: rgb(233, 236, 239)`),首项前无分隔符(`::before` 为 `none`),从第二项起自动通过 `.breadcrumb-item + .breadcrumb-item::before` 插入 `"/"` 分隔符,`.active` 末级文字呈灰色 `rgb(108, 117, 125)`。
运行 Bootstrap 4 分页状态、尺寸、对齐与面包屑导航 Chromium 离线实验
"""Offline Playwright Chromium verification for Bootstrap 4 Pagination & Breadcrumb."""
import json
from playwright.sync_api import sync_playwright
CSS = """
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; font-size: 16px; line-height: 1.5; }
.wrap { width: 800px; }
.pagination { display: flex; padding-left: 0; list-style: none; border-radius: 0.25rem; margin: 12px 0; }
.page-link {
position: relative; display: block; padding: 0.5rem 0.75rem; margin-left: -1px;
line-height: 1.25; color: #007bff; background-color: #fff; border: 1px solid #dee2e6; text-decoration: none;
}
.page-item:first-child .page-link { margin-left: 0; border-top-left-radius: 0.25rem; border-bottom-left-radius: 0.25rem; }
.page-item:last-child .page-link { border-top-right-radius: 0.25rem; border-bottom-right-radius: 0.25rem; }
.page-item.active .page-link { z-index: 3; color: #fff; background-color: #007bff; border-color: #007bff; }
.page-item.disabled .page-link { color: #6c757d; pointer-events: none; cursor: auto; background-color: #fff; border-color: #dee2e6; }
.pagination-lg .page-link { padding: 0.75rem 1.5rem; font-size: 1.25rem; line-height: 1.5; }
.pagination-sm .page-link { padding: 0.25rem 0.5rem; font-size: 0.875rem; line-height: 1.5; }
.justify-content-center { justify-content: center !important; }
.justify-content-end { justify-content: flex-end !important; }
.breadcrumb {
display: flex; flex-wrap: wrap; padding: 0.75rem 1rem; margin-bottom: 1rem;
list-style: none; background-color: #e9ecef; border-radius: 0.25rem;
}
.breadcrumb-item + .breadcrumb-item { padding-left: 0.5rem; }
.breadcrumb-item + .breadcrumb-item::before {
display: inline-block; padding-right: 0.5rem; color: #6c757d; content: "/";
}
.breadcrumb-item.active { color: #6c757d; }
"""
HTML = f"""<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><style>{CSS}</style></head>
<body>
<div class="wrap">
<ul class="pagination" id="pg-main">
<li class="page-item disabled" id="li-dis"><a class="page-link" href="#">Previous</a></li>
<li class="page-item"><a class="page-link" href="#">1</a></li>
<li class="page-item active" id="li-act"><a 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 class="pagination pagination-sm" id="pg-sm"><li class="page-item"><a class="page-link" href="#">1</a></li></ul>
<ul class="pagination" id="pg-md"><li class="page-item"><a class="page-link" href="#">1</a></li></ul>
<ul class="pagination pagination-lg" id="pg-lg"><li class="page-item"><a class="page-link" href="#">1</a></li></ul>
<ul class="breadcrumb" id="bc">
<li class="breadcrumb-item" id="bc-1"><a href="#">Photos</a></li>
<li class="breadcrumb-item" id="bc-2"><a href="#">Summer 2017</a></li>
<li class="breadcrumb-item" id="bc-3"><a href="#">Italy</a></li>
<li class="breadcrumb-item active" id="bc-4">Rome</li>
</ul>
<ul class="pagination" id="align-left"><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 class="pagination justify-content-center" id="align-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 class="pagination justify-content-end" id="align-right"><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>
</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)
try:
page = browser.new_page(viewport={"width": 1000, "height": 800})
page.set_content(HTML, wait_until="load")
c1_actual = page.evaluate("""() => {
const ul = document.getElementById('pg-main');
const act = getComputedStyle(document.querySelector('#li-act .page-link'));
const dis = getComputedStyle(document.querySelector('#li-dis .page-link'));
const items = [...ul.querySelectorAll('.page-item')].map(el => el.getBoundingClientRect());
return {
display: getComputedStyle(ul).display,
list_style: getComputedStyle(ul).listStyleType,
single_row: items.every(r => Math.abs(r.top - items[0].top) < 1),
active_bg: act.backgroundColor,
active_color: act.color,
disabled_pointer_events: dis.pointerEvents,
disabled_color: dis.color
};
}""")
c1_exp = {
"display": "flex",
"list_style": "none",
"single_row": True,
"active_bg": "rgb(0, 123, 255)",
"active_color": "rgb(255, 255, 255)",
"disabled_pointer_events": "none",
"disabled_color": "rgb(108, 117, 125)",
}
c2_actual = page.evaluate("""() => {
const sm = document.querySelector('#pg-sm .page-link');
const md = document.querySelector('#pg-md .page-link');
const lg = document.querySelector('#pg-lg .page-link');
const hs = Math.round(sm.getBoundingClientRect().height);
const hm = Math.round(md.getBoundingClientRect().height);
const hl = Math.round(lg.getBoundingClientRect().height);
return {
font_sizes: [getComputedStyle(sm).fontSize, getComputedStyle(md).fontSize, getComputedStyle(lg).fontSize],
heights_px: [hs, hm, hl],
ordered: hs < hm && hm < hl
};
}""")
c2_exp = {
"font_sizes": ["14px", "16px", "20px"],
"heights_px": [31, 38, 56],
"ordered": True,
}
c3_actual = page.evaluate("""() => {
const bc = getComputedStyle(document.getElementById('bc'));
const sep1 = getComputedStyle(document.getElementById('bc-1'), '::before').content;
const sep2 = getComputedStyle(document.getElementById('bc-2'), '::before').content;
const act = getComputedStyle(document.getElementById('bc-4'));
return {
display: bc.display,
bg: bc.backgroundColor,
first_before: sep1,
second_before: sep2,
active_color: act.color
};
}""")
c3_exp = {
"display": "flex",
"bg": "rgb(233, 236, 239)",
"first_before": "none",
"second_before": '"/"',
"active_color": "rgb(108, 117, 125)",
}
c4_actual = page.evaluate("""() => {
const box = (id) => {
const items = [...document.querySelectorAll('#'+id+' .page-item')].map(e => e.getBoundingClientRect());
return { left: items[0].left, right: items[items.length - 1].right };
};
const l = box('align-left'), c = box('align-center'), r = box('align-right');
const centerMid = (c.left + c.right) / 2;
return {
left_aligned: Math.abs(l.left - 0) < 1,
center_aligned: Math.abs(centerMid - 400) < 1,
right_aligned: Math.abs(r.right - 800) < 1
};
}""")
c4_exp = {
"left_aligned": True,
"center_aligned": True,
"right_aligned": True,
}
cases = [
{"name": "basic_active_and_disabled_pagination", "input": ".pagination with .active and .disabled", "expected": c1_exp, "actual": c1_actual, "passed": c1_actual == c1_exp},
{"name": "pagination_sizing", "input": ".pagination-sm / default / .pagination-lg", "expected": c2_exp, "actual": c2_actual, "passed": c2_actual == c2_exp},
{"name": "breadcrumb_navigation", "input": ".breadcrumb with 4 .breadcrumb-item entries", "expected": c3_exp, "actual": c3_actual, "passed": c3_actual == c3_exp},
{"name": "pagination_alignment", "input": "default / .justify-content-center / .justify-content-end in 800px", "expected": c4_exp, "actual": c4_actual, "passed": c4_actual == c4_exp},
]
print(json.dumps({"cases": cases}, ensure_ascii=False))
finally:
browser.close()
if __name__ == "__main__":
main()
- 核对 .active 蓝底白字与 .disabled pointer-events:none、sm/默认/lg 高度 31/38/56px、800px 容器左/中/右对齐,以及面包屑 ::before 斜杠分隔符。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:e39be7de1da10dd2974eb7977a123f493c1b0c5247580b8d4ed1bf88b6c67d02
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:38033c5686b576e72dabce0ad2a70da1e82ce792e69ef10cda31c48128f3b11f
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
在 Bootstrap 4 分页中,`.active` 和 `.disabled` 应当加在 `<li>` 还是 `<a>` 上?面包屑各项之间的 `/` 是如何生成的?
展开参考解答
`.active` 和 `.disabled` 均加在 `.page-item`(`<li>`)上,由后代选择器作用于内部 `.page-link`;面包屑的 `/` 分隔符由 CSS 相邻兄弟伪元素 `.breadcrumb-item + .breadcrumb-item::before { content: "/"; }` 自动插入,因此首项前不会出现多余斜杠。