CSS text-box-edge Property
The CSS text-box-edge property defines the font-based boundaries used when trimming extra space from the block-start and block-end edges of a text container.
It works with text-box-trim to control how closely a text container's edges follow selected font metrics, such as cap height, x-height, and the alphabetic baseline. The text-box shorthand can set both properties in one declaration.
The property is useful for precise typography in headings, labels, buttons, and other interface elements. Setting text-box-edge alone does not enable trimming.
text-box-edge Syntax
The text-box-edge property accepts one or two font-metric keywords. These define the over and under edges used when text box trimming is enabled.
Basic Syntax
h2 {
text-box-edge: cap alphabetic;
}
This selects the font's cap-height metric for the over edge and its alphabetic baseline for the under edge.
Common Values
h2 {
text-box-edge: auto;
}
h2 {
text-box-edge: text;
}
h2 {
text-box-edge: cap;
}
h2 {
text-box-edge: ex;
}
h2 {
text-box-edge: cap alphabetic;
}
h2 {
text-box-edge: ex alphabetic;
}
h2 {
text-box-edge: text text;
}
h2 {
text-box-edge: cap text;
}
CSS-Wide Keywords
h2 {
text-box-edge: initial;
}
h2 {
text-box-edge: inherit;
}
h2 {
text-box-edge: unset;
}
h2 {
text-box-edge: revert;
}
h2 {
text-box-edge: revert-layer;
}
The initial value is auto. The property is inherited, so descendants can use the edge metrics established by their ancestors.
Using It With text-box-trim
h2 {
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
The first declaration defines the boundaries. The second activates trimming at both outer edges of the text container.
Property Values
The supported values describe font metrics rather than fixed distances.
| Value | Description |
|---|---|
auto |
Uses the automatic edge selection defined by the specification. |
text |
Uses the font's text-over and text-under metrics when specified as a single value. |
cap |
Uses the font's cap-height metric for the over edge. |
ex |
Uses the font's x-height metric for the over edge. |
alphabetic |
Uses the alphabetic baseline as the under-edge metric. |
ideographic |
Uses the ideographic under-edge metric. |
cap alphabetic |
Uses cap height for the over edge and the alphabetic baseline for the under edge. |
ex alphabetic |
Uses x-height for the over edge and the alphabetic baseline for the under edge. |
text text |
Explicitly uses the font's text-over and text-under metrics. |
cap text |
Uses cap height for the over edge and the text-under metric for the under edge. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | auto |
| Inherited | Yes |
| Applies to | Elements participating in inline formatting |
| Computed value | As specified |
| Animation type | Discrete |
| Percentages | Not accepted |
| Related properties | text-box-trim, text-box, line-height |
| Specification | CSS Inline Layout Module Level 3 |
Valid Examples
.heading {
text-box-edge: auto;
}
.heading {
text-box-edge: text;
}
.heading {
text-box-edge: cap;
}
.heading {
text-box-edge: ex alphabetic;
}
.heading {
text-box-edge: cap alphabetic;
}
.heading {
text-box-edge: text ideographic;
}
Invalid Examples
.heading {
text-box-edge: center;
}
.heading {
text-box-edge: 10px;
}
.heading {
text-box-edge: 50%;
}
.heading {
text-box-edge: middle;
}
.heading {
text-box-edge: alphabetic cap;
}
The property does not accept alignment keywords, arbitrary lengths, or percentages. In a two-value declaration, the over-edge value must precede the under-edge value.
How It Works
A font contains measurements that determine how text is positioned within a line. These measurements can create space above and below the visible characters.
The text-box-edge property selects the metrics used as trimming boundaries. The text-box-trim property determines whether those boundaries are applied.
Step 1: Create a Heading
<h2 class="metric-heading">CSS Typography</h2>
Step 2: Apply Typography
.metric-heading {
margin: 0;
font-size: 36px;
line-height: 1.5;
}
The heading initially uses its normal text box, including space determined by font metrics and line height.
Step 3: Select the Edge Metrics
.metric-heading {
margin: 0;
font-size: 36px;
line-height: 1.5;
text-box-edge: cap alphabetic;
}
The edge metrics are now defined, but trimming has not been enabled.
Step 4: Enable Trimming
.metric-heading {
margin: 0;
font-size: 36px;
line-height: 1.5;
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
The browser trims the selected outer text-box edges according to the specified metrics.
Step 5: Compare the Result
The visible characters retain their original font size, but the text container may occupy less space along the block axis.
Important Consideration
The amount of trimming depends on the font's metrics. It is not calculated by measuring the visible pixels of individual characters.
Over-Edge Values
The over edge represents the font-metric boundary on the over side of the text. In ordinary horizontal writing, this corresponds to the upper side of the text.
The text Value
.heading {
text-box-edge: text;
}
Uses the font's text-over and text-under metrics when specified alone.
The cap Value
.heading {
text-box-edge: cap alphabetic;
}
The cap keyword selects the font's capital-letter height as the over-edge metric.
The ex Value
.heading {
text-box-edge: ex alphabetic;
}
The ex keyword selects the font's x-height metric, which is generally associated with the height of lowercase letters.
The ideographic-ink Value
.heading {
text-box-edge: ideographic-ink ideographic-ink;
}
The ideographic-ink keyword uses an ideographic ink boundary. It can be relevant when working with writing systems that use ideographic characters.
Choosing an Over Edge
Use cap when the desired boundary follows the font's capital-letter height. Use ex when the x-height is more appropriate. Use text-based or ideographic metrics when they better match the writing system and design requirements.
Important Consideration
Some glyphs, including accented capital letters, can extend beyond the selected cap-height or x-height boundary.
Under-Edge Values
The under edge represents the font-metric boundary on the opposite side of the text. In ordinary horizontal writing, this corresponds to the lower side.
The alphabetic Value
.heading {
text-box-edge: cap alphabetic;
}
The alphabetic keyword selects the alphabetic baseline as the under-edge boundary.
The text Value
.heading {
text-box-edge: cap text;
}
Uses the font's text-under metric for the under edge.
The ideographic Value
.heading {
text-box-edge: text ideographic;
}
The ideographic keyword selects the ideographic under-edge metric.
The ideographic-ink Value
.heading {
text-box-edge: ideographic-ink ideographic-ink;
}
Uses ideographic ink metrics for both selected boundaries.
Choosing an Under Edge
For Latin text, alphabetic is useful when the desired boundary is the baseline. For other writing systems, text or ideographic metrics may provide more appropriate results.
Important Consideration
Characters with descenders, such as g, p, and y, extend below the alphabetic baseline. Selecting that baseline does not change the glyph outlines.
One-Value and Two-Value Syntax
The text-box-edge property supports a single keyword or a pair of keywords, depending on the selected metrics.
One-Value Syntax
.heading {
text-box-edge: text;
}
The single text value selects the corresponding text-over and text-under metrics.
Single Over-Edge Keyword
.heading {
text-box-edge: cap;
}
The cap keyword selects the cap-height over edge. The corresponding under edge is determined by the property's syntax.
Two-Value Syntax
.heading {
text-box-edge: cap alphabetic;
}
The first keyword defines the over edge, and the second defines the under edge.
Another Two-Value Example
.heading {
text-box-edge: ex text;
}
This selects x-height for the over edge and the text-under metric for the under edge.
Value Order Matters
.heading {
text-box-edge: alphabetic cap;
}
This is invalid because alphabetic is an under-edge keyword and cannot occupy the over-edge position.
Important Consideration
Use the two-value form when you need explicit control over both text-box boundaries.
Understanding Font Metrics
Font metrics are measurements used by browsers to position text. The text-box-edge property references these measurements rather than accepting fixed pixel distances.
Cap Height
Cap height describes the font-defined height of capital letters above the alphabetic baseline.
X-Height
X-height describes the font-defined height associated with lowercase letters such as x.
Alphabetic Baseline
The alphabetic baseline is the reference line used for positioning many Latin letters.
Text-Over and Text-Under Metrics
These metrics define the font's intended text boundaries and can include space needed for different characters.
Ideographic Metrics
Ideographic metrics provide boundaries associated with ideographic writing systems.
Comparing Fonts
.sans-heading {
font-family: Arial, sans-serif;
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
.serif-heading {
font-family: Georgia, serif;
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
The same edge values can produce different amounts of trimming because the fonts have different measurements.
Font Fallbacks
If a preferred font is unavailable, a fallback font may use different metrics. This can change the appearance and dimensions of trimmed text.
Important Consideration
Font metrics are not identical to the visible outlines of every glyph. Characters may extend beyond the selected boundaries.
Relationship With text-box-trim
The text-box-edge and text-box-trim properties work together but serve different purposes.
| Property | Purpose |
|---|---|
text-box-edge |
Defines the font metrics used as trimming boundaries. |
text-box-trim |
Determines which outer text-box edges are trimmed. |
text-box |
Sets both properties using a shorthand declaration. |
Edge Metrics Without Trimming
.heading {
text-box-edge: cap alphabetic;
text-box-trim: none;
}
The boundaries are defined, but the text box is not trimmed.
Trim the Start Edge
.heading {
text-box-edge: cap alphabetic;
text-box-trim: trim-start;
}
Trims only the block-start edge using the selected over-edge metric.
Trim the End Edge
.heading {
text-box-edge: cap alphabetic;
text-box-trim: trim-end;
}
Trims only the block-end edge using the selected under-edge metric.
Trim Both Edges
.heading {
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
Trims both outer edges using their corresponding font metrics.
Important Consideration
Setting text-box-edge does not automatically enable trimming. The text-box-trim property must select at least one edge.
Using the text-box Shorthand
The text-box shorthand combines the edge definition and trimming mode in one declaration.
Longhand Version
.heading {
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
Equivalent Shorthand
.heading {
text-box: trim-both cap alphabetic;
}
Both examples select cap-height and alphabetic baseline metrics and enable trimming at both outer edges.
Trim Only the Start Edge
.heading {
text-box: trim-start cap;
}
Trim Only the End Edge
.heading {
text-box: trim-end text;
}
Restore Normal Behavior
.heading {
text-box: normal;
}
The normal value restores the initial shorthand behavior.
Shorthand Reset Behavior
.heading {
text-box-edge: cap alphabetic;
text-box: trim-both;
}
The shorthand resets the omitted text-box-edge component to its initial value. To preserve the intended edge metrics, include them in the shorthand or declare the longhand afterward.
Effects on Layout
When trimming is enabled, the selected edge metrics can affect the block-axis dimensions of a text container.
Heading Spacing
.page-heading {
margin: 0;
font-size: 40px;
line-height: 1.4;
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
The heading may occupy less vertical space because the selected outer edges are trimmed.
Padding Remains Unchanged
.button {
padding: 12px 20px;
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
The trimming affects the text box, not the element's padding.
Line Height Still Applies
.heading {
font-size: 32px;
line-height: 1.5;
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
The line-height property continues to influence text layout. Trimming adjusts the selected outer boundaries rather than removing all line spacing.
Multiline Text
For multiline text containers, trimming affects the selected outer edges rather than independently trimming every internal line.
Writing Modes
Block-start and block-end are logical directions. Their physical positions depend on the element's writing mode.
Responsive Layouts
Changes in font size, available width, or line wrapping can affect the final dimensions of trimmed text.
Practical Examples
Cap-Height Heading
.page-title {
font-size: 40px;
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
Trims the heading using cap height and the alphabetic baseline.
X-Height Label
.small-label {
font-size: 14px;
text-box-edge: ex alphabetic;
text-box-trim: trim-both;
}
Uses the x-height metric for the upper boundary.
Text-Metric Trimming
.text-label {
text-box-edge: text;
text-box-trim: trim-both;
}
Uses the font's text-over and text-under metrics.
Trim Only the Top Edge
.section-title {
text-box-edge: cap alphabetic;
text-box-trim: trim-start;
}
Trims the block-start edge while preserving the block-end edge.
Trim Only the Bottom Edge
.section-title {
text-box-edge: cap alphabetic;
text-box-trim: trim-end;
}
Trims the block-end edge while preserving the block-start edge.
Button Text
.action-button {
padding: 12px 20px;
font-size: 16px;
line-height: 1.4;
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
Adjusts the text-box boundaries without removing the button's padding.
Card Heading
.card-title {
margin: 0 0 16px;
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
Trims the heading while retaining its bottom margin.
Inherited Edge Metrics
.content-section {
text-box-edge: cap alphabetic;
}
.content-section h2 {
text-box-trim: trim-both;
}
The heading inherits the edge metrics from its parent and applies trimming locally.
Progressive Enhancement
.enhanced-heading {
margin: 0;
line-height: 1.2;
}
@supports (text-box-edge: cap alphabetic) and (text-box-trim: trim-both) {
.enhanced-heading {
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
}
Applies the feature only when the browser recognizes both required declarations.
text-box-edge Example
This example compares normal text-box spacing with two different edge definitions. Each sample uses the same font size and line height so the trimming boundaries can be compared.
HTML Example
<div class="text-edge-demo">
<div class="text-edge-panel">
<h4>Normal Text Box</h4>
<div class="text-edge-sample normal">
<span>CSS Heading</span>
</div>
</div>
<div class="text-edge-panel">
<h4>Cap Height + Alphabetic</h4>
<div class="text-edge-sample cap-edge">
<span>CSS Heading</span>
</div>
</div>
<div class="text-edge-panel">
<h4>X-Height + Alphabetic</h4>
<div class="text-edge-sample ex-edge">
<span>CSS Heading</span>
</div>
</div>
</div>
CSS Example
.text-edge-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 16px;
}
.text-edge-demo .text-edge-panel {
padding: 16px;
border: 1px solid #cbd5e1;
background: #f8fafc;
}
.text-edge-demo h4 {
margin: 0 0 16px;
font-size: 14px;
}
.text-edge-demo .text-edge-sample {
display: flow-root;
background: #dbeafe;
}
.text-edge-demo .text-edge-sample span {
display: block;
font-family: Arial, sans-serif;
font-size: 32px;
font-weight: bold;
line-height: 1.5;
}
.text-edge-demo .normal {
text-box-trim: none;
}
.text-edge-demo .cap-edge {
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
.text-edge-demo .ex-edge {
text-box-edge: ex alphabetic;
text-box-trim: trim-both;
}
Normal Text Box
Cap Height + Alphabetic
X-Height + Alphabetic
In supporting browsers, the second and third samples use different over-edge metrics. The amount of trimming depends on the font and selected boundary.
Comparing the Results
| Sample | Behavior |
|---|---|
| Normal | Preserves the normal text box without trimming. |
| Cap Height + Alphabetic | Uses cap height and the alphabetic baseline as trimming boundaries. |
| X-Height + Alphabetic | Uses x-height and the alphabetic baseline as trimming boundaries. |
Changing the Edge Definition
.text-edge-demo .cap-edge {
text-box-edge: text;
text-box-trim: trim-both;
}
The sample now uses the font's text-over and text-under metrics.
Disabling Trimming
.text-edge-demo .cap-edge {
text-box-edge: cap alphabetic;
text-box-trim: none;
}
The edge metrics remain defined, but no trimming is applied.
Testing Browser Support
@supports (text-box-edge: cap alphabetic) and (text-box-trim: trim-both) {
.text-edge-demo .cap-edge {
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
}
This feature query checks recognition of both declarations before applying them.
Accessibility Considerations
Text-box edge trimming can improve visual alignment, but it should not reduce readability or cause important characters to be clipped.
Avoid Clipping Accents and Descenders
Some glyphs extend beyond cap-height, x-height, or baseline metrics. Test accented letters, descenders, punctuation, and other character forms.
Preserve Readable Line Spacing
Use appropriate line-height values, especially for multiline text. Trimming should not cause lines to overlap.
Test Different Languages
Writing systems use different font metrics. A trimming configuration suitable for Latin text may not produce the desired result for other scripts.
Support Text Resizing
Verify that text remains readable when users enlarge fonts or increase browser zoom.
Maintain Interactive Target Sizes
Buttons and links should retain sufficient padding and dimensions. Trimming text should not make interactive controls difficult to use.
Check Font Fallbacks
Fallback fonts may have different metrics and can produce different trimming results.
Use Progressive Enhancement
Ensure the layout remains usable in browsers that do not support the property.
Common Mistakes
Expecting text-box-edge to Enable Trimming
h2 {
text-box-edge: cap alphabetic;
}
This defines the boundaries but does not activate trimming. Add text-box-trim when trimming is required.
Using Invalid Alignment Values
h2 {
text-box-edge: center;
}
The property does not accept center, left, or right.
Using Lengths or Percentages
h2 {
text-box-edge: 10px;
}
Use supported font-metric keywords rather than fixed measurements.
Reversing the Value Order
h2 {
text-box-edge: alphabetic cap;
}
The over-edge value must come before the under-edge value.
Confusing Font Metrics With Glyph Outlines
The property uses font-defined measurements rather than the exact visible boundaries of individual characters.
Ignoring Font Differences
Different fonts can produce different trimming results even when the CSS declarations are identical.
Removing Padding Unnecessarily
Text-box trimming does not remove padding. Keep appropriate padding for layout and interactive controls.
Ignoring Browser Support
Unsupported browsers ignore the declaration. Provide a usable fallback layout.
Overlooking Inheritance
The property is inherited. A parent declaration can affect descendants that use text-box trimming.
Overwriting Longhand Values With the Shorthand
A later text-box shorthand declaration can reset an earlier text-box-edge value.
Expecting Smooth Animation
The property uses discrete animation behavior. Changes between metric keywords do not interpolate smoothly.
Important Considerations
Keep the following points in mind when using text-box-edge:
- It defines font-based boundaries used for text-box trimming.
- It works with
text-box-trim. - It is a longhand component of the
text-boxshorthand. - The initial value is
auto. - The property is inherited.
- It accepts one or two font-metric keywords.
- The first value in a two-value declaration defines the over edge.
- The second value defines the under edge.
- The
capkeyword uses the font's cap-height metric. - The
exkeyword uses the font's x-height metric. - The
alphabetickeyword uses the alphabetic baseline. - The
textkeyword uses font text-edge metrics. - Ideographic metrics are available for relevant writing systems.
- The property does not accept arbitrary lengths or percentages.
- It does not change the font size.
- It does not remove margins or padding.
- Setting edge metrics alone does not activate trimming.
- The
text-box-trimproperty determines which outer edges are trimmed. - The shorthand can set both properties together.
- A shorthand declaration can reset previously specified longhand values.
- The resulting trimming depends on font metrics.
- Different font families can produce different results.
- Fallback fonts may change the layout.
- Glyphs can extend beyond selected font-metric boundaries.
- Accented characters and descenders require careful testing.
- Multiline text retains normal spacing between internal lines.
- Writing modes affect the physical location of block-axis edges.
- The property is discretely animated.
- Browser support varies by browser and version.
- Feature queries can be used for progressive enhancement.
- Test responsive layouts, text resizing, and font fallbacks.
Browser Support
The CSS text-box-edge property is a newer CSS typography feature with limited availability compared with established properties such as font-size and line-height. Support varies by browser and version.
Browsers that do not recognize the property ignore its declaration. Because the feature is intended to work with text-box-trim, check support for both properties before relying on trimming behavior.
For current compatibility information, visit Can I Use? and search for text-box-edge.
Feature Detection
@supports (text-box-edge: cap alphabetic) {
.heading {
text-box-edge: cap alphabetic;
}
}
This checks whether the browser recognizes the edge declaration.
Checking Both Properties
@supports (text-box-edge: cap alphabetic) and (text-box-trim: trim-both) {
.heading {
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
}
Checking both properties is useful when the intended result requires actual text-box trimming.
Fallback Behavior
.heading {
margin: 0;
font-size: 32px;
line-height: 1.2;
}
@supports (text-box-edge: cap alphabetic) and (text-box-trim: trim-both) {
.heading {
text-box-edge: cap alphabetic;
text-box-trim: trim-both;
}
}
The heading retains its normal text layout in unsupported browsers.
Testing Recommendations
Test different font families, fallback fonts, writing systems, multiline headings, and responsive layouts. Check for clipped glyphs and unexpected changes in element dimensions.
Specifications
The text-box-edge property is defined in the CSS Inline Layout Module Level 3 specification.
CSS Inline Layout Module Level 3 - text-box-edge Property
The specification defines the supported font-metric keywords, edge selection, inheritance, and interaction with text-box trimming.
For additional documentation and examples, consult MDN Web Docs.
