Start Coding

CSS Text Decoration

The text-decoration property in CSS is a powerful tool for enhancing the visual appearance of text. It allows you to add various decorative lines to your text, such as underlines, overlines, and line-throughs.

Basic Syntax

The basic syntax for the text-decoration property is straightforward:

selector {
    text-decoration: value;
}

The value can be one of the following:

  • none: Removes any existing decoration
  • underline: Adds a line below the text
  • overline: Adds a line above the text
  • line-through: Adds a line through the middle of the text

Examples

Let's look at some practical examples:

/* Underline text */
.underlined {
    text-decoration: underline;
}

/* Remove default underline from links */
a {
    text-decoration: none;
}

/* Strikethrough text */
.strikethrough {
    text-decoration: line-through;
}

Advanced Usage

The text-decoration property can be further customized using additional properties:

  • text-decoration-color: Sets the color of the decoration line
  • text-decoration-style: Defines the style of the line (solid, wavy, dotted, etc.)
  • text-decoration-thickness: Specifies the thickness of the line

Best Practices

  • Use text-decoration: none; to remove underlines from links when styling them differently
  • Combine text-decoration with CSS Text Shadow for more visually appealing effects
  • Consider accessibility when using decorations, especially for important text or links
  • Use text-decoration-thickness to ensure decorations are visible on different backgrounds

Browser Compatibility

The basic text-decoration property has excellent browser support. However, some of the more advanced properties like text-decoration-thickness may not be supported in older browsers. Always check browser compatibility when using these features.

Related Concepts

To further enhance your text styling skills, explore these related CSS concepts:

By mastering text-decoration and related properties, you'll be able to create visually appealing and readable text styles in your web projects. Remember to balance aesthetics with readability and accessibility for the best user experience.