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

Bootstrap4 列表组

核对 Bootstrap 4 列表组的基础纵向布局、激活与禁用状态、可交互链接悬停反馈、去边框/水平变体及 8 种语义配色列表项的离线 Chromium 实测。

学完这一章,你能够

  • 核对 Bootstrap 4 列表组的基础纵向布局、激活与禁用状态、可交互链接悬停反馈、去边框/水平变体及 8 种语义配色列表项的离线 Chromium 实测。

基础列表组与激活/禁用状态

列表组用于清晰展示一组连续条目,最常见的结构是以无序列表 `<ul>` 添加 `.list-group` 类、每个 `<li>` 添加 `.list-group-item` 类。本站离线 Chromium 实测 `ul.list-group` 采用 `display: flex` 与 `flex-direction: column`,各条目纵向紧贴堆叠,相邻条目通过取消上边框避免双线重叠。

为条目追加 `.active` 可高亮当前选中项(实测背景 `rgb(0, 123, 255)`、白字 `rgb(255, 255, 255)`);追加 `.disabled` 可将其置为不可交互的禁用态(实测文字灰化为 `rgb(108, 117, 125)` 且 `pointer-events: none`)。

链接列表项、去边框与水平布局

若条目本身是可点击导航链接,可将外层 `<ul>` 换为 `<div class="list-group">`,内部 `<li>` 换为 `<a>`,并在 `.list-group-item` 后追加 `.list-group-item-action` 以获得悬停反馈。实测默认背景为纯白 `rgb(255, 255, 255)`,鼠标悬停后背景平滑切换为浅灰 `rgb(248, 249, 250)` 且无下划线(`text-decoration-line: none`)。

在 `.list-group` 上追加 `.list-group-flush` 可移除外框与圆角(常用于嵌入卡片内部),实测首项左右及顶边框宽度均为 `0px`、末项底边框为 `0px`、圆角归零 `0px`。追加 `.list-group-horizontal` 则将排布方向由纵向改为横向(`flex-direction: row`),实测 4 个条目全部同行从左到右单行排布。

语义配色列表项与链接组合

Bootstrap 4 提供 8 种语义色列表项类:`.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`。实测 8 类分别呈现柔和语义底色与高对比深色文字(如 `success` 为浅绿 `rgb(195, 230, 203)` 配深绿字 `rgb(21, 87, 36)`,`danger` 为浅红 `rgb(245, 198, 203)` 配深红字 `rgb(114, 28, 36)`)。将语义色类用于 `<a>` 链接并组合 `.list-group-item-action` 时,悬停还会自动加深底色(实测 `success` 链接悬停由 `rgb(195, 230, 203)` 加深至 `rgb(177, 223, 187)`)。

运行 Bootstrap 4 列表组状态、悬停交互、Flush/水平变体与语义色 Chromium 实验

含完整源码 · 190 行
展开查看收起
python
"""Offline Playwright Chromium verification for Bootstrap 4 List Groups."""
import json
from playwright.sync_api import sync_playwright

CSS = """
*, *::before, *::after { box-sizing: border-box; }
body { margin: 16px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; font-size: 16px; line-height: 1.5; }
.list-group { display: flex; flex-direction: column; padding-left: 0; margin: 0 0 12px; border-radius: 0.25rem; width: 480px; }
.list-group-item {
  position: relative; display: block; padding: 0.75rem 1.25rem;
  background-color: #fff; border: 1px solid rgba(0, 0, 0, .125); text-decoration: none; color: #212529;
}
.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: #007bff; border-color: #007bff; }
.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, .list-group-item-action:focus { z-index: 1; color: #495057; text-decoration: none; background-color: #f8f9fa; }
.list-group-flush { border-radius: 0; }
.list-group-flush > .list-group-item { border-width: 0 0 1px; border-radius: 0; }
.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:first-child { border-bottom-left-radius: 0.25rem; border-top-right-radius: 0; }
.list-group-horizontal > .list-group-item:last-child { border-top-right-radius: 0.25rem; border-bottom-left-radius: 0; }
.list-group-horizontal > .list-group-item + .list-group-item { border-top-width: 1px; border-left-width: 0; }
.list-group-item-primary { color: #004085; background-color: #b8daff; }
.list-group-item-secondary { color: #383d41; background-color: #d6d8db; }
.list-group-item-success { color: #155724; background-color: #c3e6cb; }
.list-group-item-success.list-group-item-action:hover { color: #155724; background-color: #b1dfbb; }
.list-group-item-info { color: #0c5460; background-color: #bee5eb; }
.list-group-item-warning { color: #856404; background-color: #ffeeba; }
.list-group-item-danger { color: #721c24; background-color: #f5c6cb; }
.list-group-item-light { color: #818182; background-color: #fdfdfe; }
.list-group-item-dark { color: #1b1e21; background-color: #c6c8ca; }
"""

