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

Tailwind CSS 边框

核对 Tailwind CSS 边框宽度、四向独立边框、边框颜色与线型、四角圆角、盒模型 box-sizing 及分割线/外环工具类的浏览器实测表现。

学完这一章,你能够

  • 核对 Tailwind CSS 边框宽度、四向独立边框、边框颜色与线型、四角圆角、盒模型 box-sizing 及分割线/外环工具类的浏览器实测表现。

边框体系与四向宽度控制

边框用于界定组件边界、分隔内容区块并强化交互轮廓。Tailwind CSS 将边框拆分为宽度、颜色、线型、圆角以及盒模型五个正交维度,开发者可按需自由组合,无需为局部改边重写整条 CSS 规则。

统一设置四边宽度时使用 `border`(`1px`)、`border-2`(`2px`)、`border-4`(`4px`)与 `border-8`(`8px`);按方向分别控制则使用 `border-t-*`、`border-r-*`、`border-b-*`、`border-l-*`。需特别留意默认刻度差异:Tailwind 内置 `1px` 写法为无后缀的 `border` / `border-b`,并不预置 `border-b-1` 或 `border-l-3`,若要精确设置 `3px` 边框应使用任意值 `border-l-[3px]` 或在主题中扩展 `borderWidth`。本站 Chromium 实测确认 `border`/`border-2`/`border-4` 分别为 `1px`/`2px`/`4px`,四向组合 `border-t-4 border-r-2 border-b border-l-[3px]` 精确测得 `['4px', '2px', '1px', '3px']`。

边框颜色与线型样式

边框着色通过 `border-{color}` 统一设置,也支持 `border-t/r/b/l-{color}` 分边配色;线型则由 `border-solid`(实线)、`border-dashed`(虚线)、`border-dotted`(点线)与 `border-double`(双线)切换。离线 Chromium 实测验证:`border-red-500` 解析为 `rgb(239, 68, 68)`,四边分色 `border-t-blue-300`、`border-r-gray-400`、`border-b-green-600`、`border-l-yellow-500` 分别计算为 `rgb(147, 197, 253)`、`rgb(156, 163, 175)`、`rgb(22, 163, 74)`、`rgb(234, 179, 8)`,四种线型对应 `solid`、`dashed`、`dotted`、`double`。

统一圆角与单角精细控制

圆角控制使用 `rounded-{size}` 统一设定四角半径,也可按左上 `rounded-tl-*`、右上 `rounded-tr-*`、右下 `rounded-br-*`、左下 `rounded-bl-*` 单独指定,胶囊或圆形元素常用 `rounded-full`。实测显示:`rounded-lg` 计算为 `8px`,四角混排 `rounded-tl-sm rounded-tr-md rounded-br-lg rounded-bl-xl` 依次测得 `['2px', '6px', '8px', '12px']`,`rounded-full` 为 `9999px`。

盒模型、分割线外环与综合运用

边框与内边距是否计入声明宽度取决于盒模型:`box-border` 对应 `border-box`,`box-content` 对应 `content-box`。本站对声明宽 `200px`、配 `p-4`(单侧 `16px`)与 `border-4`(单侧 `4px`)的容器实测:`box-border` 实际渲染宽维持 `200px`,而 `box-content` 撑大至 `240px`。在实战卡片与列表排版中,常结合 `divide-y-2 divide-gray-200` 在相邻子项之间自动插入 `2px` 顶部分割线(实测首项 `0px`、次项 `2px`),并以 `ring-2` 生成不占位的外环阴影 `rgb(59, 130, 246) 0px 0px 0px 2px` 突出交互轮廓。

边框宽度工具类

类名作用说明示例与说明
border-{n}统一设定四条边的边框宽度(默认支持 0/2/4/8,1px 用 border)border-2(2px)
border-t-{n}单独设置顶部边框宽度border-t-4(4px)
border-r-{n}单独设置右侧边框宽度border-r-8(8px)
border-b-{n}单独设置底部边框宽度(默认 1px 写作 border-b)border-b(1px)
border-l-{n}单独设置左侧边框宽度(非默认刻度用任意值如 border-l-[3px])border-l-[3px](3px)

边框颜色工具类

类名作用说明示例与说明
border-{color}统一设置四条边框颜色border-red-500
border-t-{color}设置顶部边框颜色border-t-blue-300
border-r-{color}设置右侧边框颜色border-r-gray-400
border-b-{color}设置底部边框颜色border-b-green-600
border-l-{color}设置左侧边框颜色border-l-yellow-500

边框线型工具类

类名作用说明示例与说明
border-solid实线边框(Preflight 重置后默认线型)border-solid
border-dashed虚线边框border-dashed
border-dotted点线边框border-dotted
border-double双线边框(通常需配合 ≥3px 宽度清晰可见)border-double

