CSS Shadows

CSS Shadows are used to add depth and visual appeal to elements and text. CSS provides two shadow properties: box-shadow for HTML elements and text-shadow for text.

1. Introduction to CSS Shadows

Shadows make web pages look modern by giving elements a three-dimensional appearance. They help highlight important content and improve the overall user interface.

Why Use CSS Shadows?
  • Create depth and elevation.
  • Highlight buttons and cards.
  • Improve visual appearance.
  • Create attractive hover effects.
  • Enhance readability of text.

.box{

    box-shadow:5px 5px 10px gray;

}
CSS shadows make websites more attractive without using images.

2. box-shadow Property

The box-shadow property adds one or more shadows around an HTML element.


.card{

    box-shadow:8px 8px 15px gray;

}
Property Description
box-shadow Adds shadow around an element.
The box-shadow property is commonly used on cards, buttons, images, forms, and containers.

3. Shadow Values (Horizontal, Vertical, Blur, Spread)

A box shadow is created using several values that control its position, blur, size, and color.


box-shadow:
10px 10px 15px 5px gray;
Value Description
Horizontal Offset Moves shadow left or right.
Vertical Offset Moves shadow up or down.
Blur Radius Controls shadow softness.
Spread Radius Expands or shrinks the shadow.
Color Specifies the shadow color.

.box{

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

}
Increasing the blur radius creates a softer and more natural shadow.

4. Multiple Box Shadows

CSS allows multiple shadows by separating each shadow with a comma. This creates rich and layered shadow effects.


.box{

    box-shadow:

    2px 2px 5px gray,

    -2px -2px 5px lightgray;

}
Feature Benefit
Multiple Shadows Create layered effects.
Comma Separator Separates each shadow.
Multiple shadows are commonly used in modern UI and neumorphism designs.

5. text-shadow Property

The text-shadow property adds a shadow to text, making headings and titles more attractive.


h1{

    text-shadow:

    2px 2px 4px gray;

}

h2{

    text-shadow:

    1px 1px 3px blue;

}
Property Purpose
text-shadow Adds shadow to text.
box-shadow Adds shadow to elements.
Use text shadows carefully to improve readability without making text difficult to read.

6. Inset Shadows

The inset keyword creates an inner shadow inside an element instead of outside it. Inset shadows are commonly used to create pressed buttons, input fields, and modern UI effects.


.box{

    box-shadow:

    inset 4px 4px 8px gray;

}

.input{

    box-shadow:

    inset 0 2px 5px rgba(0,0,0,0.3);

}
Shadow Type Description
Normal Shadow Appears outside the element.
Inset Shadow Appears inside the element.
Use inset shadows to create pressed or recessed effects.

7. Practical Shadow Examples

Example 1 : Card Shadow

.card{

    box-shadow:

    0 4px 10px rgba(0,0,0,0.2);

}

Example 2 : Button Shadow

button{

    box-shadow:

    2px 2px 6px gray;

}

Example 3 : Image Shadow

img{

    box-shadow:

    5px 5px 15px rgba(0,0,0,0.4);

}

Example 4 : Text Shadow

h1{

    text-shadow:

    2px 2px 5px gray;

}
Shadow effects are widely used in cards, buttons, forms, images, and headings.

8. Shadow Effects with Hover

Combining shadows with the :hover pseudo-class creates interactive and attractive user interface effects.


.card{

    box-shadow:

    2px 2px 6px gray;

}

.card:hover{

    box-shadow:

    8px 8px 20px gray;

}

button:hover{

    box-shadow:

    0 6px 15px rgba(0,0,0,0.4);

}
State Effect
Normal Light shadow.
Hover Deeper shadow.
Hover shadows provide users with visual feedback and improve interaction.

9. Advantages of CSS Shadows

  • Adds depth to web elements.
  • Improves user interface design.
  • Highlights important content.
  • Creates attractive hover effects.
  • Works without image files.
  • Easy to customize using CSS.
  • Enhances readability with text shadows.
Feature Benefit
Modern Appearance ✔ Yes
Easy to Use ✔ Yes
Responsive ✔ Yes
CSS shadows improve the appearance of websites while keeping the design lightweight.

10. CSS Shadow Comparison Table

Property Applies To Common Use
box-shadow HTML Elements Cards, Buttons, Images
text-shadow Text Titles, Headings
Inset Shadow Inside Elements Input Fields, Pressed Buttons

.card{

    box-shadow:

    0 8px 20px rgba(0,0,0,0.25);

}

h1{

    text-shadow:

    2px 2px 5px gray;

}
Choose the appropriate shadow type based on whether you want to style an element, text, or create an inset effect.

11. CSS Shadow Best Practices

Using CSS shadows correctly improves the appearance of your website without making the design look cluttered.

  • Use subtle shadows for a clean and professional design.
  • Maintain consistency throughout the website.
  • Choose appropriate blur and spread values.
  • Avoid extremely dark or oversized shadows.
  • Use hover shadows to improve user interaction.
  • Combine shadows with border-radius for modern UI.
  • Test shadows on different screen sizes.
  • Keep text shadows light to maintain readability.
Well-designed shadows create depth while keeping the interface clean and elegant.

12. Common CSS Shadow Mistakes

Mistake Correct Practice
Using very large shadows. Use soft and balanced shadows.
Applying shadows to every element. Use shadows only where needed.
Using dark text shadows. Keep text shadows subtle.
Ignoring hover effects. Add interactive shadow effects.
Using inconsistent shadow styles. Maintain a consistent design.

❌ Poor

box-shadow:20px 20px 40px black;

✔ Better

box-shadow:0 4px 10px rgba(0,0,0,0.2);
Excessive shadows can make a website look cluttered and reduce readability.

13. Browser Support

Both box-shadow and text-shadow are fully supported in all modern web browsers.

Browser Support
Google Chrome ✔ Supported
Mozilla Firefox ✔ Supported
Microsoft Edge ✔ Supported
Safari ✔ Supported
Opera ✔ Supported
CSS shadow properties work reliably in all modern browsers.

14. CSS Shadow Interview Questions

  1. What is the purpose of the box-shadow property?
  2. What is the difference between box-shadow and text-shadow?
  3. What does the inset keyword do?
  4. How do you create multiple shadows?
  5. What is the purpose of the blur radius?
  6. What is the spread radius in box-shadow?
  7. How can shadows improve UI design?
  8. Can shadows be animated with CSS transitions?
  9. Which elements commonly use box-shadow?
  10. Are CSS shadows supported by modern browsers?
These interview questions help reinforce your understanding of CSS shadow properties.

15. Lesson Summary

In this lesson, you learned:
  • ✔ Introduction to CSS Shadows.
  • ✔ The box-shadow property.
  • ✔ Shadow values and syntax.
  • ✔ Multiple box shadows.
  • ✔ The text-shadow property.
  • ✔ Inset shadows.
  • ✔ Practical examples and hover effects.
  • ✔ Advantages of CSS shadows.
  • ✔ Best Practices and Common Mistakes.
  • ✔ Browser Support and Interview Questions.
Congratulations! You have successfully completed the CSS Shadows lesson. You can now create attractive cards, buttons, images, and text using box-shadow and text-shadow to build modern user interfaces.

Quick Quiz

Which CSS property adds a shadow around an HTML element?