蓝色天空.知识 · 实践 · 创造
我的收藏
Bootstrap5 教程目录 38章
Bootstrap5 / LEARN BY DOING

Bootstrap5 列表组

核对 Bootstrap 5 列表组基本结构、激活/禁用态、可交互链接项、无外框与水平排列、8 种语义色及表单列表案例。

学完这一章,你能够

  • 核对 Bootstrap 5 列表组基本结构、激活/禁用态、可交互链接项、无外框与水平排列、8 种语义色及表单列表案例。

列表组基本结构与激活/禁用状态

列表组(List Group)是展示导航项、消息条目或设置选项的通用竖向/横向容器,核心由 `.list-group` 容器、`.list-group-item` 列表项、负责悬停/聚焦反馈的 `.list-group-item-action` 以及 `.list-group-item-primary` 等语义配色类构成。

基础无序列表在 `<ul>` 上添加 `.list-group`、在 `<li>` 上添加 `.list-group-item` 即可纵向堆叠;叠加 `.active` 高亮当前项,叠加 `.disabled` 禁用指定项。本站离线 Chromium 实测确认 `.list-group` 为 `flex-direction: column`,相邻项自动消除顶部重叠边框(`border-top-width: 0px`),`.active` 呈蓝底白字(`rgb(13, 110, 253)`),`.disabled` 为 `pointer-events: none` 且文字灰化为 `rgb(108, 117, 125)`。

链接列表项、去外框与水平列表组

需要整行可点击的链接或按钮列表时,将 `<ul>`/`<li>` 替换为 `<div>`/`<a>`(或 `<button>`),并添加 `.list-group-item-action` 获得悬停浅灰背景反馈。实测鼠标悬停在链接项上时背景由 `rgb(255, 255, 255)` 切换为 `rgb(248, 249, 250)`。

在 `.list-group` 上附加 `.list-group-flush` 可去除外围左右边框、圆角及末项底边框,适合无缝嵌入卡片内部。实测容器圆角为 `0px`、首项左边框为 `0px`、末项底边框为 `0px`。

附加 `.list-group-horizontal`(或响应式变体 `.list-group-horizontal-sm|md|lg`)可将主轴切换为 `flex-direction: row`,使所有列表项横向并排。实测 4 个列表项顶部坐标一致且依次向右排列。

语义配色列表项与综合案例

无论静态 `<li>` 还是可交互 `<a>` 列表项,均可通过 `.list-group-item-success`、`.list-group-item-secondary`、`.list-group-item-info`、`.list-group-item-warning`、`.list-group-item-danger`、`.list-group-item-primary`、`.list-group-item-dark`、`.list-group-item-light` 设置柔和语义背景与高对比度文字;链接项还可同时保留 `.list-group-item-action` 以在悬停时加深底色。实测 8 种语义背景色均准确匹配 Bootstrap 5 柔和色板。

实际业务中常将列表组与弹性布局、图标、时间戳或表单控件(复选框多选、单选框互斥)组合使用。本站离线实测交互式复选/单选列表组:勾选第二项复选框后两项可同时保持 `checked=true`,而点击第二个单选框时第一项自动变为 `false`、第二项变为 `true`。

在离线 Chromium 中验证列表组状态、action 悬停、flush/horizontal 布局、8 种语义色与表单选项

