CSS Shadows
Add depth, focus, and modern visual effects using CSS shadows.
What are CSS Shadows?
CSS Shadows are visual effects used to create depth by adding shadows behind elements or text.
- Improve UI depth and hierarchy
- Highlight important elements
- Make designs look modern and realistic
- No images or JavaScript required
Real-Life Example: Just like real objects cast shadows under light, UI elements use shadows to feel more natural.
Types of CSS Shadows
- box-shadow → Shadow around elements
- text-shadow → Shadow behind text
Each serves a different purpose and should be used thoughtfully.
CSS box-shadow
The box-shadow property adds shadow effects around elements like cards, buttons, and containers.
Basic Syntax:
box-shadow: offset-x offset-y blur-radius spread-radius color;
offset-x→ Horizontal positionoffset-y→ Vertical positionblur-radius→ Softness of shadowspread-radius→ Size expansioncolor→ Shadow color
Basic box-shadow Example
Card Shadow
.card {
width: 300px;
padding: 20px;
background: #ffffff;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
border-radius: 8px;
}
Explanation: The card looks lifted from the page, making it stand out clearly.
Multiple Shadows
You can apply more than one shadow by separating them with commas.
.box {
box-shadow:
0 2px 5px rgba(0,0,0,0.2),
0 8px 20px rgba(0,0,0,0.15);
}
This technique is commonly used in professional UI designs.
CSS text-shadow
The text-shadow property adds shadows behind text, improving readability and style.
Syntax:
text-shadow: offset-x offset-y blur-radius color;
Text Shadow Example
Heading Shadow
h1 {
font-size: 48px;
color: #1e293b;
text-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
}
Use Case: Text shadows are useful on banners, hero sections, and dark backgrounds.
Inner Shadow
You can create an inner shadow using the inset keyword.
.input-box {
box-shadow: inset 0 2px 6px rgba(0,0,0,0.3);
}
This is commonly used for input fields and neumorphism designs.
Best Practices for CSS Shadows
- Use soft shadows for modern UI
- Avoid harsh black shadows
- Prefer
rgba()colors - Do not overuse shadows
- Keep consistency across components
Pro Tip: Shadows should support content, not steal attention.