CSS Shadows
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;
}
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. |
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);
}
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. |
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. |
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. |
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;
}
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. |
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 |
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;
}
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.
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);
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 |
14. CSS Shadow Interview Questions
- What is the purpose of the
box-shadowproperty? - What is the difference between
box-shadowandtext-shadow? - What does the
insetkeyword do? - How do you create multiple shadows?
- What is the purpose of the blur radius?
- What is the spread radius in
box-shadow? - How can shadows improve UI design?
- Can shadows be animated with CSS transitions?
- Which elements commonly use
box-shadow? - Are CSS shadows supported by modern browsers?
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS Shadows.
- ✔ The
box-shadowproperty. - ✔ Shadow values and syntax.
- ✔ Multiple box shadows.
- ✔ The
text-shadowproperty. - ✔ Inset shadows.
- ✔ Practical examples and hover effects.
- ✔ Advantages of CSS shadows.
- ✔ Best Practices and Common Mistakes.
- ✔ Browser Support and Interview Questions.
box-shadow and text-shadow to build modern user interfaces.
Quick Quiz
Which CSS property adds a shadow around an HTML element?