CSS Shadows: The Magic of Light and Depth ✨
Welcome to Professor Lumos's Advanced Shadow Manipulation class! Today, we'll explore the differences between box-shadow and filter: drop-shadow - two different spells for creating magical depth effects! 🧙♂️
Understanding Box Shadow 📦
What is box-shadow?
Box shadow creates a rectangular shadow that follows the box model of an element, like casting a shadow spell on a magical trunk!
Box Shadow Anatomy
/* Syntax */
box-shadow: [horizontal] [vertical] [blur] [spread] [color];
/* Basic example */
.magical-box {
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
Box Shadow Properties 🎯
Box Shadow Components
.shadow-examples {
/* Basic shadow */
box-shadow: 2px 4px 6px black;
/* With spread radius */
box-shadow: 2px 4px 6px 2px rgba(0, 0, 0, 0.2);
/* Inset shadow */
box-shadow: inset 2px 4px 6px rgba(0, 0, 0, 0.2);
/* Multiple shadows */
box-shadow:
0 4px 8px rgba(0, 0, 0, 0.1),
0 8px 16px rgba(0, 0, 0, 0.1);
}
Understanding Drop Shadow 🌓
What is drop-shadow?
Drop shadow follows the actual shape of the content, including transparent parts, like casting a shadow spell that respects the true form of an object!
Drop Shadow Anatomy
/* Syntax */
filter: drop-shadow([horizontal] [vertical] [blur] [color]);
/* Basic example */
.magical-element {
filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.2));
}
Drop Shadow Properties 🎯
Drop Shadow Features
.drop-shadow-examples {
/* Basic drop shadow */
filter: drop-shadow(2px 4px 6px black);
/* Colored shadow */
filter: drop-shadow(2px 4px 6px purple);
/* Multiple filters */
filter:
drop-shadow(2px 4px 6px purple)
brightness(1.2)
contrast(1.1);
}
Box Shadow vs Drop Shadow: The Key Differences 🔄
1. Shape Behavior
How do they handle different shapes?
/* Box Shadow: Rectangular shape only */
.box-example {
/* Always rectangular shadow */
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
/* Drop Shadow: Follows content shape */
.drop-example {
/* Follows PNG transparency */
filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.2));
}
2. Performance Impact
Performance Considerations
/* Box Shadow: Better performance */
.efficient {
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
/* Drop Shadow: More CPU intensive */
.intensive {
filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.2));
}
3. Feature Support
Feature Comparison
/* Box Shadow Features */
.box-features {
/* Multiple shadows */
box-shadow:
0 4px 8px rgba(0, 0, 0, 0.2),
0 8px 16px rgba(0, 0, 0, 0.1);
/* Inset shadow */
box-shadow: inset 0 4px 8px rgba(0, 0, 0, 0.2);
/* Spread radius */
box-shadow: 0 4px 8px 4px rgba(0, 0, 0, 0.2);
}
/* Drop Shadow Features */
.drop-features {
/* Follows transparency */
filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.2));
/* Combines with other filters */
filter:
drop-shadow(0 4px 8px black)
brightness(1.2)
contrast(1.1);
}
Practical Applications 🎯
1. Card Designs
Card Shadow Effects
/* Box Shadow Card */
.box-shadow-card {
background: white;
border-radius: 8px;
padding: 20px;
/* Layered shadow effect */
box-shadow:
0 2px 4px rgba(0, 0, 0, 0.1),
0 4px 8px rgba(0, 0, 0, 0.1),
0 8px 16px rgba(0, 0, 0, 0.1);
/* Hover effect */
transition: box-shadow 0.3s ease;
&:hover {
box-shadow:
0 4px 8px rgba(0, 0, 0, 0.2),
0 8px 16px rgba(0, 0, 0, 0.2),
0 16px 32px rgba(0, 0, 0, 0.2);
}
}
/* Drop Shadow Card */
.drop-shadow-card {
/* For irregular shapes */
filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.2));
/* SVG content */
svg {
width: 100%;
height: auto;
}
}
2. Text Effects
Text Shadow Effects
/* Box Shadow Text */
.box-shadow-text {
/* Glowing text effect */
box-shadow: 0 0 10px purple;
color: white;
}
/* Drop Shadow Text */
.drop-shadow-text {
/* Crisp text shadow */
filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, 0.5));
}
3. Complex Shapes
Shape Shadow Examples
/* PNG Image with Transparency */
.magical-creature {
/* Drop shadow follows creature shape */
filter: drop-shadow(2px 4px 6px rgba(0, 0, 0, 0.3));
}
/* SVG Icon */
.magical-icon {
filter: drop-shadow(0 2px 4px purple);
/* Hover effect */
&:hover {
filter: drop-shadow(0 4px 8px gold);
}
}
Choosing Between Box Shadow and Drop Shadow 🤔
Decision Guide
When to Use Each
/* Use Box Shadow When: */
.use-box-shadow {
/* Regular shapes */
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
/* Multiple shadows needed */
box-shadow:
0 2px 4px rgba(0, 0, 0, 0.1),
0 4px 8px rgba(0, 0, 0, 0.1);
/* Inner shadow needed */
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2);
}
/* Use Drop Shadow When: */
.use-drop-shadow {
/* Irregular shapes/PNG */
filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.2));
/* SVG elements */
filter: drop-shadow(2px 2px 2px purple);
/* Combined with other filters */
filter:
drop-shadow(2px 2px 2px purple)
brightness(1.1);
}
Advanced Effects 🌟
Combining Both Types
Combined Shadow Effects
.magical-element {
/* Box shadow for main element */
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
/* Drop shadow for irregular content */
.inner-shape {
filter: drop-shadow(2px 2px 4px purple);
}
/* Hover effects */
&:hover {
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
.inner-shape {
filter: drop-shadow(4px 4px 8px gold);
}
}
}
Practical Tasks 📚
Task 1: Create a Magical Button System
Task
Create buttons with different shadow effects for:
- Normal state
- Hover state
- Active state
- Disabled state
Answer
.magical-button {
/* Base styles */
position: relative;
padding: 0.8em 1.6em;
border: none;
border-radius: 4px;
background: linear-gradient(45deg, purple, indigo);
color: white;
transition: all 0.3s ease;
/* Normal state */
box-shadow:
0 2px 4px rgba(0, 0, 0, 0.1),
0 4px 8px rgba(0, 0, 0, 0.1);
/* Hover state */
&:hover {
transform: translateY(-2px);
box-shadow:
0 4px 8px rgba(0, 0, 0, 0.2),
0 8px 16px rgba(0, 0, 0, 0.2);
}
/* Active state */
&:active {
transform: translateY(1px);
box-shadow:
0 2px 4px rgba(0, 0, 0, 0.2);
}
/* Disabled state */
&:disabled {
background: #ccc;
box-shadow: none;
cursor: not-allowed;
}
/* Magical variant */
&.magical {
overflow: visible;
&::before {
content: '';
position: absolute;
inset: -3px;
background: inherit;
filter: blur(8px);
opacity: 0.5;
z-index: -1;
transition: opacity 0.3s ease;
}
&:hover::before {
opacity: 0.8;
}
}
}
Task 2: Create Complex Shadow Effects
Task
Create a card with:
- Layered shadows
- Inner shadows
- Shape-following shadows for icons
- Hover effects
Answer
.magical-card {
/* Base styles */
position: relative;
padding: 2rem;
background: white;
border-radius: 12px;
overflow: visible;
/* Complex shadow system */
box-shadow:
/* Ambient light */
0 4px 8px rgba(0, 0, 0, 0.1),
/* Directional light */
4px 8px 16px rgba(0, 0, 0, 0.1),
/* Inner shadow */
inset 0 2px 4px rgba(255, 255, 255, 0.5),
inset 0 -2px 4px rgba(0, 0, 0, 0.1);
/* SVG icon */
.icon {
filter:
drop-shadow(2px 2px 2px rgba(0, 0, 0, 0.2))
drop-shadow(0 0 4px rgba(128, 0, 128, 0.3));
transition: all 0.3s ease;
}
/* Hover state */
&:hover {
box-shadow:
0 8px 16px rgba(0, 0, 0, 0.1),
8px 16px 32px rgba(0, 0, 0, 0.1),
inset 0 2px 4px rgba(255, 255, 255, 0.5),
inset 0 -2px 4px rgba(0, 0, 0, 0.1);
.icon {
filter:
drop-shadow(4px 4px 4px rgba(0, 0, 0, 0.3))
drop-shadow(0 0 8px rgba(128, 0, 128, 0.5));
transform: translateY(-2px);
}
}
/* Magical glow effect */
&::before {
content: '';
position: absolute;
inset: -5px;
background: linear-gradient(45deg, purple, gold);
filter: blur(15px);
opacity: 0;
transition: opacity 0.3s ease;
z-index: -1;
border-radius: inherit;
}
&:hover::before {
opacity: 0.5;
}
}
Additional Study Materials 📖
References 📚
- CSS Backgrounds and Borders Module Level 3
- Filter Effects Module Level 1
- CSS Working Group Specifications
Conclusion 🎉
Remember, young shadow wizards:
- Box shadow for rectangular elements
- Drop shadow for irregular shapes
- Consider performance implications
- Test across browsers
- Use shadows purposefully
Dumbledore's Final Words
"Like the interplay of light and shadow in our magical world, these CSS properties each serve their purpose. Understanding when to use each is the mark of a true shadow master!" 🧙♂️