蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 Bootstrap 4 卡片结构、8 种配色、图文叠加、水平卡片及多卡容器 Chromium 实验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-cards.py
explore-cards.py254 行
"""Offline Playwright Chromium verification for Bootstrap 4 Cards."""
import json
from playwright.sync_api import sync_playwright

SVG_IMG = "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='300' height='180'><rect width='300' height='180' fill='%232c6e8a'/></svg>"

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; color: #212529; }
.card {
  position: relative; display: flex; flex-direction: column; min-width: 0; word-wrap: break-word;
  background-color: #fff; background-clip: border-box; border: 1px solid rgba(0, 0, 0, .125); border-radius: 0.25rem;
}
.card-body { flex: 1 1 auto; min-height: 1px; padding: 1.25rem; }
.card-title { margin-top: 0; margin-bottom: 0.75rem; }
.card-subtitle { margin-top: -0.375rem; margin-bottom: 0; }
.card-text:last-child { margin-bottom: 0; }
.card-link + .card-link { margin-left: 1.25rem; }
.card-header { padding: 0.75rem 1.25rem; margin-bottom: 0; background-color: rgba(0, 0, 0, .03); border-bottom: 1px solid rgba(0, 0, 0, .125); }
.card-footer { padding: 0.75rem 1.25rem; background-color: rgba(0, 0, 0, .03); border-top: 1px solid rgba(0, 0, 0, .125); }
.card-img, .card-img-top, .card-img-bottom { flex-shrink: 0; width: 100%; display: block; }
.card-img-top { border-top-left-radius: calc(0.25rem - 1px); border-top-right-radius: calc(0.25rem - 1px); }
.card-img-bottom { border-bottom-right-radius: calc(0.25rem - 1px); border-bottom-left-radius: calc(0.25rem - 1px); }
.card-img-overlay { position: absolute; top: 0; right: 0; bottom: 0; left: 0; padding: 1.25rem; }
.row { display: flex; flex-wrap: wrap; margin-right: -15px; margin-left: -15px; }
.no-gutters { margin-right: 0; margin-left: 0; }
.no-gutters > .col, .no-gutters > [class*="col-"] { padding-right: 0; padding-left: 0; }
.col-md-4 { flex: 0 0 33.333333%; max-width: 33.333333%; padding-right: 15px; padding-left: 15px; }
.col-md-8 { flex: 0 0 66.666667%; max-width: 66.666667%; padding-right: 15px; padding-left: 15px; }
.card-group { display: flex; flex-flow: row wrap; width: 600px; }
.card-group > .card { flex: 1 0 0%; margin-bottom: 0; }
.card-group > .card + .card { margin-left: 0; border-left: 0; }
.card-deck { display: flex; flex-flow: row wrap; margin-right: -15px; margin-left: -15px; width: 630px; }
.card-deck .card { flex: 1 0 0%; margin-right: 15px; margin-bottom: 0; margin-left: 15px; }
.card-columns { column-count: 3; column-gap: 1.25rem; width: 600px; }
.card-columns .card { display: inline-block; width: 100%; margin-bottom: 0.75rem; }
.bg-primary { background-color: #007bff !important; }
.bg-success { background-color: #28a745 !important; }
.bg-info { background-color: #17a2b8 !important; }
.bg-warning { background-color: #ffc107 !important; }
.bg-danger { background-color: #dc3545 !important; }
.bg-secondary { background-color: #6c757d !important; }
.bg-dark { background-color: #343a40 !important; }
.bg-light { background-color: #f8f9fa !important; }
.text-white { color: #fff !important; }
.text-dark { color: #343a40 !important; }
.text-muted { color: #6c757d !important; }
"""

HTML = f"""<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><style>{CSS}</style></head>
<body>
  <div class="card" id="c-simple"><div class="card-body" id="c-simple-body">简单的卡片</div></div>
  <div class="card" id="c-hf">
    <div class="card-header" id="c-hdr">头部</div>
    <div class="card-body">内容</div>
    <div class="card-footer" id="c-ftr">底部</div>
  </div>
  <div class="card" id="c-typo">
    <div class="card-body">
      <h4 class="card-title" id="c-title">Card title</h4>
      <h6 class="card-subtitle text-muted" id="c-sub">Subtitle</h6>
      <p class="card-text">Example text</p>
      <a href="#" class="card-link" id="c-l1">Card link</a>
      <a href="#" class="card-link" id="c-l2">Another link</a>
    </div>
  </div>

  <div id="c-colors">
    <div class="card bg-primary text-white" id="bg-primary"><div class="card-body">P</div></div>
    <div class="card bg-success text-white" id="bg-success"><div class="card-body">S</div></div>
    <div class="card bg-info text-white" id="bg-info"><div class="card-body">I</div></div>
    <div class="card bg-warning text-white" id="bg-warning"><div class="card-body">W</div></div>
    <div class="card bg-danger text-white" id="bg-danger"><div class="card-body">D</div></div>
    <div class="card bg-secondary text-white" id="bg-secondary"><div class="card-body">Sec</div></div>
    <div class="card bg-dark text-white" id="bg-dark"><div class="card-body">Dk</div></div>
    <div class="card bg-light text-dark" id="bg-light"><div class="card-body">Lt</div></div>
  </div>

  <div class="card" style="width:300px" id="c-img-top">
    <img class="card-img-top" id="img-top" src="{SVG_IMG}" alt="top">
    <div class="card-body" id="body-top"><h4 class="card-title">Top</h4></div>
  </div>
  <div class="card" style="width:300px" id="c-img-bot">
    <div class="card-body" id="body-bot"><h4 class="card-title">Bottom</h4></div>
    <img class="card-img-bottom" id="img-bot" src="{SVG_IMG}" alt="bottom">
  </div>
  <div class="card" style="width:300px" id="c-overlay">
    <img class="card-img-top" id="img-ov" src="{SVG_IMG}" alt="bg">
    <div class="card-img-overlay" id="ov-layer"><h4 class="card-title">Overlay</h4></div>
  </div>

  <div class="card" style="max-width:540px; width:540px;" id="c-horiz">
    <div class="row no-gutters" id="h-row">
      <div class="col-md-4" id="h-col4"><img src="{SVG_IMG}" class="card-img" alt="avatar"></div>
      <div class="col-md-8" id="h-col8"><div class="card-body"><h5 class="card-title">Card title</h5><p class="card-text">Horizontal card text.</p></div></div>
    </div>
  </div>

  <div class="card-group" id="cg">
    <div class="card" id="cg1"><div class="card-body">Short</div></div>
    <div class="card" id="cg2"><div class="card-body">Line 1<br>Line 2<br>Line 3</div></div>
  </div>
  <div class="card-deck" id="cd">
    <div class="card" id="cd1"><div class="card-body">Short</div></div>
    <div class="card" id="cd2"><div class="card-body">Line 1<br>Line 2<br>Line 3</div></div>
  </div>
  <div class="card-columns" id="cc">
    <div class="card"><div class="card-body">A</div></div>
    <div class="card"><div class="card-body">B</div></div>
    <div class="card"><div class="card-body">C</div></div>
  </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": 1200, "height": 900})
            page.set_content(HTML, wait_until="load")

            c1_actual = page.evaluate("""() => {
              const card = getComputedStyle(document.getElementById('c-simple'));
              const body = getComputedStyle(document.getElementById('c-simple-body'));
              const hdr = getComputedStyle(document.getElementById('c-hdr'));
              const ftr = getComputedStyle(document.getElementById('c-ftr'));
              const sub = getComputedStyle(document.getElementById('c-sub'));
              const l2 = getComputedStyle(document.getElementById('c-l2'));
              return {
                card_display: card.display,
                card_dir: card.flexDirection,
                card_radius: card.borderRadius,
                body_padding: body.paddingTop,
                header_footer_bg: [hdr.backgroundColor, ftr.backgroundColor],
                subtitle_margin_top: sub.marginTop,
                second_link_margin_left: l2.marginLeft
              };
            }""")
            c1_exp = {
                "card_display": "flex",
                "card_dir": "column",
                "card_radius": "4px",
                "body_padding": "20px",
                "header_footer_bg": ["rgba(0, 0, 0, 0.03)", "rgba(0, 0, 0, 0.03)"],
                "subtitle_margin_top": "-6px",
                "second_link_margin_left": "20px",
            }

            c2_actual = page.evaluate("""() => {
              const keys = ['primary','success','info','warning','danger','secondary','dark','light'];
              const res = {};
              for (const k of keys) {
                const st = getComputedStyle(document.getElementById('bg-'+k));
                res[k] = [st.backgroundColor, st.color];
              }
              return res;
            }""")
            c2_exp = {
                "primary": ["rgb(0, 123, 255)", "rgb(255, 255, 255)"],
                "success": ["rgb(40, 167, 69)", "rgb(255, 255, 255)"],
                "info": ["rgb(23, 162, 184)", "rgb(255, 255, 255)"],
                "warning": ["rgb(255, 193, 7)", "rgb(255, 255, 255)"],
                "danger": ["rgb(220, 53, 69)", "rgb(255, 255, 255)"],
                "secondary": ["rgb(108, 117, 125)", "rgb(255, 255, 255)"],
                "dark": ["rgb(52, 58, 64)", "rgb(255, 255, 255)"],
                "light": ["rgb(248, 249, 250)", "rgb(52, 58, 64)"],
            }

            c3_actual = page.evaluate("""() => {
              const it = document.getElementById('img-top').getBoundingClientRect();
              const bt = document.getElementById('body-top').getBoundingClientRect();
              const ib = document.getElementById('img-bot').getBoundingClientRect();
              const bb = document.getElementById('body-bot').getBoundingClientRect();
              const io = document.getElementById('img-ov').getBoundingClientRect();
              const ov = document.getElementById('ov-layer');
              const ovr = ov.getBoundingClientRect();
              return {
                img_top_above_body: it.bottom <= bt.top + 1,
                img_bottom_below_body: ib.top >= bb.bottom - 1,
                overlay_position: getComputedStyle(ov).position,
                overlay_covers_img: Math.abs(ovr.top - io.top) < 2 && Math.abs(ovr.left - io.left) < 2
              };
            }""")
            c3_exp = {
                "img_top_above_body": True,
                "img_bottom_below_body": True,
                "overlay_position": "absolute",
                "overlay_covers_img": True,
            }

            c4_actual = page.evaluate("""() => {
              const row = getComputedStyle(document.getElementById('h-row'));
              const c4 = document.getElementById('h-col4');
              const c8 = document.getElementById('h-col8');
              const r4 = c4.getBoundingClientRect(), r8 = c8.getBoundingClientRect();
              return {
                no_gutters_margin: [row.marginLeft, row.marginRight],
                col_padding: [getComputedStyle(c4).paddingLeft, getComputedStyle(c8).paddingRight],
                side_by_side: Math.abs(r4.top - r8.top) < 1 && Math.abs(r8.left - r4.right) < 1,
                width_ratio_2_to_1: Math.round(r8.width / r4.width) === 2
              };
            }""")
            c4_exp = {
                "no_gutters_margin": ["0px", "0px"],
                "col_padding": ["0px", "0px"],
                "side_by_side": True,
                "width_ratio_2_to_1": True,
            }

            c5_actual = page.evaluate("""() => {
              const g1 = document.getElementById('cg1').getBoundingClientRect();
              const g2 = document.getElementById('cg2').getBoundingClientRect();
              const d1 = document.getElementById('cd1').getBoundingClientRect();
              const d2 = document.getElementById('cd2').getBoundingClientRect();
              const cc = getComputedStyle(document.getElementById('cc'));
              return {
                group_equal_height: Math.abs(g1.height - g2.height) < 1,
                group_gap_px: Math.round(g2.left - g1.right),
                deck_equal_height: Math.abs(d1.height - d2.height) < 1,
                deck_gap_px: Math.round(d2.left - d1.right),
                columns_count: cc.columnCount
              };
            }""")
            c5_exp = {
                "group_equal_height": True,
                "group_gap_px": 0,
                "deck_equal_height": True,
                "deck_gap_px": 30,
                "columns_count": "3",
            }

            cases = [
                {"name": "basic_header_footer_and_typography", "input": ".card, .card-body, .card-header/.card-footer, .card-title/.card-subtitle/.card-link", "expected": c1_exp, "actual": c1_actual, "passed": c1_actual == c1_exp},
                {"name": "contextual_color_cards", "input": "8 contextual .bg-* cards", "expected": c2_exp, "actual": c2_actual, "passed": c2_actual == c2_exp},
                {"name": "image_top_bottom_and_overlay", "input": ".card-img-top, .card-img-bottom & .card-img-overlay", "expected": c3_exp, "actual": c3_actual, "passed": c3_actual == c3_exp},
                {"name": "horizontal_card_no_gutters", "input": ".row.no-gutters + .col-md-4/.col-md-8 horizontal card", "expected": c4_exp, "actual": c4_actual, "passed": c4_actual == c4_exp},
                {"name": "card_group_deck_and_columns", "input": ".card-group, .card-deck & .card-columns", "expected": c5_exp, "actual": c5_actual, "passed": c5_actual == c5_exp},
            ]
            print(json.dumps({"cases": cases}, ensure_ascii=False))
        finally:
            browser.close()


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

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

入口文件:projects/bootstrap4-cards-lab/explore-cards.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-cards.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Bootstrap4

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

下载课程关联工程包:Bootstrap4

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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