网页与 HTML

Unicode in CSS

CSS supports Unicode via escape sequences (\2713 for ✓), the content property for generated text, unicode-range for font subsetting, and the writing-mode property for vertical text.

What is Unicode in CSS?

CSS has multiple mechanisms for working with Unicode characters: escape sequences for inserting characters by code point, the content property for generated text, unicode-range for selective font loading, and properties that control writing direction and script rendering. Understanding these tools is essential for building multilingual, internationalized web interfaces.

CSS Unicode Escape Sequences

CSS uses a backslash-based escape syntax to insert Unicode characters by code point. The format is \ followed by one to six hexadecimal digits, optionally followed by a space:

/* Checkmark ✓ (U+2713) in generated content */
.item::before {
  content: "\2713  ";
}

/* Copyright symbol © (U+00A9) */
.footer::after {
  content: "\A9 2024 My Company";
}

/* Emoji via code point (🌍 = U+1F30D) */
.global::before {
  content: "\1F30D";
}

The trailing space after the hex digits is required if the next character is also a valid hex digit, to prevent ambiguity. Modern CSS also accepts the notation \{1F30D} with curly braces in some contexts, but browser support for this form is uneven.

The content Property and Unicode

The CSS content property (valid on ::before and ::after pseudo-elements) accepts literal Unicode strings or escape sequences. You can mix them:

.required::after {
  content: " \2733";  /* ✳ */
  color: red;
}

@font-face unicode-range

The unicode-range descriptor inside @font-face tells the browser to download a font file only when the page contains characters in the specified range. This is the backbone of efficient multilingual font loading — it prevents loading a full Japanese font just because a page has one Japanese character.

@font-face {
  font-family: "MyFont";
  src: url("latin.woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153;
}

@font-face {
  font-family: "MyFont";
  src: url("japanese.woff2");
  unicode-range: U+3000-9FFF, U+F900-FAFF;
}

The browser matches code points in the document against the declared ranges and downloads only the relevant font files.

Writing Mode and Direction

CSS controls text flow direction through three core properties:

/* Horizontal top-to-bottom (default) */
.latin { writing-mode: horizontal-tb; }

/* Vertical right-to-left (Traditional Chinese, Japanese) */
.vertical-cjk { writing-mode: vertical-rl; }

/* Right-to-left base direction (Arabic, Hebrew) */
.arabic { direction: rtl; }

CSS Counters with Non-Latin Numerals

The list-style-type property and counter() function support dozens of non-Latin numeral systems:

ol { list-style-type: cjk-decimal; }      /* CJK numerals: 一, 二, 三 */
ol { list-style-type: arabic-indic; }     /* ١, ٢, ٣ */
ol { list-style-type: devanagari; }       /* १, २, ३ */

Quick Facts

Feature CSS Property / Syntax
Insert by code point content: "\2713" (1–6 hex digits)
Selective font loading @font-face { unicode-range: U+... }
Writing direction direction: rtl \| ltr
Vertical text writing-mode: vertical-rl \| vertical-lr
Non-Latin list numbers list-style-type: devanagari \| arabic-indic \| ...
Bidi control unicode-bidi: isolate \| embed \| override
Font rendering font-variant (OpenType feature access)

相关术语

网页与 HTML 中的更多内容

Content-Type 字符集

声明响应字符编码的HTTP头参数(Content-Type: text/html; charset=utf-8),优先级高于文档内的编码声明。

CSS content 属性

通过::before和::after伪元素使用Unicode转义插入生成内容的CSS属性:content: '\2713'可插入✓。

CSS Text Direction

CSS properties (direction, writing-mode, unicode-bidi) controlling text layout direction. Works with Unicode …

HTML 实体

HTML中字符的文本表示方式,有三种形式:命名(&)、十进制(&)、十六进制(&),对于与HTML语法冲突的字符是必需的。

JavaScript Intl API

ECMAScript Internationalization API providing locale-aware string comparison (Collator), number formatting (NumberFormat), date …

Punycode

将Unicode域名转换为xn--前缀ASCII字符串的ASCII兼容编码,例如münchen.de → xn--mnchen-3ya.de。

XML 字符引用

XML版本的数字字符引用:✓或✓,XML只有5个命名实体(& < > " '),而HTML5有2,231个。

变体选择符

选择特定字形变体的字符(U+FE00–U+FE0F、U+E0100–U+E01EF),VS15(U+FE0E)表示文本呈现,VS16(U+FE0F)表示表情符号呈现。

命名字符引用

使用人类可读名称的HTML实体:© → ©,— → —。HTML5定义了2,231个命名引用,且区分大小写。

国际化域名 (IDN)

包含非ASCII Unicode字符的域名,内部以Punycode(xn--...)存储,但向用户显示为Unicode,安全隐患:同形字攻击。