边框圆角工具类

类名作用说明示例与说明
rounded-{size}统一设置四个角的 border-radiusrounded-lg(8px)
rounded-tl-{size}单独设置左上角圆角rounded-tl-sm(2px)
rounded-tr-{size}单独设置右上角圆角rounded-tr-md(6px)
rounded-br-{size}单独设置右下角圆角rounded-br-lg(8px)
rounded-bl-{size}单独设置左下角圆角rounded-bl-xl(12px)

四向边框独立宽度对照

类名作用说明示例与说明
border-t-{size}控制顶部边框粗细border-t-2(2px)
border-r-{size}控制右侧边框粗细border-r-4(4px)
border-b-{size}控制底部边框粗细(1px 默认写作 border-b)border-b(1px)
border-l-{size}控制左侧边框粗细(3px 需配置或任意值)border-l-[3px](3px)

统一边框快捷类

类名作用说明示例与说明
border为四边设置 1px 宽度(配合默认实线风格)border
border-2为四边设置 2px 宽度border-2
border-4为四边设置 4px 宽度border-4

盒模型计算方式工具类

类名作用说明示例与说明
box-borderborder-box:声明宽高包含内边距与边框box-border(200px 实测仍为 200px)
box-contentcontent-box:声明宽高仅含内容区,边框内边距外扩box-content(200px 实测扩为 240px)

离线 Chromium 实测四向边框、线型颜色、圆角、盒模型尺寸及分割外环

含完整源码 · 145 行
展开查看收起
python
"""Offline Chromium verification for Tailwind CSS borders chapter."""
import json
from playwright.sync_api import sync_playwright

