首页›Lea Verou›文章

The best way to use icons in Web UIs

Lea Verou · 2026年10月11日

AI 摘要 AI 生成,可能有误

文章认为,目前向 Web UI 添加图标的主流方式——内联 SVG 标记和图标字体——要么笨重且充满样板代码,要么存在下载体积大、可访问性差等诸多问题。作者提出了一套纯 CSS 系统:图标作为设计令牌定义为自定义属性(例如 --icon-trash),并通过伪元素结合 CSS 遮罩(mask)进行渲染,这样图标会自动跟随文本颜色,可以按组件分配(例如 .delete 按钮默认带一个垃圾桶图标),并且能根据选择器、状态或媒体查询响应式地变化,而无需改动标记代码。这种方法让简单场景保持简单,复杂覆盖仍然可行,并避免了可用性上的断崖式下跌;唯一仍需要内联 SVG 的情况,是需要对 SVG 本身进行动态变形的场景。

TL;DR? Jump to demo

I’ve always had a soft spot for icons1. Not only do they add polish to a UI, they can also tangibly improve usability. Familiar icons help users find what they need quickly, since we recognize pictures much faster than we read words.

And as a web architect at heart, the current dominant ways we have to include icons on the Web make me sad. While there are many, many different ways to include icons in a Web UI, each with different performance characteristics, by far the most common ones involve adding inline SVG to the HTML. For example, a trash icon might be included like this:

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 Lea Verou

Nudeps: bundler-like imports, none of the baggage

AI 摘要Nudeps is a lightweight build tool that lets developers keep using npm install and import dependencies by bare specifiers (like "vue") directly in the browser, without a bundler. It works by copying dependency files from node_modules into a deployable client_modules directory and generating an import map (using JSPM under the hood), touching only dependencies and never the developer's own code, so it requires no watcher and complements any other tooling. It supports many edge cases like dynamic imports, git dependencies, and CJS packages via a require() shim, but doesn't transform code (TypeScript/JSX still need their own tools), is npm-only for now, and may load many-small-module packages more slowly since it doesn't bundle.

Candidate Statement for the 2026 W3C TAG Election

AI 摘要In her candidate statement for the 2026 W3C TAG election, Lea Verou announces she is running to return to the TAG after serving two terms (2021–2025), with her participation this time fully funded by Google, Microsoft, and the OpenJS Foundation. She highlights her broad web platform experience and past TAG contributions, including the Web Platform Design Principles, API design reviews, and the Web Platform Gaps initiative. Her central platform is for the TAG to proactively shape the Web's architecture rather than just review it, including treating the rise of AI agents that write and consume web code as an architecture question guided by principles and evidence.

The best dark mode toggle is probably none

AI 摘要In this follow-up to her earlier article, the author argues that the best dark mode toggle is no toggle at all, though if one is required, a two-state control alternating between system and the opposite theme beats a tri-state (system/light/dark) one. She rebuts Bramus's objections—such as the OS auto-switching edge case—contending that the failure scenario is rare and cheaply recoverable, that polls of developers measure preference rather than usability, and that tri-state controls add unnecessary cognitive load by exposing the implementation model instead of the user's actual goal. The piece also recounts the widespread positive reception of the original article and the alternative designs the ensuing debate inspired.

Dark mode toggles: two states are enough

AI 摘要The article argues that tri-state dark mode toggles (Light/Dark/System) leak the underlying data model into the UI and solve a largely imaginary user goal, since real users only seek out the toggle when something looks wrong. It advocates a well-designed two-state toggle that shows the currently resolved value and alternates between storing an explicit override and reverting to the system default, capable of expressing all three underlying states. Key implementation rules include only evaluating the stored override at user interaction, keeping it even if the OS preference later coincides with it, since any resulting confusion is rare and fixable with a single click.

In defense of polyfills

AI 摘要The article argues that polyfills are a net positive for the Web platform, responding to Anne van Kesteren's view at a WHATWG meeting that polyfilling is harmful because it restricts native API design flexibility. The author contends that the need for fallback code exists regardless of polyfills, and that polyfills are superior to ad-hoc alternatives because they are centrally maintainable and decouple standardized API design from implementation. Proposed alternatives—separate namespaces, dropping feature detection, and ponyfills—are dismissed as repeating the failed vendor prefix experiment or trading away real benefits like performance and accessibility for a largely theoretical risk.