HTML = f"""<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><style>{CSS}</style></head>
<body>
  <ul class="list-group" id="lg-basic">
    <li class="list-group-item active" id="lg-act">Active item</li>
    <li class="list-group-item disabled" id="lg-dis">Disabled item</li>
    <li class="list-group-item" id="lg-norm">Third item</li>
  </ul>

  <div class="list-group" id="lg-links">
    <a href="#" class="list-group-item list-group-item-action" id="lga-1">First item</a>
    <a href="#" class="list-group-item list-group-item-action" id="lga-2">Second item</a>
  </div>

  <ul class="list-group list-group-flush" id="lg-flush">
    <li class="list-group-item" id="fl-1">First</li>
    <li class="list-group-item" id="fl-2">Second</li>
    <li class="list-group-item" id="fl-3">Last</li>
  </ul>

  <ul class="list-group list-group-horizontal" id="lg-horiz">
    <li class="list-group-item">First</li>
    <li class="list-group-item">Second</li>
    <li class="list-group-item">Third</li>
    <li class="list-group-item">Fourth</li>
  </ul>

  <div class="list-group" id="lg-ctx">
    <a href="#" class="list-group-item list-group-item-action list-group-item-success" id="ctx-success">成功</a>
    <a href="#" class="list-group-item list-group-item-secondary" id="ctx-secondary">次要</a>
    <a href="#" class="list-group-item list-group-item-info" id="ctx-info">信息</a>
    <a href="#" class="list-group-item list-group-item-warning" id="ctx-warning">警告</a>
    <a href="#" class="list-group-item list-group-item-danger" id="ctx-danger">危险</a>
    <a href="#" class="list-group-item list-group-item-primary" id="ctx-primary">主要</a>
    <a href="#" class="list-group-item list-group-item-dark" id="ctx-dark">深灰</a>
    <a href="#" class="list-group-item list-group-item-light" id="ctx-light">浅色</a>
  </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": 900})
            page.set_content(HTML, wait_until="load")

            c1_actual = page.evaluate("""() => {
              const ul = document.getElementById('lg-basic');
              const act = getComputedStyle(document.getElementById('lg-act'));
              const dis = getComputedStyle(document.getElementById('lg-dis'));
              const rs = [...ul.children].map(el => el.getBoundingClientRect());
              return {
                display: getComputedStyle(ul).display,
                flex_direction: getComputedStyle(ul).flexDirection,
                stacked: rs[1].top > rs[0].top && rs[2].top > rs[1].top,
                active_bg: act.backgroundColor,
                active_color: act.color,
                disabled_pointer_events: dis.pointerEvents,
                disabled_color: dis.color
              };
            }""")
            c1_exp = {
                "display": "flex",
                "flex_direction": "column",
                "stacked": True,
                "active_bg": "rgb(0, 123, 255)",
                "active_color": "rgb(255, 255, 255)",
                "disabled_pointer_events": "none",
                "disabled_color": "rgb(108, 117, 125)",
            }

            before_hover = page.evaluate("() => getComputedStyle(document.getElementById('lga-1')).backgroundColor")
            page.hover("#lga-1")
            after_hover = page.evaluate("""() => {
              const st = getComputedStyle(document.getElementById('lga-1'));
              return { hover_bg: st.backgroundColor, text_decoration: st.textDecorationLine };
            }""")
            c2_actual = {"default_bg": before_hover, **after_hover}
            c2_exp = {
                "default_bg": "rgb(255, 255, 255)",
                "hover_bg": "rgb(248, 249, 250)",
                "text_decoration": "none",
            }

            c3_actual = page.evaluate("""() => {
              const f1 = getComputedStyle(document.getElementById('fl-1'));
              const f3 = getComputedStyle(document.getElementById('fl-3'));
              const hz = document.getElementById('lg-horiz');
              const hrs = [...hz.children].map(el => el.getBoundingClientRect());
              return {
                flush_side_borders: [f1.borderLeftWidth, f1.borderRightWidth, f1.borderTopWidth],
                flush_last_bottom: f3.borderBottomWidth,
                flush_radius: f1.borderTopLeftRadius,
                horiz_direction: getComputedStyle(hz).flexDirection,
                horiz_single_row: hrs.every((r, i) => Math.abs(r.top - hrs[0].top) < 1 && (i === 0 || r.left > hrs[i-1].left))
              };
            }""")
            c3_exp = {
                "flush_side_borders": ["0px", "0px", "0px"],
                "flush_last_bottom": "0px",
                "flush_radius": "0px",
                "horiz_direction": "row",
                "horiz_single_row": True,
            }

            ctx_before = page.evaluate("""() => {
              const keys = ['success','secondary','info','warning','danger','primary','dark','light'];
              const res = {};
              for (const k of keys) {
                const st = getComputedStyle(document.getElementById('ctx-'+k));
                res[k] = [st.backgroundColor, st.color];
              }
              return res;
            }""")
            page.hover("#ctx-success")
            ctx_success_hover = page.evaluate("() => getComputedStyle(document.getElementById('ctx-success')).backgroundColor")
            c4_actual = {"colors": ctx_before, "success_action_hover_bg": ctx_success_hover}
            c4_exp = {
                "colors": {
                    "success": ["rgb(195, 230, 203)", "rgb(21, 87, 36)"],
                    "secondary": ["rgb(214, 216, 219)", "rgb(56, 61, 65)"],
                    "info": ["rgb(190, 229, 235)", "rgb(12, 84, 96)"],
                    "warning": ["rgb(255, 238, 186)", "rgb(133, 100, 4)"],
                    "danger": ["rgb(245, 198, 203)", "rgb(114, 28, 36)"],
                    "primary": ["rgb(184, 218, 255)", "rgb(0, 64, 133)"],
                    "dark": ["rgb(198, 200, 202)", "rgb(27, 30, 33)"],
                    "light": ["rgb(253, 253, 254)", "rgb(129, 129, 130)"],
                },
                "success_action_hover_bg": "rgb(177, 223, 187)",
            }

            cases = [
                {"name": "basic_active_and_disabled_items", "input": "ul.list-group with .active & .disabled", "expected": c1_exp, "actual": c1_actual, "passed": c1_actual == c1_exp},
                {"name": "action_links_and_hover_state", "input": "a.list-group-item.list-group-item-action hover", "expected": c2_exp, "actual": c2_actual, "passed": c2_actual == c2_exp},
                {"name": "flush_and_horizontal_variants", "input": ".list-group-flush & .list-group-horizontal", "expected": c3_exp, "actual": c3_actual, "passed": c3_actual == c3_exp},
                {"name": "contextual_colors_and_link_variants", "input": "8 contextual classes & hoverable success link", "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 禁用、.list-group-item-action 悬停变灰 #f8f9fa、.list-group-flush 0px 外边框与 .list-group-horizontal 单行排布,以及 8 种语义色与加深悬停态。

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

本地运行

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

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

本章附件与实例包

下载课程关联工程包:Bootstrap4

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

下载课程关联工程包:Bootstrap4

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

常见错误与边界检查

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

技术规范与官方资料

练习与参考解答

查看参考解答

将 `<ul>` 改为 `<div class="list-group">`、`<li>` 改为 `<a>` 并添加 `.list-group-item-action`,以获得全宽点击区与悬停底色反馈;`.list-group-flush` 去除外框与圆角,适合紧贴放入 `.card` 等已有外边框的父容器中。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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