filter: drop-shadow()
The CSS filter
property applies graphical effects like blurring or color shifting to an element. The drop-shadow()
function creates a shadow that conforms to the shape of an element's content, unlike box-shadow
which creates a rectangular shadow. This is particularly useful for creating shadows on transparent PNGs or complex shapes.
Example 1: Basic Drop Shadow
.basic-shadow {
/* Applies a simple drop shadow */
filter: drop-shadow(8px 8px 10px #555); /* offset-x | offset-y | blur-radius | color */
}
Explanation: This code adds a basic drop shadow to an element. The shadow is offset by 8 pixels horizontally and 8 pixels vertically, has a blur radius of 10 pixels, and its color is a dark gray.
Example 2: Colored Drop Shadow
.colored-shadow {
/* Creates a blue, semi-transparent drop shadow */
filter: drop-shadow(5px 5px 5px rgba(0, 0, 255, 0.6));
}
Explanation: This example demonstrates how to apply a colored shadow. We use an rgba()
color value to create a semi-transparent blue shadow, allowing the background to show through.
Example 3: No Blur Drop Shadow
.no-blur-shadow {
/* A sharp, solid drop shadow without any blur */
filter: drop-shadow(10px 10px 0 #333); /* blur-radius is set to 0 */
}
Explanation: By setting the blur radius to 0, this code creates a crisp, hard-edged shadow. This effect can be used for creating a simple, stylized duplicate of the element's shape.
Example 4: Negative Offset Drop Shadow
.negative-offset-shadow {
/* Shadow is cast to the top and left of the element */
filter: drop-shadow(-6px -6px 8px #888);
}
Explanation: This code uses negative values for the horizontal and vertical offsets. This causes the shadow to appear to the top and left of the element, as if the light source is from the bottom right.
Example 5: Multiple Drop Shadows
.multiple-shadows {
/* Layering multiple drop-shadow() functions for a unique effect */
filter: drop-shadow(5px 5px 5px #f00) drop-shadow(-5px -5px 5px #00f);
}
Explanation: You can apply multiple drop-shadow()
filters to a single element. In this example, a red shadow is cast to the bottom-right and a blue shadow is cast to the top-left, creating a layered, 3D-like effect.
Example 6: Drop Shadow on Hover
.hover-shadow {
transition: filter 0.3s ease-in-out; /* Smooth transition for the filter property */
}
.hover-shadow:hover {
/* A more prominent shadow appears on hover */
filter: drop-shadow(0 0 15px rgba(255, 165, 0, 0.8));
}
Explanation: This example creates an interactive effect where the drop shadow appears or changes when the user hovers over the element. The transition
property ensures the effect animates smoothly.
Example 7: Animating a Drop Shadow
@keyframes pulse {
0% {
filter: drop-shadow(0 0 5px #ffc107);
}
50% {
filter: drop-shadow(0 0 20px #ffc107);
}
100% {
filter: drop-shadow(0 0 5px #ffc107);
}
}
.animated-shadow {
/* Applies the pulsing animation to the drop shadow */
animation: pulse 2s infinite;
}
Explanation: This code uses a @keyframes
animation to create a "pulsing" glow effect. The drop-shadow()
blur radius expands and contracts over two seconds, and the animation repeats indefinitely.