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

Foundation 图标

核对 Foundation Icon Fonts 的 283 个图标类语法(fi-name)、CSS 字号与色彩定制、按钮内图标继承,以及 .icon-bar 工具条的多列均分、文字标签、禁用态与垂直模式。

学完这一章,你能够

  • 核对 Foundation Icon Fonts 的 283 个图标类语法(fi-name)、CSS 字号与色彩定制、按钮内图标继承,以及 .icon-bar 工具条的多列均分、文字标签、禁用态与垂直模式。

Foundation 图标语法与样式定制

Foundation Icons 图标字库提供 283 个矢量字体图标,可灵活嵌入正文、按钮、工具条、导航栏与表单控件,并像普通文字一样通过 CSS 自由调整字号与颜色。常见示例包括刷新图标(`fi-refresh`)、勾选检测图标(`fi-check`)与主页图标(`fi-home`),均可在默认字号、`20px` 与 `30px` 红色等样式间直接缩放。

创建图标的标准语法为 `<i class="fi-name"></i>`(将 `name` 替换为具体图标名),使用前需在 `<head>` 中引入 Foundation Icons 样式表(如 `foundation-icons.min.css` 或本地托管图标字体包)。

图标可直接置于普通段落、`<a>` 链接或不同语境的 `.button` 按钮内部,自动继承父元素的文字颜色与字号,也可以通过行内样式单独覆写。本站离线 Chromium 实测验证:`fi-refresh` 默认继承 `16px` / `rgb(34, 34, 34)`,行内指定后精确变为 `20px` 及 `30px` 红色 `rgb(255, 0, 0)`,`fi-cloud` 设为 `35px` 红色生效;嵌入 `.button` 与 `.button.info.large` 内部的 `fi-home` 则分别继承白色 `rgb(255, 255, 255)` 与深灰 `rgb(51, 51, 51)`、`20px` 字号。

图标工具条(.icon-bar)布局、标签、禁用与垂直模式

使用 `<div class="icon-bar five-up">` 配合子项 `<a class="item">` 可构建等宽水平图标工具栏(`five-up` 表示五等分,同理还有 `two-up` 至 `six-up`);在每个 `.item` 内紧随 `<i>` 添加 `<label>` 即可在图标正下方展示说明文字。本站于 `800px` 容器内实测 `.icon-bar.five-up` 背景为深灰 `rgb(51, 51, 51)`,5 个 `.item` 均分各占 `160px`(`20%`),图标字号 `30px`(`1.875rem`),`<label>` 呈 `display: block` 且位于图标下方。

为单个工具条项添加 `.disabled` 类可将其置为不可交互状态(实测 `opacity: 0.7`、`pointer-events: none`、`cursor: default`);为 `.icon-bar` 容器附加 `.vertical` 类则将工具栏转为窄侧边垂直堆叠模式(实测整栏宽 `112px` 即 `7rem`,内部 5 个 `.item` 各自占满 `112px` 并自上而下纵向排布)。完整 283 个图标名称可进一步查阅 Foundation 图标参考手册。

离线验证 Foundation 图标类缩放配色、按钮内继承与 .icon-bar 五等分/禁用/垂直模式

含完整源码 · 205 行
展开查看收起
python
"""Offline Playwright Chromium lab for Foundation Ch 07 (icons)."""
import json
from playwright.sync_api import sync_playwright

