CSS text-underline-position Property
The CSS text-underline-position property controls where an underline is placed relative to the text.
It allows browsers to position underlines automatically, beneath the text, or on a specified side of vertical text. The property is especially useful for multilingual typography and layouts that use vertical writing modes. It affects underlines created by text decorations but does not create an underline by itself.
text-underline-position Syntax
The property accepts auto, under, left, right, or supported combinations of under with a side keyword.
Basic Syntax
.example {
text-underline-position: under;
}
Available Values
.example {
text-underline-position: auto;
}
.example {
text-underline-position: under;
}
.example {
text-underline-position: left;
}
.example {
text-underline-position: right;
}
.example {
text-underline-position: under left;
}
.example {
text-underline-position: under right;
}
The combined forms are part of the CSS Text Decoration specification, but browser support for individual combinations can vary.
CSS-Wide Keywords
.example {
text-underline-position: initial;
}
.example {
text-underline-position: inherit;
}
.example {
text-underline-position: unset;
}
.example {
text-underline-position: revert;
}
.example {
text-underline-position: revert-layer;
}
The initial value is auto. The property is inherited.
Invalid Examples
.example {
text-underline-position: none;
}
.example {
text-underline-position: top;
}
.example {
text-underline-position: bottom;
}
.example {
text-underline-position: 5px;
}
.example {
text-underline-position: left right;
}
.example {
text-underline-position: auto under;
}
The property does not accept lengths, percentages, none, or arbitrary combinations of keywords. Use text-underline-offset when a numeric adjustment is needed.
Property Values
| Value | Description |
|---|---|
auto |
Allows the browser to determine the underline's position. |
under |
Places the underline beneath the text, accounting for the text's typographic position. |
left |
Places the underline on the left side of vertical text. |
right |
Places the underline on the right side of vertical text. |
under left |
Requests under positioning for horizontal text and left-side positioning for vertical text. |
under right |
Requests under positioning for horizontal text and right-side positioning for vertical text. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | auto |
| Inherited | Yes |
| Applies to | All elements |
| Computed value | As specified |
| Animation type | Discrete |
| Percentages | Not accepted |
| Specification | CSS Text Decoration Module Level 3 |
Note: Underline placement depends on the writing mode, font metrics, and the browser's text decoration implementation.
How It Works
The browser first determines whether the text has an underline. It then uses text-underline-position to determine where that underline should be drawn.
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: Set the Position
.underlined-text {
text-decoration-line: underline;
text-underline-position: under;
}
The browser positions the underline beneath the text rather than relying solely on its default placement.
Step 4: Adjust the Distance
.underlined-text {
text-decoration-line: underline;
text-underline-position: under;
text-underline-offset: 3px;
}
The text-underline-offset property adjusts the underline's distance from the position established by text-underline-position.
Important Consideration
Underline positioning changes the decoration's placement without changing the text itself or directly modifying the element's dimensions.
The auto Value
The auto value allows the browser to select the underline position using its normal text decoration behavior.
Basic Example
.example {
text-underline-position: auto;
}
HTML Example
<p class="automatic-underline">Automatic Underline Position</p>
CSS Example
.automatic-underline {
text-decoration: underline;
text-underline-position: auto;
}
Automatic Underline Position
Default Behavior
Because auto is the initial value, the browser uses it when no other value is specified or inherited.
Font Metrics
Browsers can use font metrics and language conventions to determine an appropriate underline position.
When to Use auto
Use auto when the browser's normal underline placement is suitable or when resetting a previously specified position.
The under Value
The under value positions the underline beneath the text, accounting for glyph descenders and typographic conventions.
Basic Example
.example {
text-underline-position: under;
}
HTML Example
<p class="under-position">Typography and Underlines</p>
CSS Example
.under-position {
text-decoration-line: underline;
text-underline-position: under;
}
Typography and Underlines
Avoiding Descenders
Letters such as g, j, p, q, and y extend below the baseline. The under value can help position the underline below these descenders.
CJK Typography
The under value is useful in East Asian typography, where underline placement may follow conventions different from those used for Latin text.
Combining With Offset
.example {
text-decoration: underline;
text-underline-position: under;
text-underline-offset: 2px;
}
When to Use under
Use this value when an underline should be positioned below the text rather than through the lower portions of glyphs.
Important Consideration
The exact position depends on the font and browser. The value does not specify a fixed distance in pixels.
The left Value
The left value positions the underline on the left side of vertical text.
Basic Example
.example {
text-underline-position: left;
}
Vertical Writing Example
<p class="vertical-left">Vertical Text Example</p>
CSS Example
.vertical-left {
writing-mode: vertical-rl;
text-decoration-line: underline;
text-underline-position: left;
}
Understanding the Result
In vertical writing, an underline is drawn along the side of the text rather than horizontally beneath it. The left value selects the left side.
Writing Mode
The writing-mode property controls the direction in which lines of text are arranged. It is commonly used for vertical East Asian text.
When to Use left
Use left when a vertical text layout requires the underline to appear on the left side of the characters.
Horizontal Text
In horizontal writing modes, left does not move the underline to the left edge of the element. Its side-placement behavior applies to vertical text.
The right Value
The right value positions the underline on the right side of vertical text.
Basic Example
.example {
text-underline-position: right;
}
Vertical Writing Example
<p class="vertical-right">Vertical Text Example</p>
CSS Example
.vertical-right {
writing-mode: vertical-rl;
text-decoration-line: underline;
text-underline-position: right;
}
Understanding the Result
The underline appears along the right side of the vertical text.
Comparison With left
.vertical-left {
text-underline-position: left;
}
.vertical-right {
text-underline-position: right;
}
The values place the underline on opposite sides of vertical text.
When to Use right
Use right when the typography or writing convention calls for an underline on the right side of vertical characters.
Important Consideration
The right value does not align the text itself. It only affects the underline's placement.
Combining Values
The specification allows under to be combined with left or right, providing separate placement behavior for horizontal and vertical writing modes.
under left
.example {
text-underline-position: under left;
}
This requests under positioning in horizontal text and left-side positioning in vertical text.
under right
.example {
text-underline-position: under right;
}
This requests under positioning in horizontal text and right-side positioning in vertical text.
Practical Example
.multilingual-text {
text-decoration-line: underline;
text-underline-position: under right;
}
Keyword Order
The grammar permits the supported keywords in either order.
.example {
text-underline-position: right under;
}
Invalid Combination
.example {
text-underline-position: left right;
}
The left and right values cannot be used together.
Browser Support
Support for combined values is not uniform across browsers. Test the exact combination before using it in a production layout.
Writing Modes and Underline Placement
The writing-mode property determines whether text is laid out horizontally or vertically. This affects how underline positions are interpreted.
Horizontal Writing
.horizontal-text {
writing-mode: horizontal-tb;
text-decoration: underline;
text-underline-position: under;
}
In ordinary horizontal writing, the underline is positioned beneath the text.
Vertical Right-to-Left Writing
.vertical-rl-text {
writing-mode: vertical-rl;
text-decoration: underline;
text-underline-position: right;
}
Text flows vertically, and the underline appears on the right side.
Vertical Left-to-Right Writing
.vertical-lr-text {
writing-mode: vertical-lr;
text-decoration: underline;
text-underline-position: left;
}
Text flows vertically, and the underline appears on the left side.
Writing Mode Comparison
| Writing Mode | Underline Placement |
|---|---|
horizontal-tb |
Typically beneath the text. |
vertical-rl |
Along a side of the vertical text. |
vertical-lr |
Along a side of the vertical text. |
Language Considerations
Vertical writing is commonly used for Japanese and other East Asian scripts. The appropriate underline side depends on the writing convention and design.
Important Consideration
Use the correct writing-mode and language markup for the content. Underline placement should support the text's natural reading direction.
Underline Position vs. Underline Offset
The text-underline-position and text-underline-offset properties work together but control different aspects of an underline.
text-underline-position
.example {
text-underline-position: under;
}
Determines the underline's base placement.
text-underline-offset
.example {
text-underline-offset: 4px;
}
Adjusts the distance from the underline's normal position.
Combining Both Properties
.example {
text-decoration-line: underline;
text-underline-position: under;
text-underline-offset: 4px;
}
Comparison
| Property | Purpose | Example |
|---|---|---|
text-underline-position |
Chooses the underline's placement. | under |
text-underline-offset |
Adjusts the underline's distance. | 4px |
When to Use Each Property
Use text-underline-position to control placement relative to the text and writing mode. Use text-underline-offset when the underline needs a specific additional displacement.
Related CSS Properties
Several CSS properties can be combined to control the appearance and placement of underlines.
text-decoration-line
.example {
text-decoration-line: underline;
}
Creates the underline decoration.
text-decoration-color
.example {
text-decoration-color: #2563eb;
}
Controls the underline color.
text-decoration-style
.example {
text-decoration-style: dotted;
}
Controls the underline style.
text-decoration-thickness
.example {
text-decoration-thickness: 2px;
}
Controls the thickness of the underline.
text-decoration-skip-ink
.example {
text-decoration-skip-ink: auto;
}
Controls whether the underline skips portions of glyphs that would otherwise intersect it.
writing-mode
.example {
writing-mode: vertical-rl;
}
Controls the direction in which lines of text are arranged.
text-orientation
.example {
text-orientation: upright;
}
Controls the orientation of characters in vertical writing modes.
Complete Underline Styling
.example {
text-decoration-line: underline;
text-decoration-color: #2563eb;
text-decoration-style: solid;
text-decoration-thickness: 2px;
text-underline-position: under;
text-underline-offset: 3px;
}
Inheritance
The text-underline-position property is inherited. Descendant elements receive the parent's computed value unless they specify another value.
Parent Element Example
.content {
text-underline-position: under;
}
Child Element Override
.content {
text-underline-position: under;
}
.content .special-text {
text-underline-position: auto;
}
Explicit Inheritance
.child {
text-underline-position: inherit;
}
Restore the Initial Value
.child {
text-underline-position: initial;
}
This restores auto.
Using unset
.child {
text-underline-position: unset;
}
Because the property is inherited, unset uses the parent's computed value.
Nested Example
<div class="text-container">
<p><a href="#">Standard Link</a></p>
<p><a href="#" class="special-link">Special Link</a></p>
</div>
.text-container {
text-underline-position: under;
}
.text-container .special-link {
text-underline-position: auto;
}
Important Consideration
Inheriting an underline position does not automatically create an underline. The text must have an underline decoration for the position to become visible.
Practical Examples
Underline a Link
a {
text-decoration: underline;
text-underline-position: under;
}
Navigation Links
.main-navigation a {
text-decoration-line: underline;
text-underline-position: under;
}
Heading Underline
.section-heading {
text-decoration: underline;
text-underline-position: under;
}
Underline With Offset
.custom-underline {
text-decoration: underline;
text-underline-position: under;
text-underline-offset: 3px;
}
Colored Underline
.colored-link {
text-decoration-line: underline;
text-decoration-color: #2563eb;
text-underline-position: under;
}
Thick Underline
.highlight-link {
text-decoration-line: underline;
text-decoration-thickness: 3px;
text-underline-position: under;
}
Vertical Text With Left Underline
.vertical-left {
writing-mode: vertical-rl;
text-decoration: underline;
text-underline-position: left;
}
Vertical Text With Right Underline
.vertical-right {
writing-mode: vertical-rl;
text-decoration: underline;
text-underline-position: right;
}
Japanese Vertical Text
<p lang="ja" class="japanese-vertical">日本語の文章</p>
.japanese-vertical {
writing-mode: vertical-rl;
text-decoration: underline;
text-underline-position: right;
}
Combined Position
.multilingual-text {
text-decoration: underline;
text-underline-position: under right;
}
Automatic Position
.default-underline {
text-decoration: underline;
text-underline-position: auto;
}
Focus State
.interactive-link:focus-visible {
text-decoration-line: underline;
text-decoration-thickness: 3px;
text-underline-position: under;
outline: 2px solid currentColor;
outline-offset: 3px;
}
text-underline-position Example
This example compares automatic and under-positioned underlines in horizontal text, followed by left- and right-positioned underlines in vertical text.
HTML Example
<div class="underline-position-demo">
<div class="position-sample">
<strong>auto</strong>
<p class="position-auto">Typography and Underlines</p>
</div>
<div class="position-sample">
<strong>under</strong>
<p class="position-under">Typography and Underlines</p>
</div>
<div class="vertical-examples">
<div class="position-sample">
<strong>left</strong>
<p class="position-left" lang="ja">日本語の文章</p>
</div>
<div class="position-sample">
<strong>right</strong>
<p class="position-right" lang="ja">日本語の文章</p>
</div>
</div>
</div>
CSS Example
.underline-position-demo {
display: grid;
gap: 12px;
}
.underline-position-demo .position-sample {
padding: 18px 14px;
border: 1px solid #cbd5e1;
background: #f8fafc;
}
.underline-position-demo strong {
display: block;
margin-bottom: 12px;
color: #334155;
font-size: 14px;
}
.underline-position-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-position-demo .position-auto {
text-underline-position: auto;
}
.underline-position-demo .position-under {
text-underline-position: under;
}
.underline-position-demo .vertical-examples {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 12px;
}
.underline-position-demo .position-left,
.underline-position-demo .position-right {
writing-mode: vertical-rl;
height: 160px;
}
.underline-position-demo .position-left {
text-underline-position: left;
}
.underline-position-demo .position-right {
text-underline-position: right;
}
Typography and Underlines
Typography and Underlines
日本語の文章
日本語の文章
Understanding the Results
The first two samples demonstrate horizontal underline placement. The vertical samples show how the left and right values position underlines on opposite sides of vertically arranged characters.
Browser Differences
The visual difference between auto and under may be subtle for some fonts. The vertical examples are generally more useful for observing side placement.
Experiment With writing-mode
.underline-position-demo .position-left,
.underline-position-demo .position-right {
writing-mode: vertical-lr;
}
Changing the writing mode alters the arrangement of vertical text while the underline side remains controlled by the selected value.
Accessibility Considerations
Underline placement should improve text clarity without interfering with readability or the conventions of the writing system.
Keep Underlines Recognizable
Links should remain visually identifiable. Avoid underline placements that make the decoration difficult to distinguish from the text.
Avoid Glyph Overlap
Use an appropriate position when underlines intersect characters or make text difficult to read.
Respect Writing Conventions
Vertical text should use underline placement appropriate to the language and writing mode.
Use Correct Language Markup
<p lang="ja" class="vertical-text">日本語の文章</p>
The lang attribute helps identify the language for browsers and assistive technologies.
Preserve Keyboard Focus Indicators
Underline positioning does not replace a visible keyboard focus indicator.
Maintain Sufficient Contrast
Underline colors should remain distinguishable from the background.
Consider Font Differences
Underline positions may vary between fonts. Test the actual typography used on the website.
Test Text Scaling
Verify that underlines remain clear when users enlarge text or change browser zoom levels.
Avoid Relying on Underline Placement Alone
Important information should not depend solely on whether an underline appears to the left, right, or beneath the text.
Common Mistakes
Expecting the Property to Create an Underline
.example {
text-underline-position: under;
}
This declaration does not create an underline. Add text-decoration-line: underline when needed.
Using a Length Value
.example {
text-underline-position: 5px;
}
Lengths are invalid for this property. Use text-underline-offset to specify a distance.
Using none
.example {
text-underline-position: none;
}
The none keyword is not valid. Use auto to restore automatic placement.
Using top or bottom
.example {
text-underline-position: bottom;
}
The property does not accept top or bottom.
Combining left and right
.example {
text-underline-position: left right;
}
These side-placement keywords are mutually exclusive.
Confusing Position With Offset
The position property chooses the underline's placement. The offset property adjusts its distance.
Expecting left to Align Text
The left value does not left-align text. It controls underline placement in vertical writing.
Expecting right to Align Text
The right value does not right-align text.
Ignoring Writing Modes
Side-placement values are intended for vertical writing and should not be confused with horizontal text alignment.
Assuming auto and under Always Look Different
Some fonts and browsers may produce similar underline positions for these values.
Assuming Combined Values Work Everywhere
Browser support for under left and under right can vary.
Forgetting Inheritance
Descendant elements inherit the property unless they override it.
Ignoring Font Metrics
Different fonts can produce different underline placements even with the same CSS declaration.
Important Considerations
Keep the following points in mind when using text-underline-position:
- The property controls where an underline is placed relative to text.
- It does not create an underline by itself.
- The initial value is
auto. - The property is inherited.
- The main values are
auto,under,left, andright. - The specification also defines combined forms such as
under leftandunder right. - The
autovalue uses browser-determined positioning. - The
undervalue places the underline beneath the text. - The
leftandrightvalues control underline sides in vertical writing. - Side-placement values do not align the text itself.
- The property does not accept lengths or percentages.
- Use
text-underline-offsetto adjust underline distance. - Use
text-decoration-lineto create an underline. - Use
text-decoration-thicknessto control underline thickness. - Use
text-decoration-colorto control underline color. - The
writing-modeproperty affects how underline placement is interpreted. - Font metrics influence the final underline position.
- Some values may produce similar results in horizontal text.
- Combined-value support varies across browsers.
- Underlines should remain visible and readable.
- Vertical typography should follow appropriate language conventions.
- Test underline placement with the actual fonts and browsers used by the website.
Browser Support
The CSS text-underline-position property is supported in modern browsers, although support for individual values and combinations varies.
The auto and under values are broadly supported. Side-placement values and combinations should be tested in the browsers used by the website.
For current compatibility information, visit Can I Use? and search for text-underline-position.
Basic Declaration
.example {
text-decoration: underline;
text-underline-position: under;
}
Feature Detection
@supports (text-underline-position: under) {
.example {
text-underline-position: under;
}
}
Checking Combined Values
@supports (text-underline-position: under right) {
.vertical-text {
text-underline-position: under right;
}
}
Feature queries check whether a declaration is recognized, but they do not guarantee identical rendering across fonts and writing modes.
Fallback Behavior
Browsers that do not recognize a value ignore the declaration and use the previously applicable underline position.
Testing Recommendations
Test horizontal and vertical text separately, particularly when using side-placement values or multilingual content.
Specifications
The text-underline-position property is defined in the CSS Text Decoration Module Level 3 specification.
CSS Text Decoration Module Level 3 - text-underline-position Property
The specification defines the available keywords, underline placement behavior, and interactions with horizontal and vertical writing modes.
For additional documentation and examples, consult MDN Web Docs.