CSS = """
*, *::before, *::after { box-sizing: border-box; border-width: 0; border-style: solid; border-color: #e5e7eb; }
.border { border-width: 1px; }
.border-2 { border-width: 2px; }
.border-4 { border-width: 4px; }
.border-t-4 { border-top-width: 4px; }
.border-r-2 { border-right-width: 2px; }
.border-b { border-bottom-width: 1px; }
.border-l-\\[3px\\] { border-left-width: 3px; }
.border-red-500 { border-color: rgb(239 68 68); }
.border-blue-500 { border-color: rgb(59 130 246); }
.border-t-blue-300 { border-top-color: rgb(147 197 253); }
.border-r-gray-400 { border-right-color: rgb(156 163 175); }
.border-b-green-600 { border-bottom-color: rgb(22 163 74); }
.border-l-yellow-500 { border-left-color: rgb(234 179 8); }
.border-solid { border-style: solid; }
.border-dashed { border-style: dashed; }
.border-dotted { border-style: dotted; }
.border-double { border-style: double; }
.rounded-lg { border-radius: 0.5rem; }
.rounded-full { border-radius: 9999px; }
.rounded-tl-sm { border-top-left-radius: 0.125rem; }
.rounded-tr-md { border-top-right-radius: 0.375rem; }
.rounded-br-lg { border-bottom-right-radius: 0.5rem; }
.rounded-bl-xl { border-bottom-left-radius: 0.75rem; }
.box-border { box-sizing: border-box; }
.box-content { box-sizing: content-box; }
.w-50 { width: 200px; }
.p-4 { padding: 1rem; }
.divide-y-2 > :not([hidden]) ~ :not([hidden]) { border-top-width: 2px; }
.divide-gray-200 > :not([hidden]) ~ :not([hidden]) { border-color: rgb(229 231 235); }
.ring-2 { box-shadow: 0 0 0 2px rgb(59 130 246); }
"""


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 pw:
        browser = launch_browser(pw)
        page = browser.new_page()
        page.set_content(f"""<!doctype html><html><head><style>{CSS}</style></head><body>
            <div id="w-1" class="border">1px</div>
            <div id="w-2" class="border-2">2px</div>
            <div id="w-4" class="border-4">4px</div>
            <div id="w-sides" class="border-t-4 border-r-2 border-b border-l-[3px] border-blue-500">Sides</div>

            <div id="c-all" class="border-2 border-red-500 border-solid">All</div>
            <div id="c-sides" class="border-2 border-t-blue-300 border-r-gray-400 border-b-green-600 border-l-yellow-500">SideColors</div>
            <div id="s-dashed" class="border-4 border-dashed">Dashed</div>
            <div id="s-dotted" class="border-2 border-dotted">Dotted</div>
            <div id="s-double" class="border-4 border-double">Double</div>

            <div id="r-lg" class="border-4 rounded-lg">LG</div>
            <div id="r-corners" class="border-2 rounded-tl-sm rounded-tr-md rounded-br-lg rounded-bl-xl">Corners</div>
            <div id="r-full" class="rounded-full">Full</div>

            <div id="b-border" class="box-border w-50 p-4 border-4 border-red-500">BorderBox</div>
            <div id="b-content" class="box-content w-50 p-4 border-4 border-red-500">ContentBox</div>
            <div id="div-list" class="divide-y-2 divide-gray-200"><div id="d1">A</div><div id="d2">B</div></div>
            <button id="ring-btn" class="ring-2">Ring</button>
        </body></html>""")

        c1_act = page.evaluate("""() => {
            const st = id => getComputedStyle(document.getElementById(id));
            const s = st('w-sides');
            return {
                b1: st('w-1').borderTopWidth,
                b2: st('w-2').borderTopWidth,
                b4: st('w-4').borderTopWidth,
                sides: [s.borderTopWidth, s.borderRightWidth, s.borderBottomWidth, s.borderLeftWidth],
            };
        }""")
        c1_exp = {"b1": "1px", "b2": "2px", "b4": "4px", "sides": ["4px", "2px", "1px", "3px"]}
        cases.append(dict(name="border_widths_and_sides", expected=c1_exp, actual=c1_act, passed=(c1_act == c1_exp)))

        c2_act = page.evaluate("""() => {
            const st = id => getComputedStyle(document.getElementById(id));
            const cs = st('c-sides');
            return {
                red_all: st('c-all').borderTopColor,
                side_colors: [cs.borderTopColor, cs.borderRightColor, cs.borderBottomColor, cs.borderLeftColor],
                styles: [st('c-all').borderTopStyle, st('s-dashed').borderTopStyle, st('s-dotted').borderTopStyle, st('s-double').borderTopStyle],
            };
        }""")
        c2_exp = {
            "red_all": "rgb(239, 68, 68)",
            "side_colors": ["rgb(147, 197, 253)", "rgb(156, 163, 175)", "rgb(22, 163, 74)", "rgb(234, 179, 8)"],
            "styles": ["solid", "dashed", "dotted", "double"],
        }
        cases.append(dict(name="border_colors_and_styles", expected=c2_exp, actual=c2_act, passed=(c2_act == c2_exp)))

        c3_act = page.evaluate("""() => {
            const st = id => getComputedStyle(document.getElementById(id));
            const rc = st('r-corners');
            return {
                lg: st('r-lg').borderTopLeftRadius,
                corners: [rc.borderTopLeftRadius, rc.borderTopRightRadius, rc.borderBottomRightRadius, rc.borderBottomLeftRadius],
                full: st('r-full').borderTopLeftRadius,
            };
        }""")
        c3_exp = {"lg": "8px", "corners": ["2px", "6px", "8px", "12px"], "full": "9999px"}
        cases.append(dict(name="border_radius_and_corners", expected=c3_exp, actual=c3_act, passed=(c3_act == c3_exp)))

        c4_act = page.evaluate("""() => {
            const bb = document.getElementById('b-border');
            const bc = document.getElementById('b-content');
            return {
                border_box_sizing: getComputedStyle(bb).boxSizing,
                border_box_width: Math.round(bb.getBoundingClientRect().width),
                content_box_sizing: getComputedStyle(bc).boxSizing,
                content_box_width: Math.round(bc.getBoundingClientRect().width),
                divide_first_top: getComputedStyle(document.getElementById('d1')).borderTopWidth,
                divide_second_top: getComputedStyle(document.getElementById('d2')).borderTopWidth,
                ring_shadow: getComputedStyle(document.getElementById('ring-btn')).boxShadow,
            };
        }""")
        c4_exp = {
            "border_box_sizing": "border-box",
            "border_box_width": 200,
            "content_box_sizing": "content-box",
            "content_box_width": 240,
            "divide_first_top": "0px",
            "divide_second_top": "2px",
            "ring_shadow": "rgb(59, 130, 246) 0px 0px 0px 2px",
        }
        cases.append(dict(name="box_sizing_divide_and_ring", expected=c4_exp, actual=c4_act, passed=(c4_act == c4_exp)))
        browser.close()

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


if __name__ == "__main__":
    main()
核对 1px/2px/4px 及四向不同宽度、四边独立颜色与 4 种线型、四角独立圆角、border-box(200px) vs content-box(240px)、divide-y-2 及 ring-2 全部一致。

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

本地运行

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

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

本章附件与实例包

下载课程关联工程包:Tailwind CSS

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

下载课程关联工程包:Tailwind CSS

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

常见错误与边界检查

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

技术规范与官方资料

练习与参考解答

查看参考解答

box-border(border-box)下边框与内边距向内挤压内容区,总渲染宽度保持 200px;box-content(content-box)下需额外累加左右内边距 32px 与左右边框 8px,最终渲染宽度为 240px。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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