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

离线 Chromium 实测 Bootstrap 5 图像形状、.float-start/.float-end、.mx-auto.d-block 与 .img-fluid

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-images.py
explore-images.py192 行
"""Offline Playwright Chromium lab for Bootstrap 5 Chapter 08 (images)."""
import json
from playwright.sync_api import sync_playwright

BS5_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; }
.rounded { border-radius: 0.25rem !important; }
.rounded-circle { border-radius: 50% !important; }
.img-thumbnail {
  padding: 0.25rem;
  background-color: #fff;
  border: 1px solid #dee2e6;
  border-radius: 0.25rem;
  max-width: 100%;
  height: auto;
}
.float-start { float: left !important; }
.float-end { float: right !important; }
.mx-auto { margin-right: auto !important; margin-left: auto !important; }
.d-block { display: block !important; }
.img-fluid { max-width: 100%; height: auto; }
.clearfix::after { display: block; clear: both; content: ""; }
"""

SVG_300 = "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='300' height='200'><rect width='300' height='200' fill='%230d6efd'/></svg>"
SVG_800 = "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='800' height='400'><rect width='800' height='400' fill='%23198754'/></svg>"

HTML_DOC = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{BS5_CSS}</style></head>
<body>
  <img id="img-rounded" src="{SVG_300}" class="rounded" alt="rounded">
  <img id="img-circle" src="{SVG_300}" class="rounded-circle" alt="circle">
  <img id="img-thumb" src="{SVG_300}" class="img-thumbnail" alt="thumb">

  <div id="align-box" class="clearfix" style="width: 600px;">
    <img id="img-start" src="{SVG_300}" width="120" height="80" class="float-start" alt="start">
    <img id="img-end" src="{SVG_300}" width="120" height="80" class="float-end" alt="end">
  </div>

  <div id="center-box" style="width: 600px;">
    <img id="img-center" src="{SVG_300}" width="300" height="200" class="mx-auto d-block" alt="center">
  </div>

  <div id="fluid-wide" style="width: 1000px;">
    <img id="img-fluid-w" src="{SVG_800}" class="img-fluid" alt="fluid wide">
  </div>
  <div id="fluid-narrow" style="width: 400px;">
    <img id="img-fluid-n" src="{SVG_800}" class="img-fluid" alt="fluid narrow">
  </div>
</body>
</html>
"""


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


def main():
    cases = []
    with sync_playwright() as p:
        browser = launch_browser(p)
        page = browser.new_page(viewport={"width": 1200, "height": 900})
        page.set_content(HTML_DOC)

        # Case 1: .rounded, .rounded-circle, .img-thumbnail
        c1 = page.evaluate("""() => {
            const rd = getComputedStyle(document.getElementById('img-rounded'));
            const ci = getComputedStyle(document.getElementById('img-circle'));
            const th = getComputedStyle(document.getElementById('img-thumb'));
            return {
                rounded_radius: rd.borderRadius,
                circle_radius: ci.borderRadius,
                thumb: {
                    padding: th.paddingTop,
                    border_width: th.borderTopWidth,
                    border_color: th.borderTopColor,
                    radius: th.borderRadius,
                    bg: th.backgroundColor
                }
            };
        }""")
        exp1 = {
            "rounded_radius": "4px",
            "circle_radius": "50%",
            "thumb": {
                "padding": "4px",
                "border_width": "1px",
                "border_color": "rgb(222, 226, 230)",
                "radius": "4px",
                "bg": "rgb(255, 255, 255)",
            },
        }
        cases.append({
            "name": "image_shapes_rounded_circle_thumbnail",
            "passed": c1 == exp1,
            "expected": exp1,
            "actual": c1,
        })

        # Case 2: .float-start and .float-end alignment
        c2 = page.evaluate("""() => {
            const box = document.getElementById('align-box').getBoundingClientRect();
            const st = document.getElementById('img-start');
            const en = document.getElementById('img-end');
            const rs = st.getBoundingClientRect();
            const re = en.getBoundingClientRect();
            return {
                start_float: getComputedStyle(st).float,
                end_float: getComputedStyle(en).float,
                start_offset_left: Math.round(rs.left - box.left),
                end_offset_right: Math.round(box.right - re.right),
                same_top: Math.abs(rs.top - re.top) < 1
            };
        }""")
        exp2 = {
            "start_float": "left",
            "end_float": "right",
            "start_offset_left": 0,
            "end_offset_right": 0,
            "same_top": True,
        }
        cases.append({
            "name": "image_float_start_and_end",
            "passed": c2 == exp2,
            "expected": exp2,
            "actual": c2,
        })

        # Case 3: .mx-auto.d-block centering
        c3 = page.evaluate("""() => {
            const box = document.getElementById('center-box').getBoundingClientRect();
            const el = document.getElementById('img-center');
            const r = el.getBoundingClientRect();
            const cs = getComputedStyle(el);
            return {
                display: cs.display,
                margins_x: [cs.marginLeft, cs.marginRight],
                left_gap: Math.round(r.left - box.left),
                right_gap: Math.round(box.right - r.right)
            };
        }""")
        exp3 = {
            "display": "block",
            "margins_x": ["150px", "150px"],
            "left_gap": 150,
            "right_gap": 150,
        }
        cases.append({
            "name": "image_center_mx_auto_d_block",
            "passed": c3 == exp3,
            "expected": exp3,
            "actual": c3,
        })

        # Case 4: .img-fluid responsive scaling
        c4 = page.evaluate("""() => {
            const w = document.getElementById('img-fluid-w');
            const n = document.getElementById('img-fluid-n');
            const rw = w.getBoundingClientRect();
            const rn = n.getBoundingClientRect();
            return {
                max_width: getComputedStyle(n).maxWidth,
                wide_box_dims: [Math.round(rw.width), Math.round(rw.height)],
                narrow_box_dims: [Math.round(rn.width), Math.round(rn.height)]
            };
        }""")
        exp4 = {
            "max_width": "100%",
            "wide_box_dims": [800, 400],
            "narrow_box_dims": [400, 200],
        }
        cases.append({
            "name": "responsive_img_fluid",
            "passed": c4 == exp4,
            "expected": exp4,
            "actual": c4,
        })

        browser.close()
    print(json.dumps({"cases": cases}, ensure_ascii=False))


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

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

入口文件:projects/bootstrap5-images-lab/explore-images.py

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

下载课程关联工程包:Bootstrap5

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

下载课程关联工程包:Bootstrap5

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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