CSS text-spacing-trim Property
The CSS text-spacing-trim property controls spacing around certain punctuation characters used in Chinese, Japanese, and Korean (CJK) typography.
Some CJK punctuation characters occupy a full-width character box even though their visible glyphs use only part of that space. This property lets browsers adjust the spacing around opening punctuation, closing punctuation, and punctuation at the beginning or end of a line. Its effects depend on the characters, fonts, and browser implementation.
text-spacing-trim Syntax
The text-spacing-trim property accepts one keyword that determines how spacing around supported CJK punctuation is handled.
Basic Syntax
.example {
text-spacing-trim: trim-start;
}
Available Values
.example {
text-spacing-trim: normal;
}
.example {
text-spacing-trim: space-all;
}
.example {
text-spacing-trim: space-first;
}
.example {
text-spacing-trim: trim-start;
}
.example {
text-spacing-trim: trim-both;
}
.example {
text-spacing-trim: trim-all;
}
CSS-Wide Keywords
.example {
text-spacing-trim: initial;
}
.example {
text-spacing-trim: inherit;
}
.example {
text-spacing-trim: unset;
}
.example {
text-spacing-trim: revert;
}
.example {
text-spacing-trim: revert-layer;
}
The initial value is normal. The property is inherited, so unset behaves like inherit.
Valid Examples
.japanese-text {
text-spacing-trim: normal;
}
.japanese-heading {
text-spacing-trim: trim-start;
}
.japanese-paragraph {
text-spacing-trim: trim-both;
}
.cjk-example {
text-spacing-trim: space-all;
}
Invalid Examples
.example {
text-spacing-trim: 10px;
}
.example {
text-spacing-trim: 50%;
}
.example {
text-spacing-trim: none;
}
.example {
text-spacing-trim: trim;
}
.example {
text-spacing-trim: trim-start trim-both;
}
Lengths, percentages, none, trim, and combinations of multiple keywords are not valid values.
Property Values
| Value | Description |
|---|---|
normal |
Uses the browser's normal punctuation-spacing behavior, which can include language-specific adjustments. |
space-all |
Preserves the full spacing around affected punctuation instead of applying automatic trimming. |
space-first |
Preserves full-width spacing for opening punctuation at the beginning of the first line while allowing other specified punctuation adjustments. |
trim-start |
Trims the spacing before opening punctuation at the beginning of a line. |
trim-both |
Applies line-start trimming and additional punctuation-spacing adjustments at line boundaries. |
trim-all |
Applies more extensive trimming to eligible punctuation, including punctuation within lines. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | normal |
| Inherited | Yes |
| Applies to | Text elements |
| Computed value | Specified keyword |
| Animation type | Discrete |
| Percentages | Not accepted |
| Specification | CSS Text Module Level 4 |
Note: The property controls spacing adjustments around specific punctuation categories. It does not remove punctuation characters or modify the underlying text.
How It Works
CJK typography often uses full-width punctuation characters. Some of these characters include built-in blank space that may be adjusted for more balanced text layout.
Step 1: Create CJK Text
<p lang="ja" class="japanese-text">「こんにちは」、世界!</p>
The text contains Japanese opening and closing punctuation.
Step 2: Apply a Spacing Rule
.japanese-text {
text-spacing-trim: trim-start;
}
Where supported, the browser adjusts eligible opening punctuation at the beginning of a line.
Step 3: Compare Another Value
.japanese-text {
text-spacing-trim: space-all;
}
This requests that the browser preserve full spacing around affected punctuation.
Step 4: Apply More Extensive Trimming
.japanese-text {
text-spacing-trim: trim-all;
}
This requests more extensive punctuation-spacing adjustments.
Important Consideration
The visible result depends on the punctuation characters, font metrics, line breaks, and browser support. Some examples may look identical even when different values are specified.
The normal Value
The normal value allows the browser to use its default punctuation-spacing behavior.
Basic Example
.japanese-text {
text-spacing-trim: normal;
}
Default Behavior
Because normal is the initial value, it is normally unnecessary to declare it explicitly.
Browser-Dependent Adjustments
The browser may apply punctuation-spacing rules according to its implementation and the text's language.
Restoring Normal Behavior
.article {
text-spacing-trim: trim-all;
}
.article .standard-text {
text-spacing-trim: normal;
}
This restores normal punctuation-spacing behavior for the selected content.
When to Use normal
Use normal when browser defaults are acceptable or when there is no specific typographic requirement for punctuation trimming.
The space-all Value
The space-all value preserves full spacing around eligible punctuation characters.
Basic Example
.japanese-text {
text-spacing-trim: space-all;
}
Preserving Punctuation Spacing
Full-width punctuation retains its spacing instead of being automatically trimmed by the rules controlled by this property.
HTML Example
<p lang="ja" class="preserve-spacing">「日本語の文章です。」</p>
CSS Example
.preserve-spacing {
text-spacing-trim: space-all;
}
When to Use space-all
Use this value when preserving full punctuation spacing is preferred over automatic trimming.
Important Consideration
Full spacing can produce noticeable gaps around punctuation, especially at line boundaries.
The space-first Value
The space-first value preserves full-width spacing for opening punctuation at the beginning of the first line while allowing other punctuation-spacing adjustments.
Basic Example
.japanese-paragraph {
text-spacing-trim: space-first;
}
First-Line Behavior
The beginning of the first line receives special treatment when it starts with eligible opening punctuation.
HTML Example
<p lang="ja" class="japanese-paragraph">「これは日本語の文章です。」</p>
CSS Example
.japanese-paragraph {
text-spacing-trim: space-first;
}
Comparison With trim-start
The trim-start value trims eligible opening punctuation at line starts, while space-first preserves the full spacing of opening punctuation at the beginning of the first line.
When to Use space-first
Use this value when the first line's opening punctuation should retain its full spacing while other punctuation adjustments remain available.
The trim-start Value
The trim-start value removes eligible spacing before opening punctuation at the beginning of a line.
Basic Example
.japanese-heading {
text-spacing-trim: trim-start;
}
Opening Punctuation
Opening brackets and quotation marks in CJK typography may include blank space before the visible glyph.
HTML Example
<h2 lang="ja" class="japanese-heading">「ウェブデザイン」</h2>
CSS Example
.japanese-heading {
text-spacing-trim: trim-start;
}
Visual Alignment
Trimming eligible leading spacing can help opening punctuation align more closely with the start of a text block.
Important Consideration
The property trims punctuation spacing, not the element's margin or padding.
The trim-both Value
The trim-both value applies punctuation-spacing adjustments at both line boundaries, including trimming eligible opening punctuation at line starts.
Basic Example
.japanese-paragraph {
text-spacing-trim: trim-both;
}
Line Boundary Adjustments
The browser adjusts eligible punctuation spacing according to the rules for this value, helping avoid unnecessary gaps at line boundaries.
HTML Example
<p lang="ja" class="japanese-paragraph">「文章の始まりです。」次の文章です。</p>
CSS Example
.japanese-paragraph {
text-spacing-trim: trim-both;
}
Comparison With trim-start
The trim-start value focuses on leading punctuation spacing, while trim-both applies additional adjustments at line boundaries.
When to Use trim-both
Use this value when punctuation spacing at line boundaries should be more tightly controlled.
The trim-all Value
The trim-all value requests more extensive punctuation-spacing adjustments, including eligible punctuation within lines.
Basic Example
.japanese-text {
text-spacing-trim: trim-all;
}
Interior Punctuation
Unlike values that focus primarily on line boundaries, trim-all can adjust spacing around eligible punctuation within a line.
HTML Example
<p lang="ja" class="compact-punctuation">「HTML」、「CSS」、そして「JavaScript」。</p>
CSS Example
.compact-punctuation {
text-spacing-trim: trim-all;
}
Compact Typography
More extensive trimming can reduce excess spacing around punctuation in supported CJK text.
Important Consideration
Not every punctuation character is affected. The specification defines which character categories are eligible for adjustment.
CJK Punctuation and Spacing
Chinese, Japanese, and Korean typography includes punctuation characters that may occupy a full-width character box.
Opening Punctuation
Examples include opening quotation marks and brackets such as 「, 『, and (.
Closing Punctuation
Examples include closing quotation marks and brackets such as 」, 』, and ).
Other Punctuation
Some punctuation marks, including the Japanese comma 、 and full stop 。, have spacing characteristics that differ from ordinary Latin punctuation.
Full-Width Character Boxes
The visible punctuation glyph may occupy only part of its character box, leaving space that can be adjusted during text layout.
Language Identification
<p lang="ja">「日本語の文章です。」</p>
<p lang="zh">「中文文字示例。」</p>
<p lang="ko">「한국어 문장입니다.」</p>
Use the appropriate lang attribute so browsers and assistive technologies can identify the language.
Font Differences
Fonts may position punctuation glyphs differently within their character boxes, affecting the visible results of trimming.
Important Consideration
The property is intended for specific punctuation-spacing behavior, not general character spacing throughout a paragraph.
Line Boundaries
Several text-spacing-trim values distinguish between punctuation at line boundaries and punctuation within a line.
Beginning of a Line
Opening punctuation may contain blank space before its visible glyph. Trimming can reduce that leading space.
End of a Line
Closing punctuation may contain blank space after its visible glyph. Certain trimming rules can adjust that spacing.
Within a Line
Some punctuation-spacing adjustments affect adjacent punctuation characters inside a line rather than only at its boundaries.
Line Wrapping
.japanese-text {
max-width: 20em;
text-spacing-trim: trim-both;
}
The available width determines where lines wrap, which can change which punctuation characters appear at line boundaries.
Responsive Layouts
As the viewport width changes, line breaks can move and punctuation-spacing adjustments may become more or less noticeable.
Important Consideration
Do not assume a punctuation character will always appear at the same line position across fonts, devices, and screen widths.
Writing Modes
CJK text may be displayed horizontally or vertically. Punctuation positioning and spacing can vary with the writing mode.
Horizontal Writing
.horizontal-text {
writing-mode: horizontal-tb;
text-spacing-trim: trim-start;
}
Vertical Writing
.vertical-text {
writing-mode: vertical-rl;
text-spacing-trim: trim-start;
}
HTML Example
<p lang="ja" class="vertical-text">「縦書きの文章です。」</p>
CSS Example
.vertical-text {
writing-mode: vertical-rl;
text-spacing-trim: trim-start;
height: 220px;
}
Relationship to text-orientation
The text-orientation property controls glyph orientation in vertical writing modes. The text-spacing-trim property controls eligible punctuation spacing.
Important Consideration
Support for punctuation trimming in vertical writing modes may vary. Test the actual combination of browser, font, and writing mode.
Related CSS Properties
Several CSS properties affect text spacing, line breaking, and CJK typography.
letter-spacing
.example {
letter-spacing: 0.05em;
}
Adjusts spacing between typographic characters more generally.
word-spacing
.example {
word-spacing: 0.2em;
}
Adjusts spacing between words or word-separator characters.
line-height
.example {
line-height: 1.7;
}
Controls the height of line boxes.
word-break
.example {
word-break: normal;
}
Controls opportunities for breaking text within words.
line-break
.example {
line-break: strict;
}
Controls line-breaking rules, particularly for CJK text.
writing-mode
.example {
writing-mode: vertical-rl;
}
Controls whether text flows horizontally or vertically.
text-orientation
.example {
text-orientation: mixed;
}
Controls the orientation of characters in vertical writing modes.
Comparison
| Property | Purpose |
|---|---|
text-spacing-trim |
Adjusts spacing around eligible CJK punctuation. |
letter-spacing |
Adjusts spacing between typographic characters. |
word-spacing |
Adjusts spacing between words. |
line-break |
Controls line-breaking rules. |
writing-mode |
Controls text flow direction. |
text-orientation |
Controls glyph orientation in vertical text. |
Practical Examples
Default CJK Punctuation Spacing
.japanese-text {
text-spacing-trim: normal;
}
Preserve Full Punctuation Spacing
.japanese-text {
text-spacing-trim: space-all;
}
Preserve First-Line Opening Spacing
.japanese-text {
text-spacing-trim: space-first;
}
Trim Leading Punctuation Spacing
.japanese-heading {
text-spacing-trim: trim-start;
}
Trim Punctuation at Line Boundaries
.japanese-paragraph {
text-spacing-trim: trim-both;
}
Apply Extensive Punctuation Trimming
.japanese-paragraph {
text-spacing-trim: trim-all;
}
Japanese Article Typography
.japanese-article {
font-family: "Noto Sans CJK JP", sans-serif;
line-height: 1.8;
text-spacing-trim: trim-start;
}
Vertical Japanese Text
.vertical-japanese {
writing-mode: vertical-rl;
text-orientation: mixed;
text-spacing-trim: trim-start;
}
Restore Normal Spacing
.article {
text-spacing-trim: trim-all;
}
.article .standard-text {
text-spacing-trim: normal;
}
Feature Detection
@supports (text-spacing-trim: trim-start) {
.japanese-heading {
text-spacing-trim: trim-start;
}
}
The feature query applies the declaration only when the browser recognizes the specified value.
text-spacing-trim Example
This example compares the available values using Japanese text. The samples use the same font size and layout so differences in punctuation spacing can be examined where the property is supported.
HTML Example
<div class="text-spacing-trim-demo">
<div class="spacing-sample">
<strong>normal</strong>
<p lang="ja" class="spacing-normal">「日本語の文章です。」</p>
</div>
<div class="spacing-sample">
<strong>space-all</strong>
<p lang="ja" class="spacing-all">「日本語の文章です。」</p>
</div>
<div class="spacing-sample">
<strong>space-first</strong>
<p lang="ja" class="spacing-first">「日本語の文章です。」</p>
</div>
<div class="spacing-sample">
<strong>trim-start</strong>
<p lang="ja" class="spacing-start">「日本語の文章です。」</p>
</div>
<div class="spacing-sample">
<strong>trim-both</strong>
<p lang="ja" class="spacing-both">「日本語の文章です。」</p>
</div>
<div class="spacing-sample">
<strong>trim-all</strong>
<p lang="ja" class="spacing-trim-all">「日本語の文章です。」</p>
</div>
</div>
CSS Example
.text-spacing-trim-demo {
display: grid;
gap: 12px;
}
.text-spacing-trim-demo .spacing-sample {
padding: 14px;
border: 1px solid #cbd5e1;
background: #f8fafc;
}
.text-spacing-trim-demo strong {
display: block;
margin-bottom: 8px;
color: #334155;
font-size: 14px;
}
.text-spacing-trim-demo p {
margin: 0;
color: #1f2937;
font-size: 22px;
line-height: 1.6;
}
.text-spacing-trim-demo .spacing-normal {
text-spacing-trim: normal;
}
.text-spacing-trim-demo .spacing-all {
text-spacing-trim: space-all;
}
.text-spacing-trim-demo .spacing-first {
text-spacing-trim: space-first;
}
.text-spacing-trim-demo .spacing-start {
text-spacing-trim: trim-start;
}
.text-spacing-trim-demo .spacing-both {
text-spacing-trim: trim-both;
}
.text-spacing-trim-demo .spacing-trim-all {
text-spacing-trim: trim-all;
}
「日本語の文章です。」
「日本語の文章です。」
「日本語の文章です。」
「日本語の文章です。」
「日本語の文章です。」
「日本語の文章です。」
Understanding the Results
In a supporting browser, eligible punctuation may have different spacing under each value. In browsers without support, the samples may appear identical.
Testing Line Wrapping
.text-spacing-trim-demo p {
max-width: 10em;
}
Reducing the available width can create additional line boundaries where punctuation trimming becomes more noticeable.
Testing a Different Font
.text-spacing-trim-demo p {
font-family: "Noto Serif CJK JP", serif;
}
Different fonts can produce different punctuation-spacing results because their glyph metrics vary.
Important Consideration
Browser support for this property is limited. The example is intended to demonstrate the syntax and provide a comparison where the feature is implemented.
Accessibility Considerations
Punctuation spacing can influence the readability of CJK text, particularly in long passages and narrow layouts.
Preserve the Original Characters
Use CSS to control spacing rather than removing punctuation characters or replacing them with decorative elements.
Identify the Language
<p lang="ja">「日本語の文章です。」</p>
The lang attribute helps assistive technologies identify the language of the text.
Maintain Readable Typography
Use appropriate font sizes, line heights, and text widths regardless of punctuation-spacing adjustments.
Avoid Excessive Compaction
More extensive trimming is not necessarily better. Choose spacing that remains visually clear for the intended language and design.
Test With Different Fonts
Font differences can affect the appearance of punctuation and the amount of visible spacing.
Test Narrow Viewports
Line wrapping changes which punctuation characters appear at line boundaries.
Do Not Depend on the Property for Meaning
Text should remain understandable when the browser ignores the declaration.
Use Appropriate Line-Breaking Rules
Consider the line-break property when language-specific line-breaking behavior is important.
Test Vertical Text
Vertical writing modes may require additional testing for punctuation positioning and readability.
Common Mistakes
Using the none Keyword
.example {
text-spacing-trim: none;
}
The none keyword is not valid. Use space-all when full punctuation spacing should be preserved.
Using Length Values
.example {
text-spacing-trim: 5px;
}
The property accepts keywords, not lengths.
Using Percentage Values
.example {
text-spacing-trim: 50%;
}
Percentages are not accepted.
Combining Multiple Keywords
.example {
text-spacing-trim: trim-start trim-all;
}
Only one value can be specified.
Confusing It With letter-spacing
The letter-spacing property controls spacing between typographic characters generally. The text-spacing-trim property targets specific punctuation-spacing behavior.
Expecting Latin Punctuation to Change
The property is designed for eligible CJK punctuation and does not provide general spacing control for ordinary Latin punctuation.
Assuming Every CJK Character Is Affected
The property does not trim spacing around all CJK characters.
Ignoring Browser Support
Unsupported browsers ignore the declaration.
Expecting Identical Results Across Fonts
Different fonts can have different punctuation metrics.
Confusing Line Trimming With Element Padding
The property does not remove margins or padding from the containing element.
Ignoring Line Wrapping
Line breaks influence which punctuation characters are eligible for boundary adjustments.
Using the Property as a Replacement for Responsive Design
Responsive widths, readable fonts, and appropriate line heights remain necessary.
Important Considerations
Keep the following points in mind when using text-spacing-trim:
- The property controls spacing around eligible CJK punctuation.
- The initial value is
normal. - The property is inherited.
- It accepts one keyword value at a time.
- The available values are
normal,space-all,space-first,trim-start,trim-both, andtrim-all. - Lengths and percentages are not accepted.
- The
nonekeyword is not valid. - The property does not remove punctuation characters.
- It does not directly change the element's margins or padding.
- Some values affect punctuation at line boundaries.
- More extensive trimming can affect eligible punctuation within lines.
- Line wrapping can change the visible result.
- Font metrics influence punctuation spacing.
- The property's effects depend on the punctuation characters present.
- It is intended primarily for CJK typography.
- It does not replace
letter-spacingorword-spacing. - It does not replace the
line-breakproperty. - It can be combined with horizontal or vertical writing modes.
- Browser support is limited.
- Unsupported browsers ignore the declaration.
- Use the appropriate
langattribute for multilingual content. - Test the property with the actual fonts and languages used on the website.
Browser Support
The CSS text-spacing-trim property has limited availability. Support for its values is not consistent across browsers, and some implementations may support only part of the specification.
Because the property is intended for specialized CJK typography, its effects may not be noticeable in ordinary English-language content.
For current compatibility information, visit Can I Use? and search for text-spacing-trim.
Basic Declaration
.japanese-text {
text-spacing-trim: trim-start;
}
Feature Detection
@supports (text-spacing-trim: trim-start) {
.japanese-text {
text-spacing-trim: trim-start;
}
}
This applies the rule only when the browser recognizes the specified declaration.
Checking Another Value
@supports (text-spacing-trim: trim-all) {
.japanese-text {
text-spacing-trim: trim-all;
}
}
Checking individual values can be useful because browser implementations may not support every value equally.
Fallback Behavior
Browsers that do not recognize the property ignore the declaration and use their existing punctuation-spacing behavior.
Testing Recommendations
Test actual CJK text in the target browsers, including different fonts, line lengths, and writing modes. Feature detection confirms syntax recognition but does not guarantee identical visual results.
Specifications
The text-spacing-trim property is defined in the CSS Text Module Level 4 specification.
CSS Text Module Level 4 - text-spacing-trim Property
The specification defines the available values and the rules for adjusting spacing around eligible punctuation characters.
For additional documentation and examples, consult MDN Web Docs.
