-->

css - Shadow Effects

CSS Shadow Effects allow you to add depth and dimensionality to elements using box shadows and text shadows. These effects enhance visual aesthetics and improve user interface design.

Types of Shadow Effects

Box Shadow: Adds shadow to elements like divs, buttons, and containers.

Text Shadow: Applies shadow to text for stylistic emphasis.

1. Box Shadow

The box-shadow property adds shadow to the edges of an element.

Syntax:

box-shadow: offset-x offset-y blur-radius spread-radius color;

offset-x: Horizontal shadow position (positive for right, negative for left).

offset-y: Vertical shadow position (positive for down, negative for up).

blur-radius: Amount of blur; higher values create softer shadows (optional).

spread-radius: Increases or decreases the size of the shadow (optional).

color: The color of the shadow.

Example: Basic Box Shadow

<div style="width: 200px; height: 100px; background: lightblue; box-shadow: 10px 10px 5px gray;"></div>

A shadow appears 10px to the right and 10px down with a 5px blur.

Advanced Example: Multiple Shadows

<div style="width: 200px; height: 100px; background: lightgreen; box-shadow: 5px 5px 10px rgba(0,0,0,0.5), -5px -5px 10px rgba(255,255,255,0.7);"></div>

Shadows on both sides, creating a glowing effect.

Inset Box Shadow

<div style="width: 200px; height: 100px; background: lightcoral; box-shadow: inset 5px 5px 10px rgba(0, 0, 0, 0.5);"></div>

inset: Creates an inner shadow.

2. Text Shadow

The text-shadow property applies shadows to text.

Syntax:

text-shadow: offset-x offset-y blur-radius color;

Example: Basic Text Shadow

<h1 style="color: blue; text-shadow: 2px 2px 5px gray;">Hello World</h1>

Shadow appears 2px to the right and 2px down with a blur radius of 5px.

Advanced Example: Neon Effect

<h1 style="color: white; text-shadow: 0 0 10px blue, 0 0 20px cyan, 0 0 30px lightblue;">Glow Text</h1>

Multiple shadows create a glowing neon effect.

Subtle Shadow

<p style="font-size: 18px; color: black; text-shadow: 1px 1px 2px lightgray;">Soft Shadow</p>

Adds a subtle shadow for a professional look.

Customizing Shadow Effects

1. Color

Use rgba for transparent shadows.

Example: box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.5);

2. Blur

Higher values result in softer, more diffused shadows.

Example: box-shadow: 10px 10px 20px black;

3. Spread

Positive values increase the shadow size, while negative values shrink it.

Example: box-shadow: 5px 5px 5px 5px red;

Combining Shadows

You can combine both box-shadow and text-shadow for a cohesive design.

Example: Card Design

<div style="width: 300px; padding: 20px; background: white; box-shadow: 0 4px 8px rgba(0,0,0,0.2); text-shadow: 1px 1px 2px gray;">

    <h2 style="margin: 0;">Card Title</h2>

    <p>This is a card with shadows.</p>

</div>

Browser Compatibility

CSS Shadow Effects are supported in most modern browsers. Older browsers may require fallback designs.

Conclusion

CSS Shadow Effects add depth and visual appeal to your web elements, enhancing user experience. Experiment with different properties like blur, spread, and colors to create unique designs!