CSS text-underline-offset Property
The CSS text-underline-offset property controls the distance between an underline and its normal position beneath the text.
It allows authors to move an underline closer to or farther from the text without changing the font size or line height. The property is useful for links, headings, navigation menus, and other underlined text. It works with underlines created by text-decoration and does not create an underline by itself.
text-underline-offset Syntax
The property accepts auto, a length, or a percentage. Positive and negative offsets are allowed.
Basic Syntax
.example {
text-underline-offset: 4px;
}
Available Values
.example {
text-underline-offset: auto;
}
.example {
text-underline-offset: 3px;
}
.example {
text-underline-offset: 0.2em;
}
.example {
text-underline-offset: 10%;
}
.example {
text-underline-offset: -2px;
}
CSS-Wide Keywords
.example {
text-underline-offset: initial;
}
.example {
text-underline-offset: inherit;
}
.example {
text-underline-offset: unset;
}
.example {
text-underline-offset: revert;
}
.example {
text-underline-offset: revert-layer;
}
The initial value is auto. The property is inherited, so unset behaves like inherit.
Valid Examples
.example {
text-underline-offset: 0;
}
.example {
text-underline-offset: 5px;
}
.example {
text-underline-offset: 0.15em;
}
.example {
text-underline-offset: 12%;
}
.example {
text-underline-offset: -0.1em;
}
Invalid Examples
.example {
text-underline-offset: none;
}
.example {
text-underline-offset: normal;
}
.example {
text-underline-offset: bold;
}
.example {
text-underline-offset: auto 4px;
}
.example {
text-underline-offset: 4;
}
The property does not accept none, normal, font-weight keywords, or multiple values. A nonzero length requires a unit.
Property Values
| Value | Description |
|---|---|
auto |
Lets the browser determine the underline offset. |
<length> |
Specifies an underline offset using a CSS length, such as px, em, or rem. |
<percentage> |
Specifies an offset relative to the element's font size. |
initial |
Restores the initial value, auto. |
inherit |
Uses the computed value of the parent element. |
unset |
Inherits the parent's computed value. |
revert |
Reverts the property according to the CSS cascade origin. |
revert-layer |
Reverts the property according to the previous cascade layer. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | auto |
| Inherited | Yes |
| Applies to | All elements |
| Computed value | Specified keyword, absolute length, or percentage |
| Animation type | By computed value type |
| Percentages | Relative to the element's font size |
| Negative values | Allowed |
| Specification | CSS Text Decoration Module Level 4 |
Note: The offset is measured from the underline's normal position, not directly from the bottom edge of the text or its containing element.
How It Works
The browser first determines where an underline would normally appear. The text-underline-offset property then adjusts its position by the specified amount.
Step 1: Create Underlined Text
<p class="underlined-text">Learning CSS Underlines</p>
Step 2: Add an Underline
.underlined-text {
text-decoration-line: underline;
}
Step 3: Adjust the Offset
.underlined-text {
text-decoration-line: underline;
text-underline-offset: 5px;
}
The underline moves away from its normal position by 5 pixels.
Step 4: Compare Different Offsets
.small-offset {
text-decoration: underline;
text-underline-offset: 2px;
}
.large-offset {
text-decoration: underline;
text-underline-offset: 8px;
}
Browser Result
Small offset: 2px
Large offset: 8px
Important Consideration
The property changes the underline's position without changing the element's font size, line height, margin, or padding.
The auto Value
The auto value lets the browser determine the underline's offset.
Basic Example
.example {
text-underline-offset: auto;
}
HTML Example
<p class="automatic-underline">Automatic Underline Position</p>
CSS Example
.automatic-underline {
text-decoration: underline;
text-underline-offset: auto;
}
Automatic Underline Position
Font Metrics
Fonts contain metrics that help browsers determine suitable underline positions. The browser may use this information when positioning an automatic underline.
Default Behavior
Because auto is the initial value, an explicit declaration is normally unnecessary.
When to Use auto
Use auto when the browser's default underline position is acceptable or when restoring the default behavior.
Length Values
Length values specify an underline offset using CSS units such as px, em, and rem.
Pixel Values
.example {
text-decoration: underline;
text-underline-offset: 4px;
}
Pixel values provide a fixed CSS length for the offset.
em Values
.example {
text-decoration: underline;
text-underline-offset: 0.2em;
}
The em unit is relative to the font size, making it useful for underlines that scale with the text.
rem Values
.example {
text-decoration: underline;
text-underline-offset: 0.25rem;
}
The rem unit is relative to the root element's font size.
Comparing Length Units
| Value | Behavior |
|---|---|
4px |
Offsets the underline by 4 CSS pixels. |
0.2em |
Offsets the underline relative to the element's font size. |
0.25rem |
Offsets the underline relative to the root font size. |
0 |
Applies no additional offset from the normal underline position. |
Recommended Approach
Relative units such as em are often useful when text sizes vary because the underline offset can scale with the typography.
Percentage Values
Percentage values specify an offset relative to the element's font size.
Basic Example
.example {
text-decoration: underline;
text-underline-offset: 15%;
}
Percentage Calculation
For an element with a 20-pixel font size, an offset of 15% corresponds to 3 pixels.
Different Percentages
.small-offset {
text-underline-offset: 10%;
}
.medium-offset {
text-underline-offset: 20%;
}
.large-offset {
text-underline-offset: 30%;
}
Percentage vs. em
For a given font size, 20% and 0.2em represent the same offset distance.
Browser Support Consideration
Percentage values are defined by the specification, but support has varied among browser implementations. Test percentage offsets in the browsers your website supports.
Fallback Example
.example {
text-decoration: underline;
text-underline-offset: 0.2em;
text-underline-offset: 20%;
}
If the browser does not recognize percentage offsets, the preceding em declaration can remain in effect.
Positive and Negative Values
Positive and negative offsets move the underline in opposite directions from its normal position.
Positive Offset
.positive-offset {
text-decoration: underline;
text-underline-offset: 5px;
}
In ordinary horizontal text with an underline below the characters, a positive offset moves the underline farther downward.
Negative Offset
.negative-offset {
text-decoration: underline;
text-underline-offset: -3px;
}
A negative offset moves the underline in the opposite direction, potentially bringing it closer to or across the text.
Zero Offset
.zero-offset {
text-decoration: underline;
text-underline-offset: 0;
}
A zero offset requests no displacement from the normal underline position.
Visual Comparison
Negative offset: -2px
Zero offset: 0
Positive offset: 6px
Important Consideration
Large negative offsets may cause underlines to overlap letters. Large positive offsets can make underlines appear disconnected from the text.
Underline Position
The text-underline-offset property works with text-underline-position, but the two properties serve different purposes.
text-underline-position
.example {
text-decoration: underline;
text-underline-position: under;
}
The text-underline-position property controls how the browser chooses the underline's base position.
text-underline-offset
.example {
text-decoration: underline;
text-underline-offset: 4px;
}
The text-underline-offset property adjusts the underline's position relative to that base position.
Combining Both Properties
.example {
text-decoration-line: underline;
text-underline-position: under;
text-underline-offset: 3px;
}
Comparison
| Property | Purpose |
|---|---|
text-underline-position |
Determines the underline's base placement. |
text-underline-offset |
Adjusts the distance from that placement. |
Important Consideration
The actual underline position depends on font metrics, writing mode, and the browser's text decoration implementation.
Related Text Decoration Properties
Several CSS properties work together to control the appearance of underlined text.
text-decoration-line
.example {
text-decoration-line: underline;
}
Creates the underline.
text-decoration-color
.example {
text-decoration-line: underline;
text-decoration-color: #2563eb;
}
Controls the color of the underline.
text-decoration-style
.example {
text-decoration-line: underline;
text-decoration-style: wavy;
}
Controls the underline's style, such as solid, dotted, dashed, double, or wavy.
text-decoration-thickness
.example {
text-decoration-line: underline;
text-decoration-thickness: 2px;
}
Controls the thickness of the underline.
text-decoration-skip-ink
.example {
text-decoration-line: underline;
text-decoration-skip-ink: auto;
}
Controls whether the underline skips portions of glyphs that would otherwise intersect the decoration.
Combining Decoration Properties
.example {
text-decoration-line: underline;
text-decoration-color: #2563eb;
text-decoration-style: solid;
text-decoration-thickness: 2px;
text-underline-offset: 4px;
}
Using the text-decoration Shorthand
.example {
text-decoration: underline solid #2563eb 2px;
text-underline-offset: 4px;
}
The text-decoration shorthand does not include text-underline-offset. The offset must be declared separately.
Inheritance
The text-underline-offset property is inherited, so descendant elements can receive the parent's computed offset.
Parent Element Example
.content {
text-underline-offset: 4px;
}
Descendants inherit the offset unless they specify a different value.
Child Element Override
.content {
text-underline-offset: 4px;
}
.content .special-link {
text-underline-offset: 8px;
}
Explicit Inheritance
.child {
text-underline-offset: inherit;
}
Restore the Initial Value
.child {
text-underline-offset: initial;
}
This restores auto.
Using unset
.child {
text-underline-offset: unset;
}
Because the property is inherited, unset uses the parent's computed value.
Inheritance and Font Size
Relative lengths such as em are resolved as part of the computed value. A child inheriting a computed length does not automatically recalculate it using its own font size.
Percentage Inheritance
Percentage offsets remain percentages in the computed value, so their used distance can reflect the font size of the element where the underline is rendered.
Important Consideration
Inheritance of the offset does not automatically create an underline. An underline must still be established by a text decoration.
Practical Examples
Underline a Link
a {
text-decoration: underline;
text-underline-offset: 3px;
}
Navigation Links
.main-navigation a {
text-decoration: underline;
text-underline-offset: 5px;
}
Heading Underline
.section-heading {
text-decoration: underline;
text-underline-offset: 0.2em;
}
Colored Underline
.colored-link {
text-decoration-line: underline;
text-decoration-color: #2563eb;
text-underline-offset: 4px;
}
Thick Underline
.highlight-link {
text-decoration-line: underline;
text-decoration-thickness: 3px;
text-underline-offset: 5px;
}
Dotted Underline
.dotted-text {
text-decoration: underline dotted;
text-underline-offset: 4px;
}
Wavy Underline
.wavy-text {
text-decoration: underline wavy;
text-underline-offset: 5px;
}
Relative Underline Offset
.responsive-link {
text-decoration: underline;
text-underline-offset: 0.15em;
}
Hover Underline Offset
.interactive-link {
text-decoration: underline;
text-underline-offset: 2px;
transition: text-underline-offset 0.2s ease;
}
.interactive-link:hover {
text-underline-offset: 6px;
}
Supporting browsers can animate between compatible length values.
Focus State
.interactive-link:focus-visible {
text-decoration-thickness: 3px;
text-underline-offset: 5px;
outline: 2px solid currentColor;
outline-offset: 3px;
}
The underline is enhanced while a separate outline provides a clear keyboard focus indicator.
Percentage Offset
.percentage-link {
text-decoration: underline;
text-underline-offset: 20%;
}
Restore Automatic Positioning
.default-underline {
text-decoration: underline;
text-underline-offset: auto;
}
text-underline-offset Example
This example compares several underline offsets. Each sample uses the same font size and underline style so the differences in position are easier to see.
HTML Example
<div class="underline-offset-demo">
<div class="offset-sample">
<strong>auto</strong>
<p class="offset-auto">Learning CSS Underlines</p>
</div>
<div class="offset-sample">
<strong>2px</strong>
<p class="offset-small">Learning CSS Underlines</p>
</div>
<div class="offset-sample">
<strong>5px</strong>
<p class="offset-medium">Learning CSS Underlines</p>
</div>
<div class="offset-sample">
<strong>0.4em</strong>
<p class="offset-large">Learning CSS Underlines</p>
</div>
<div class="offset-sample">
<strong>-2px</strong>
<p class="offset-negative">Learning CSS Underlines</p>
</div>
</div>
CSS Example
.underline-offset-demo {
display: grid;
gap: 12px;
}
.underline-offset-demo .offset-sample {
padding: 18px 14px;
border: 1px solid #cbd5e1;
background: #f8fafc;
}
.underline-offset-demo strong {
display: block;
margin-bottom: 12px;
color: #334155;
font-size: 14px;
}
.underline-offset-demo p {
margin: 0;
color: #1f2937;
font-size: 20px;
line-height: 1.8;
text-decoration-line: underline;
text-decoration-color: #2563eb;
text-decoration-thickness: 2px;
}
.underline-offset-demo .offset-auto {
text-underline-offset: auto;
}
.underline-offset-demo .offset-small {
text-underline-offset: 2px;
}
.underline-offset-demo .offset-medium {
text-underline-offset: 5px;
}
.underline-offset-demo .offset-large {
text-underline-offset: 0.4em;
}
.underline-offset-demo .offset-negative {
text-underline-offset: -2px;
}
Learning CSS Underlines
Learning CSS Underlines
Learning CSS Underlines
Learning CSS Underlines
Learning CSS Underlines
Understanding the Results
The auto value uses the browser's normal underline offset. Positive values move the underline farther from its normal position, while negative values move it in the opposite direction.
Changing the Font Size
.underline-offset-demo p {
font-size: 28px;
}
Increasing the font size changes the distance represented by an em offset. Fixed pixel offsets remain the same CSS length.
Changing the Underline Thickness
.underline-offset-demo p {
text-decoration-thickness: 4px;
}
A thicker underline can make the difference between offsets more noticeable.
Accessibility Considerations
Underline positioning can improve the visual clarity of links and other decorated text when used appropriately.
Keep Underlines Visible
Choose an offset that makes the underline easy to recognize without separating it excessively from the text.
Avoid Overlapping Characters
Large negative offsets can cause underlines to cross through letters and reduce readability.
Preserve Link Identification
Underlines help users distinguish links from surrounding text. Avoid removing link underlines without providing another clear visual indicator.
Maintain Sufficient Contrast
Ensure that underline colors remain distinguishable from the background.
Use Appropriate Thickness
Very thin underlines may be difficult to see, particularly at small font sizes.
Consider Text Scaling
Relative offsets such as em can scale with the font size, helping maintain consistent proportions.
Preserve Keyboard Focus Indicators
An underline adjustment should not replace a visible focus indicator for keyboard navigation.
Respect Reduced Motion Preferences
If underline offsets are animated, consider disabling the transition when the user requests reduced motion.
@media (prefers-reduced-motion: reduce) {
.interactive-link {
transition: none;
}
}
Test Different Fonts
Underline positions depend partly on font metrics. Test the design with the fonts used on the website.
Avoid Decorative Overuse
Excessively distant or thick underlines can distract from the text and make interfaces harder to scan.
Common Mistakes
Expecting the Property to Create an Underline
.example {
text-underline-offset: 5px;
}
This declaration does not create an underline. Use text-decoration-line: underline or another declaration that establishes an underline.
Using the none Keyword
.example {
text-underline-offset: none;
}
The none keyword is not valid. Use auto to restore automatic positioning.
Using a Unitless Nonzero Number
.example {
text-underline-offset: 5;
}
Nonzero lengths require units.
Using Multiple Values
.example {
text-underline-offset: 2px 5px;
}
The property accepts one offset value.
Confusing Offset With Thickness
Use text-decoration-thickness to change underline thickness. The text-underline-offset property changes its position.
Confusing Offset With Position
The text-underline-position property determines the underline's base placement. The offset adjusts that placement.
Using Excessive Negative Values
Negative offsets can move an underline through the text.
Using Excessive Positive Values
Large offsets can make the underline appear disconnected from the text.
Assuming the Offset Changes Line Height
The property does not directly increase line height or reserve additional space for the underline.
Including Offset in the text-decoration Shorthand
.example {
text-decoration: underline 4px;
}
In this shorthand, 4px specifies decoration thickness, not underline offset.
Ignoring Percentage Compatibility
Percentage values may require additional browser testing.
Assuming All Fonts Produce Identical Results
Different fonts have different underline metrics and may produce different visual positions.
Forgetting Inheritance
Descendant elements can inherit the offset even when they establish their own underline decoration.
Important Considerations
Keep the following points in mind when using text-underline-offset:
- The property controls the offset of an underline from its normal position.
- It does not create an underline by itself.
- The initial value is
auto. - The property is inherited.
- It accepts
auto, lengths, and percentages. - Positive and negative values are allowed.
- A zero offset is valid without a unit.
- Nonzero lengths require units.
- Percentage offsets are relative to the element's font size.
- Relative units such as
emcan help offsets scale with typography. - Large negative offsets can overlap characters.
- Large positive offsets can separate the underline excessively from the text.
- The property does not directly change line height.
- It does not change font size or font weight.
- It does not control underline thickness.
- It is not included in the
text-decorationshorthand. - It can be combined with
text-decoration-color. - It can be combined with
text-decoration-style. - It can be combined with
text-decoration-thickness. - It works alongside
text-underline-position. - Font metrics influence the normal underline position.
- Underline positioning may vary across writing modes.
- Percentage support should be checked for target browsers.
- Underline visibility is important for accessible links.
- Test the result with different fonts and text sizes.
Browser Support
The CSS text-underline-offset property is widely available in modern browsers for commonly used values such as auto and CSS lengths.
Support for percentage offsets may differ between browser implementations. Underline placement can also vary according to font metrics and text decoration behavior.
For current compatibility information, visit Can I Use? and search for text-underline-offset.
Basic Declaration
.example {
text-decoration: underline;
text-underline-offset: 4px;
}
Feature Detection
@supports (text-underline-offset: 4px) {
.example {
text-underline-offset: 4px;
}
}
Checking Percentage Support
@supports (text-underline-offset: 20%) {
.example {
text-underline-offset: 20%;
}
}
Feature queries check whether a declaration is recognized, but they do not guarantee identical visual rendering across fonts and browsers.
Fallback Behavior
Browsers that do not recognize the property ignore the declaration and use their normal underline positioning.
Testing Recommendations
Test underline offsets with the fonts, font sizes, writing modes, and browsers used by the website.
Specifications
The text-underline-offset property is defined in the CSS Text Decoration Module Level 4 specification.
CSS Text Decoration Module Level 4 - text-underline-offset Property
The specification defines the property's syntax, inheritance, offset calculations, and relationship to underline positioning.
For additional documentation and examples, consult MDN Web Docs.
