CSS text-decoration is a fundamental property that allows developers to add visual emphasis and styling to text through decorative lines. Whether you’re creating links, highlighting important content, or adding visual flair to your typography, understanding text-decoration properties is essential for modern web design.
Understanding CSS Text-Decoration Properties
The CSS text-decoration property is actually a shorthand for several individual properties that control different aspects of text decoration:
- text-decoration-line: Specifies the type of line (underline, overline, line-through)
- text-decoration-color: Sets the color of the decoration line
- text-decoration-style: Defines the style of the line (solid, dashed, dotted, etc.)
- text-decoration-thickness: Controls the thickness of the decoration line
Basic Text-Decoration Syntax
The basic syntax for text-decoration follows this pattern:
Text-Decoration-Line Values
The text-decoration-line property accepts several values that determine where the decorative line appears:
Basic Line Types
This text has an underline decoration
This text has an overline decoration
This text has a line-through decoration
This text has no decoration
.overline { text-decoration-line: overline; }
.strikethrough { text-decoration-line: line-through; }
.no-decoration { text-decoration-line: none; }
Multiple Decoration Lines
You can combine multiple decoration lines by separating them with spaces:
Text with both underline and overline
Text with underline and strikethrough
Text with all three decorations
text-decoration-line: underline line-through;
text-decoration-line: underline overline line-through;
Text-Decoration-Style Options
The text-decoration-style property controls the visual appearance of the decoration line. Here are all available styles:
Decoration Styles
Solid underline (default)
Double underline
Dotted underline
Dashed underline
Wavy underline
Colored solid overline
Double blue strikethrough
Wavy green underline
text-decoration-style: double;
text-decoration-style: dotted;
text-decoration-style: dashed;
text-decoration-style: wavy;
Customizing Text-Decoration Colors
The text-decoration-color property allows you to set a different color for the decoration line than the text itself:
Color Examples
Dark text with red underline
Blue text with orange overline
Green text with purple strikethrough
Navy text with orange wavy underline
color: #333;
text-decoration-line: underline;
text-decoration-color: #e74c3c;
}
Controlling Text-Decoration Thickness
Modern browsers support the text-decoration-thickness property, which allows precise control over line thickness:
Thickness Variations
Thin underline (1px)
Medium underline (2px)
Thick underline (4px)
Relative thickness (0.1em)
Auto thickness (browser default)
text-decoration-thickness: 2px;
text-decoration-thickness: 0.1em;
text-decoration-thickness: auto;
Shorthand Text-Decoration Property
You can combine all text-decoration properties using the shorthand syntax:
Shorthand Examples
Shorthand: underline solid red 2px
Shorthand: overline dashed blue 3px
Shorthand: line-through wavy purple 1px
text-decoration: [line] [style] [color] [thickness];
/* Examples */
text-decoration: underline solid red 2px;
text-decoration: overline dashed blue 3px;
text-decoration: line-through wavy purple 1px;
Interactive Text-Decoration Demo
Try this interactive demo to experiment with different text-decoration combinations:
Interactive Demo
Sample Text for Decoration
text-decoration: underline solid blue 2px;
Practical Use Cases and Examples
Link Styling
Text-decoration is commonly used for customizing link appearances:
Dashed underline link
Colored thick underline
Wavy underline link
a { text-decoration: none; }
/* Custom link styles */
.fancy-link {
text-decoration: underline wavy #f39c12 2px;
}
/* Hover effects */
a:hover {
text-decoration: underline solid currentColor 3px;
}
Content Highlighting
Use text-decoration to highlight different types of content:
Important terms can be highlighted with dotted underlines.
Outdated information can be crossed out while keeping it visible.
Special emphasis can use both overline and underline.
Spelling suggestions often use wavy underlines.
text-decoration: underline dotted #e74c3c;
}
.outdated {
text-decoration: line-through;
}
.spell-check {
text-decoration: underline wavy #27ae60;
}
Advanced Techniques and Browser Support
Text-Decoration with Pseudo-elements
For more control over decoration appearance, you can use pseudo-elements:
Here’s text with a custom gradient underline created using pseudo-elements.
position: relative;
text-decoration: none;
}
.custom-underline::after {
content: ”;
position: absolute;
left: 0;
bottom: -2px;
width: 100%;
height: 3px;
background: linear-gradient(45deg, #3498db, #e74c3c);
border-radius: 2px;
}
Accessibility Considerations
When using text-decoration, consider these accessibility guidelines:
- Color contrast: Ensure decoration colors have sufficient contrast against the background
- Don’t rely solely on color: Use decoration styles (dotted, wavy) to convey meaning, not just color
- Link identification: Maintain clear visual distinction for links, especially when removing default underlines
- Screen readers: Text-decoration is purely visual and doesn’t affect screen reader interpretation
Browser Support and Compatibility
Text-decoration properties have excellent browser support:
- text-decoration (shorthand) – All browsers
- text-decoration-line – All modern browsers
- text-decoration-color – All modern browsers
- text-decoration-style – All modern browsers
- text-decoration-thickness – Modern browsers only (IE not supported)
- Some style values like ‘wavy’ may not work in older browsers
Performance and Best Practices
Follow these best practices when using text-decoration:
- Use shorthand when possible:
text-decoration: underline solid red
is more concise than separate properties - Avoid excessive decorations: Multiple decorations can make text harder to read
- Consider performance: CSS text-decoration is more performant than creating decorations with borders or pseudo-elements
- Maintain consistency: Use consistent decoration styles throughout your site for similar content types
- Test across browsers: Always test decoration styles across different browsers and devices
Common Issues and Solutions
Decoration Not Appearing
If text-decoration isn’t showing, check these common issues:
- Parent element has
text-decoration: none
- Color matches the background
- Thickness is set to 0
- Browser doesn’t support the specific style
Inconsistent Appearance
For consistent cross-browser appearance:
.consistent-decoration {
text-decoration: none; /* Reset */
text-decoration-line: underline;
text-decoration-style: solid;
text-decoration-color: currentColor;
text-decoration-thickness: 1px;
}
Conclusion
CSS text-decoration properties provide powerful tools for enhancing typography and creating visual emphasis in web design. From simple underlines to complex multi-style decorations, these properties offer flexibility while maintaining excellent browser support. By understanding the individual properties and their combinations, you can create engaging, accessible, and visually appealing text treatments that enhance your users’ reading experience.
Remember to always consider accessibility, test across different browsers, and use decorations purposefully to support your content’s meaning rather than just for visual appeal. With these techniques in your toolkit, you’ll be able to create professional, polished text styling that works across all modern browsers.