FOUNDATION_ICON_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; padding: 16px; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; color: #222222; }
.wrap { width: 800px; }
[class^="fi-"], [class*=" fi-"] { display: inline-block; font-style: normal; font-weight: normal; line-height: 1; vertical-align: middle; }
.fi-refresh::before { content: "\\21BB"; }
.fi-check::before { content: "\\2713"; }
.fi-home::before { content: "\\2302"; }
.fi-cloud::before { content: "\\2601"; }
.fi-bookmark::before { content: "\\2605"; }
.fi-info::before { content: "\\24D8"; }
.fi-mail::before { content: "\\2709"; }
.fi-like::before { content: "\\2665"; }
.fi-share::before { content: "\\21AA"; }
.fi-magnifying-glass::before { content: "\\2315"; }
.button { display: inline-block; padding: 1rem 2rem; font-size: 1rem; background: #008CBA; color: #ffffff; border: 0; text-decoration: none; cursor: pointer; }
.button.success { background: #43AC6A; }
.button.info { background: #a0d3e8; color: #333333; }
.button.large { padding: 1.125rem 2.25rem; font-size: 1.25rem; }
.icon-bar { width: 100%; font-size: 0; display: inline-block; background: #333333; margin-bottom: 16px; }
.icon-bar > .item { text-align: center; font-size: 1rem; width: 25%; margin: 0 auto; display: block; padding: 1.25rem; float: left; text-decoration: none; }
.icon-bar.five-up > .item { width: 20%; }
.icon-bar .item i { display: block; font-size: 1.875rem; color: #ffffff; margin: 0 auto 0.25rem; }
.icon-bar .item label { display: block; font-size: 1rem; color: #ffffff; margin: 0; cursor: pointer; }
.icon-bar .item.disabled { opacity: 0.7; pointer-events: none; cursor: default; }
.icon-bar.vertical { width: 7rem; }
.icon-bar.vertical > .item { width: 100%; float: none; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_ICON_CSS}</style></head>
<body>
  <div class="wrap">
    <p>刷新: <i id="ic-ref-1" class="fi-refresh"></i> <i id="ic-ref-20" class="fi-refresh" style="font-size:20px;"></i> <i id="ic-ref-30" class="fi-refresh" style="font-size:30px;color:red"></i></p>
    <p>检测: <i id="ic-chk" class="fi-check"></i> 主页: <i id="ic-home" class="fi-home"></i></p>
    <p>Cloud: <i id="ic-cloud-def" class="fi-cloud"></i> <a id="ic-cloud-link" href="#"><i class="fi-cloud"></i></a> <i id="ic-cloud-styled" class="fi-cloud" style="font-size:35px;color:red;"></i></p>
    <p>
      <button id="btn-home" type="button" class="button"><i class="fi-home"></i> Home</button>
      <button id="btn-home-ok" type="button" class="button success"><i class="fi-home"></i> Home</button>
      <a id="btn-home-info" href="#" class="button info large"><i class="fi-home"></i> Home</a>
    </p>
    <div id="bar-five" class="icon-bar five-up">
      <a href="#" class="item"><i class="fi-home"></i></a>
      <a href="#" class="item"><i class="fi-bookmark"></i></a>
      <a href="#" class="item"><i class="fi-info"></i></a>
      <a href="#" class="item"><i class="fi-mail"></i></a>
      <a href="#" class="item"><i class="fi-like"></i></a>
    </div>
    <div id="bar-labeled" class="icon-bar five-up">
      <a href="#" class="item"><i class="fi-home"></i><label>Home</label></a>
      <a href="#" class="item disabled" id="item-dis-share"><i class="fi-share"></i><label>Share</label></a>
      <a href="#" class="item"><i class="fi-info"></i><label>Info</label></a>
      <a href="#" class="item disabled" id="item-dis-mail"><i class="fi-mail"></i><label>Mail</label></a>
      <a href="#" class="item"><i class="fi-magnifying-glass"></i><label>Search</label></a>
    </div>
    <div id="bar-vertical" class="icon-bar vertical five-up">
      <a href="#" class="item"><i class="fi-home"></i><label>Home</label></a>
      <a href="#" class="item"><i class="fi-bookmark"></i><label>Bookmark</label></a>
      <a href="#" class="item"><i class="fi-info"></i><label>Info</label></a>
      <a href="#" class="item"><i class="fi-mail"></i><label>Mail</label></a>
      <a href="#" class="item"><i class="fi-like"></i><label>Like</label></a>
    </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():
    cases = []
    with sync_playwright() as p:
        browser = launch_browser(p)
        page = browser.new_page(viewport={"width": 900, "height": 1200})
        page.set_content(HTML_PAGE)

        # Case 1: fi-* icon syntax, font-size scaling & color styling
        actual_1 = page.evaluate("""() => {
            const g = (id) => {
                const s = getComputedStyle(document.getElementById(id));
                return { font_size: s.fontSize, color: s.color };
            };
            return {
                refresh_default: g('ic-ref-1'),
                refresh_20: g('ic-ref-20'),
                refresh_30_red: g('ic-ref-30'),
                cloud_35_red: g('ic-cloud-styled')
            };
        }""")
        expected_1 = {
            "refresh_default": {"font_size": "16px", "color": "rgb(34, 34, 34)"},
            "refresh_20": {"font_size": "20px", "color": "rgb(34, 34, 34)"},
            "refresh_30_red": {"font_size": "30px", "color": "rgb(255, 0, 0)"},
            "cloud_35_red": {"font_size": "35px", "color": "rgb(255, 0, 0)"},
        }
        cases.append({
            "name": "icon_syntax_and_inline_styling",
            "expected": expected_1,
            "actual": actual_1,
            "passed": actual_1 == expected_1,
        })

        # Case 2: icons inside buttons and links inherit color/size
        actual_2 = page.evaluate("""() => {
            const b1 = getComputedStyle(document.querySelector('#btn-home i'));
            const b2 = getComputedStyle(document.querySelector('#btn-home-ok'));
            const b3 = getComputedStyle(document.querySelector('#btn-home-info i'));
            return {
                btn_icon_color: b1.color,
                success_btn_bg: b2.backgroundColor,
                info_large_icon_size: b3.fontSize,
                info_large_icon_color: b3.color
            };
        }""")
        expected_2 = {
            "btn_icon_color": "rgb(255, 255, 255)",
            "success_btn_bg": "rgb(67, 172, 106)",
            "info_large_icon_size": "20px",
            "info_large_icon_color": "rgb(51, 51, 51)",
        }
        cases.append({
            "name": "icons_inside_buttons_and_links",
            "expected": expected_2,
            "actual": actual_2,
            "passed": actual_2 == expected_2,
        })

        # Case 3: .icon-bar.five-up horizontal widths & <label> layout
        actual_3 = page.evaluate("""() => {
            const items = [...document.querySelectorAll('#bar-five > .item')].map(el => Math.round(el.getBoundingClientRect().width));
            const lbl = document.querySelector('#bar-labeled .item label');
            const ic = document.querySelector('#bar-labeled .item i');
            const barBg = getComputedStyle(document.getElementById('bar-five')).backgroundColor;
            return {
                bar_bg: barBg,
                five_up_widths: items,
                icon_font_size: getComputedStyle(ic).fontSize,
                label_display: getComputedStyle(lbl).display,
                label_below_icon: lbl.getBoundingClientRect().top >= ic.getBoundingClientRect().bottom
            };
        }""")
        expected_3 = {
            "bar_bg": "rgb(51, 51, 51)",
            "five_up_widths": [160, 160, 160, 160, 160],
            "icon_font_size": "30px",
            "label_display": "block",
            "label_below_icon": True,
        }
        cases.append({
            "name": "icon_bar_five_up_and_labels",
            "expected": expected_3,
            "actual": actual_3,
            "passed": actual_3 == expected_3,
        })

        # Case 4: .disabled items and .vertical icon bar
        actual_4 = page.evaluate("""() => {
            const dis = getComputedStyle(document.getElementById('item-dis-share'));
            const vbar = document.getElementById('bar-vertical').getBoundingClientRect();
            const vitems = [...document.querySelectorAll('#bar-vertical > .item')].map(el => el.getBoundingClientRect());
            return {
                disabled_opacity: dis.opacity,
                disabled_pointer_events: dis.pointerEvents,
                disabled_cursor: dis.cursor,
                vertical_bar_width: Math.round(vbar.width),
                vertical_item_count: vitems.length,
                vertical_stacked: vitems[1].top > vitems[0].top && vitems[4].top > vitems[3].top,
                vertical_item_width: Math.round(vitems[0].width)
            };
        }""")
        expected_4 = {
            "disabled_opacity": "0.7",
            "disabled_pointer_events": "none",
            "disabled_cursor": "default",
            "vertical_bar_width": 112,
            "vertical_item_count": 5,
            "vertical_stacked": True,
            "vertical_item_width": 112,
        }
        cases.append({
            "name": "icon_bar_disabled_and_vertical",
            "expected": expected_4,
            "actual": actual_4,
            "passed": actual_4 == expected_4,
        })

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


if __name__ == "__main__":
    main()
在离线 Chromium 中核对 fi-* 图标字号(16/20/30/35px)与红色样式、按钮内图标色彩与字号继承、800px 容器下 five-up 每项 160px 与 label 下置,以及 disabled 不可点击态与 vertical 112px 垂直堆叠。

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

本地运行

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

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

本章附件与实例包

下载课程关联工程包:Foundation

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

下载课程关联工程包:Foundation

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

常见错误与边界检查

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

技术规范与官方资料

练习与参考解答

查看参考解答

Foundation Icons 本质是 Web 图标字体(Icon Font),图标通过伪元素字形渲染,因此直接受 CSS font-size 与 color 继承和覆写控制;给 .icon-bar 添加 .vertical 后,工具条宽度收敛为固定侧栏宽度(默认 7rem / 112px),内部每个 .item 取消水平浮动改为 100% 宽度纵向堆叠。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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