Start Coding

CSS Text Shadow

The text-shadow property in CSS is a powerful tool for adding depth and visual interest to text on web pages. It allows developers to create shadow effects behind text content, enhancing readability and aesthetic appeal.

Syntax and Usage

The basic syntax for text-shadow is as follows:

text-shadow: horizontal-offset vertical-offset blur-radius color;

Each component serves a specific purpose:

  • horizontal-offset: Moves the shadow right (positive) or left (negative)
  • vertical-offset: Moves the shadow down (positive) or up (negative)
  • blur-radius: Optional. Determines the fuzziness of the shadow
  • color: Specifies the color of the shadow

Examples

Let's explore some common use cases for text-shadow:

1. Simple Shadow

h1 {
    text-shadow: 2px 2px #888888;
}

This creates a subtle gray shadow 2 pixels to the right and 2 pixels down from the text.

2. Blurred Shadow with Color

p {
    text-shadow: 1px 1px 2px #ff0000;
}

This applies a slightly blurred red shadow to paragraph text.

Multiple Shadows

You can apply multiple shadows to text by separating each shadow definition with a comma:

h2 {
    text-shadow: 2px 2px 5px #000000, 0 0 3px #ff0000;
}

This creates a black shadow with a red glow effect.

Best Practices

  • Use text-shadow sparingly to maintain readability
  • Consider contrast between text, shadow, and background colors
  • Test shadow effects across different devices and screen sizes
  • Combine with other CSS text formatting properties for enhanced effects

Browser Support and Performance

text-shadow is widely supported in modern browsers. However, be mindful of performance when using complex shadow effects, especially on text-heavy pages or mobile devices.

Related Concepts

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

By mastering text-shadow along with other text-related CSS properties, you'll be able to create visually striking and readable web typography.