Lesson 15: Mastering Text Decoration
Learn how to enhance your text with decorative lines using CSS. Understand underlines, overlines, line-throughs, and how to customize them for modern web design.
Start LearningUnderstanding Text Decoration
Text decoration in CSS allows you to add visual elements to your text, such as underlines, overlines, and line-throughs. These decorations can enhance readability, indicate importance, or create visual hierarchy in your content.
Text Decoration Examples
CSS Text-Decoration Syntax
/* Basic text decoration */ .link { text-decoration: underline; } .old-price { text-decoration: line-through; } .special-heading { text-decoration: overline; } /* Removing default underlines */ a { text-decoration: none; } /* Combining multiple decorations */ .double-decoration { text-decoration: underline overline; } /* Using with pseudo-elements */ h2::after { content: ""; display: block; width: 50px; height: 2px; background: var(--primary); margin: 5px auto; }
Advanced Decoration Properties
Modern CSS provides additional properties to customize the appearance of text decorations:
text-decoration-color
Sets the color of the decoration line
text-decoration-style
Defines the style of the line (solid, double, dotted, dashed, wavy)
text-decoration-thickness
Sets the thickness of the decoration line
text-underline-offset
Sets the offset distance of an underline from its original position
Advanced Text-Decoration Syntax
/* Customizing decoration appearance */ .custom-underline { text-decoration: underline; text-decoration-color: #ff9800; text-decoration-style: wavy; text-decoration-thickness: 2px; text-underline-offset: 4px; } /* Shorthand version */ .shorthand-decoration { text-decoration: underline wavy #ff9800 2px; } /* Using with transitions */ .animated-link { text-decoration: underline; text-decoration-color: transparent; transition: text-decoration-color 0.3s; } .animated-link:hover { text-decoration-color: var(--primary); }
Interactive Decoration Demo
Experiment with different text decoration options and see the results in real-time:
.preview { text-decoration: underline; }
Real-World Use Cases
Link Styling
Hover over this link to see the effect
Create distinctive link styles with custom underlines that stand out from regular text.
Price Display
$49.99 $39.99
Use line-through to show original prices and discounts in e-commerce interfaces.
Section Headings
Featured Products
Enhance section headings with decorative overlines for visual hierarchy and style.
Best Practices
Follow these guidelines for effective use of text decorations:
Text Decoration vs. Border
Creative Decoration Techniques
/* Gradient underline */ .gradient-underline { background: linear-gradient(to right, transparent 0%, #ff9800 100%) bottom no-repeat; background-size: 100% 2px; padding-bottom: 3px; } /* Animated underline */ .animated-underline { position: relative; text-decoration: none; } .animated-underline::after { content: ''; position: absolute; bottom: -2px; left: 0; width: 0; height: 2px; background: var(--primary); transition: width 0.3s; } .animated-underline:hover::after { width: 100%; } /* Custom line-through */ .custom-line-through { position: relative; } .custom-line-through::before { content: ''; position: absolute; top: 50%; left: 0; width: 100%; height: 2px; background: var(--error); transform: translateY(-50%); }
Common Mistakes to Avoid
Be aware of these pitfalls when using text decorations:
Overusing Underlines
Underlining every important word makes content look cluttered and reduces the effectiveness of underlines for actual links.
Solution: Use underlines primarily for links and sparingly for other emphasis.
Poor Color Contrast
This underline has insufficient contrast against the background
Solution: Ensure text decorations have sufficient contrast with both text and background.
Inconsistent Link Styling
Regular text without underline and with underline creates confusion.
Solution: Maintain consistent styling for all interactive elements.
Next Steps
Now that you understand text decorations, continue to the next lesson to learn about font styles and advanced typography techniques.