CSS content 属性
通过::before和::after伪元素使用Unicode转义插入生成内容的CSS属性:content: '\2713'可插入✓。
What Is the CSS content Property?
The CSS content property inserts generated content into a document via the ::before and ::after pseudo-elements. One of its most powerful uses is inserting Unicode characters using the \XXXXXX hex notation, allowing decorative symbols, icons, arrows, and glyphs to be added purely through stylesheets — without modifying the HTML.
The content property only applies to ::before and ::after pseudo-elements (and a few other generated content contexts like ::marker).
Unicode Notation in CSS
In CSS string values, Unicode escape sequences use a backslash followed by one to six hexadecimal digits:
/* These all insert the CHECK MARK ✓ (U+2713) */
.done::before {
content: "\2713"; /* 4-digit hex */
content: "\002713"; /* 6-digit hex */
content: "✓"; /* direct UTF-8 character */
}
CSS Unicode escapes are terminated by: the end of the hex sequence (max 6 digits), an optional whitespace character (which is consumed and not displayed), or any non-hex character.
/* Terminating space is consumed — not rendered */
.icon::before {
content: "\2713 Check"; /* renders: ✓Check (space consumed) */
}
/* No terminating space needed when followed by non-hex */
.icon::before {
content: "\2713!"; /* renders: ✓! */
}
Common Use Cases
/* Quotation marks */
q::before { content: "\201C"; } /* " (LEFT DOUBLE QUOTATION MARK) */
q::after { content: "\201D"; } /* " (RIGHT DOUBLE QUOTATION MARK) */
/* Arrows for navigation */
.next::after { content: " \2192"; } /* → */
.prev::before { content: "\2190 "; } /* ← */
/* Decorative bullets */
.feature-list li::before {
content: "\2022"; /* • BULLET */
margin-right: 0.5em;
}
/* Warning icon */
.warning::before {
content: "\26A0\FE0F"; /* ⚠️ (warning + emoji variation selector) */
}
/* External link indicator */
a[href^="http"]::after {
content: " \2197"; /* ↗ NORTH EAST ARROW */
font-size: 0.8em;
}
/* Counter combined with Unicode */
ol.custom {
counter-reset: section;
}
ol.custom li::before {
counter-increment: section;
content: counter(section) "\FE0F\20E3"; /* Keycap emoji sequence */
}
Font Dependency
The inserted character renders using the element's computed font-family. If the font does not include a glyph for the specified code point, the browser falls back through the font stack. For full Unicode coverage, include a system emoji font or a comprehensive symbol font:
.icon::before {
content: "\1F4A1"; /* 💡 LIGHT BULB */
font-family: "Segoe UI Emoji", "Apple Color Emoji", "Noto Color Emoji", sans-serif;
}
Accessibility Considerations
Content generated with the content property is read by most screen readers (unless alt is set to empty string). For decorative characters, suppress them:
/* Decorative only — hide from screen readers */
.separator::before {
content: "\2022";
speak: never; /* CSS Speech module (limited support) */
/* Or use aria-hidden="true" on the element itself */
}
/* Meaningful content — provide alt text (CSS4) */
.external::after {
content: "\2197" / "external link";
}
Quick Facts
| Property | Value |
|---|---|
| Applies to | ::before and ::after pseudo-elements |
| Unicode syntax | \XXXXXX (1–6 hex digits) |
| Terminator | Optional single whitespace after hex sequence |
| Direct characters | Allowed (UTF-8 source) |
| Max code point | U+10FFFF (6 hex digits) |
| Accessibility | Exposed to screen readers by default; use / "" alt to suppress |
| Font required | Character must have a glyph in the computed font stack |
网页与 HTML 中的更多内容
声明响应字符编码的HTTP头参数(Content-Type: text/html; charset=utf-8),优先级高于文档内的编码声明。
CSS properties (direction, writing-mode, unicode-bidi) controlling text layout direction. Works with Unicode …
HTML中字符的文本表示方式,有三种形式:命名(&)、十进制(&)、十六进制(&),对于与HTML语法冲突的字符是必需的。
ECMAScript Internationalization API providing locale-aware string comparison (Collator), number formatting (NumberFormat), date …
将Unicode域名转换为xn--前缀ASCII字符串的ASCII兼容编码,例如münchen.de → xn--mnchen-3ya.de。
CSS supports Unicode via escape sequences (\2713 for ✓), the content property …
XML版本的数字字符引用:✓或✓,XML只有5个命名实体(& < > " '),而HTML5有2,231个。
选择特定字形变体的字符(U+FE00–U+FE0F、U+E0100–U+E01EF),VS15(U+FE0E)表示文本呈现,VS16(U+FE0F)表示表情符号呈现。
使用人类可读名称的HTML实体:© → ©,— → —。HTML5定义了2,231个命名引用,且区分大小写。
包含非ASCII Unicode字符的域名,内部以Punycode(xn--...)存储,但向用户显示为Unicode,安全隐患:同形字攻击。