含完整源码 · 227 行
展开查看收起
python
"""Offline Playwright Chromium verification for Bootstrap 5 List Groups (Ch 17)."""
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; }
  .list-group {
    display: flex; flex-direction: column; padding-left: 0; margin: 0 0 12px 0;
    border-radius: 0.375rem; list-style: none; width: 420px;
  }
  .list-group-item {
    position: relative; display: block; padding: 0.5rem 1rem; color: #212529;
    text-decoration: none; background-color: #fff; border: 1px solid rgba(0, 0, 0, 0.125);
  }
  .list-group-item:first-child { border-top-left-radius: inherit; border-top-right-radius: inherit; }
  .list-group-item:last-child { border-bottom-right-radius: inherit; border-bottom-left-radius: inherit; }
  .list-group-item + .list-group-item { border-top-width: 0; }
  .list-group-item.active { z-index: 2; color: #fff; background-color: #0d6efd; border-color: #0d6efd; }
  .list-group-item.disabled { color: #6c757d; pointer-events: none; background-color: #fff; }

  .list-group-item-action { width: 100%; color: #495057; text-align: inherit; }
  .list-group-item-action:hover { z-index: 1; color: #495057; background-color: #f8f9fa; }

  .list-group-flush { border-radius: 0; }
  .list-group-flush > .list-group-item { border-width: 0 0 1px; }
  .list-group-flush > .list-group-item:last-child { border-bottom-width: 0; }

  .list-group-horizontal { flex-direction: row; width: auto; }
  .list-group-horizontal > .list-group-item + .list-group-item { border-top-width: 1px; border-left-width: 0; }

  .list-group-item-primary { color: #084298; background-color: #cfe2ff; }
  .list-group-item-secondary { color: #41464b; background-color: #e2e3e5; }
  .list-group-item-success { color: #0f5132; background-color: #d1e7dd; }
  .list-group-item-danger { color: #842029; background-color: #f8d7da; }
  .list-group-item-warning { color: #664d03; background-color: #fff3cd; }
  .list-group-item-info { color: #055160; background-color: #cff4fc; }
  .list-group-item-light { color: #636464; background-color: #fefefe; }
  .list-group-item-dark { color: #141619; background-color: #d3d3d4; }
  .d-flex { display: flex; }
  .gap-2 { gap: 0.5rem; }
</style>
</head>
<body>
<ul id="lg-basic" class="list-group">
  <li id="li-active" class="list-group-item active">激活列表项</li>
  <li id="li-disabled" class="list-group-item disabled">禁用项</li>
  <li id="li-third" class="list-group-item">第三项</li>
</ul>

<div id="lg-links" class="list-group">
  <a id="a-hover" href="#" class="list-group-item list-group-item-action">第一项</a>
  <a href="#" class="list-group-item list-group-item-action">第二项</a>
</div>

<ul id="lg-flush" class="list-group list-group-flush">
  <li id="fl-1" class="list-group-item">第一项</li>
  <li id="fl-2" class="list-group-item">第二项</li>
</ul>

<ul id="lg-horiz" class="list-group list-group-horizontal">
  <li id="hz-1" class="list-group-item">第一项</li>
  <li id="hz-2" class="list-group-item">第二项</li>
  <li id="hz-3" class="list-group-item">第三项</li>
  <li id="hz-4" class="list-group-item">第四项</li>
</ul>

<ul id="lg-colors" class="list-group">
  <li id="co-success" class="list-group-item list-group-item-success">成功列表项</li>
  <li id="co-secondary" class="list-group-item list-group-item-secondary">次要列表项</li>
  <li id="co-info" class="list-group-item list-group-item-info">信息列表项</li>
  <li id="co-warning" class="list-group-item list-group-item-warning">警告列表项</li>
  <li id="co-danger" class="list-group-item list-group-item-danger">危险列表项</li>
  <li id="co-primary" class="list-group-item list-group-item-primary">主要列表项</li>
  <li id="co-dark" class="list-group-item list-group-item-dark">深灰色列表项</li>
  <li id="co-light" class="list-group-item list-group-item-light">浅色列表项</li>
</ul>

<div id="lg-case" class="list-group">
  <label class="list-group-item d-flex gap-2">
    <input id="cb-1" type="checkbox" checked> <span>First checkbox</span>
  </label>
  <label class="list-group-item d-flex gap-2">
    <input id="cb-2" type="checkbox"> <span>Second checkbox</span>
  </label>
</div>
<div id="lg-radio" class="list-group">
  <label class="list-group-item d-flex gap-2">
    <input id="rd-1" name="lgRadio" type="radio" checked> <span>First radio</span>
  </label>
  <label class="list-group-item d-flex gap-2">
    <input id="rd-2" name="lgRadio" type="radio"> <span>Second radio</span>
  </label>
</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": 900, "height": 900})
        page.set_content(HTML, wait_until="load")

        c1_actual = page.evaluate("""() => {
            const g = getComputedStyle(document.getElementById("lg-basic"));
            const act = getComputedStyle(document.getElementById("li-active"));
            const dis = getComputedStyle(document.getElementById("li-disabled"));
            const r1 = document.getElementById("li-active").getBoundingClientRect();
            const r2 = document.getElementById("li-disabled").getBoundingClientRect();
            return {
                flex_direction: g.flexDirection,
                stacked_vertically: r2.top >= r1.bottom - 1,
                active_bg: act.backgroundColor,
                active_color: act.color,
                disabled_pointer_events: dis.pointerEvents,
                disabled_color: dis.color,
                second_border_top_width: dis.borderTopWidth,
            };
        }""")
        c1_expected = {
            "flex_direction": "column",
            "stacked_vertically": True,
            "active_bg": "rgb(13, 110, 253)",
            "active_color": "rgb(255, 255, 255)",
            "disabled_pointer_events": "none",
            "disabled_color": "rgb(108, 117, 125)",
            "second_border_top_width": "0px",
        }

        hover_before = page.evaluate('getComputedStyle(document.getElementById("a-hover")).backgroundColor')
        page.locator("#a-hover").hover()
        hover_after = page.evaluate('getComputedStyle(document.getElementById("a-hover")).backgroundColor')

        c2_actual = page.evaluate("""([hb, ha]) => {
            const fl = getComputedStyle(document.getElementById("lg-flush"));
            const fl1 = getComputedStyle(document.getElementById("fl-1"));
            const fl2 = getComputedStyle(document.getElementById("fl-2"));
            const hz = getComputedStyle(document.getElementById("lg-horiz"));
            const r1 = document.getElementById("hz-1").getBoundingClientRect();
            const r4 = document.getElementById("hz-4").getBoundingClientRect();
            return {
                action_hover_before: hb,
                action_hover_after: ha,
                flush_radius: fl.borderTopLeftRadius,
                flush_left_border: fl1.borderLeftWidth,
                flush_last_bottom_border: fl2.borderBottomWidth,
                horizontal_flex_dir: hz.flexDirection,
                horizontal_same_row: Math.round(r1.top) === Math.round(r4.top) && r4.left > r1.right,
            };
        }""", [hover_before, hover_after])
        c2_expected = {
            "action_hover_before": "rgb(255, 255, 255)",
            "action_hover_after": "rgb(248, 249, 250)",
            "flush_radius": "0px",
            "flush_left_border": "0px",
            "flush_last_bottom_border": "0px",
            "horizontal_flex_dir": "row",
            "horizontal_same_row": True,
        }

        c3_actual = page.evaluate("""() => {
            const bg = id => getComputedStyle(document.getElementById(id)).backgroundColor;
            return {
                success: bg("co-success"),
                secondary: bg("co-secondary"),
                info: bg("co-info"),
                warning: bg("co-warning"),
                danger: bg("co-danger"),
                primary: bg("co-primary"),
                dark: bg("co-dark"),
                light: bg("co-light"),
            };
        }""")
        c3_expected = {
            "success": "rgb(209, 231, 221)",
            "secondary": "rgb(226, 227, 229)",
            "info": "rgb(207, 244, 252)",
            "warning": "rgb(255, 243, 205)",
            "danger": "rgb(248, 215, 218)",
            "primary": "rgb(207, 226, 255)",
            "dark": "rgb(211, 211, 212)",
            "light": "rgb(254, 254, 254)",
        }

        page.click("#cb-2")
        page.click("#rd-2")
        c4_actual = page.evaluate("""() => ({
            cb1_checked: document.getElementById("cb-1").checked,
            cb2_checked: document.getElementById("cb-2").checked,
            rd1_checked: document.getElementById("rd-1").checked,
            rd2_checked: document.getElementById("rd-2").checked,
        })""")
        c4_expected = {
            "cb1_checked": True,
            "cb2_checked": True,
            "rd1_checked": False,
            "rd2_checked": True,
        }

        browser.close()

    cases = [
        dict(name="basic_active_disabled_items", description="核对 .list-group 纵向布局、边框合并及 .active/.disabled 项", expected=c1_expected, actual=c1_actual, passed=c1_actual == c1_expected),
        dict(name="action_links_flush_and_horizontal", description="核对 .list-group-item-action 悬停背景、.list-group-flush 去外框及 .list-group-horizontal 横向排列", expected=c2_expected, actual=c2_actual, passed=c2_actual == c2_expected),
        dict(name="contextual_item_colors", description="核对 8 种语义 .list-group-item-* 柔和背景色", expected=c3_expected, actual=c3_actual, passed=c3_actual == c3_expected),
        dict(name="interactive_rich_list_group_case", description="核对复选框多选与单选框互斥列表组交互状态", 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()
核对 column 堆叠与边框折叠、action 悬停 #f8f9fa、flush 零外框圆角、horizontal 横向同行、8 种语义色及复选/单选交互。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

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

下载本章实验包 (mbskycom202612a700.zip) ↓ 查看运行说明 ↗

本章附件与实例包

下载课程关联工程包:Bootstrap5

课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a700.zip

下载课程关联工程包:Bootstrap5

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a722.zip

常见错误与边界检查

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

技术规范与官方资料

练习与参考解答

查看参考解答

当每个列表项本身是可导航链接或可点击操作入口、需要整行命中热区与悬停/焦点态反馈时,应使用 `<div>` 包裹 `<a>` 或 `<button>` 并配合 `.list-group-item-action`。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习