CSS Shadows
Add depth and visual hierarchy to your web elements using CSS's powerful box-shadow and text-shadow properties.
Understanding Shadows in CSS
Shadows are a common design element used to create a sense of depth, separation, and visual hierarchy on a webpage. In CSS, you can apply shadows to elements using the `box-shadow` property for boxes and the `text-shadow` property for text. These properties allow you to control the shadow's offset, blur, spread, and color, enabling a wide range of visual effects from subtle lifts to dramatic glows.
Using shadows effectively can significantly improve the user experience by guiding the user's eye, indicating interactivity, and making the interface feel more tangible.
💡 Design Principle
Subtle shadows can enhance the perceived depth of an element, making it appear to lift off the background, while more pronounced shadows can indicate active or interactive elements.
The `box-shadow` Property
The `box-shadow` property applies one or more drop shadows to an element. It's highly customizable, allowing for complex effects.
Syntax and Values
The syntax for `box-shadow` is typically `box-shadow: offset-x offset-y blur-radius spread-radius color inset;`
- `offset-x`: Required. The horizontal offset of the shadow. Positive values cast the shadow to the right, negative to the left.
- `offset-y`: Required. The vertical offset of the shadow. Positive values cast the shadow downwards, negative upwards.
- `blur-radius`: Optional. The blur distance. A larger value creates a more blurred shadow. Default is `0`.
- `spread-radius`: Optional. The spread distance. Positive values expand the shadow, negative values shrink it. Default is `0`.
- `color`: Optional. The color of the shadow. Default is the browser's default text color.
- `inset`: Optional. Changes the shadow from an outer (default) drop shadow to an inner shadow.
.simple-shadow { box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3); /* Right, down, blur, color */ } .larger-shadow { box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2); /* Only vertical offset and blur */ } .inset-shadow { box-shadow: inset 0 0 10px var(--primary-color); /* Inner shadow */ } .multiple-shadows { box-shadow: 3px 3px 5px rgba(0, 0, 0, 0.4), -3px -3px 5px rgba(255, 255, 255, 0.5); /* Multiple shadows */ }
Simple Shadow
Larger Shadow
Inset Shadow
The `text-shadow` Property
The `text-shadow` property applies one or more drop shadows to the text content of an element. It's commonly used for visual effects like embossing, glowing text, or simple drop shadows for readability.
Syntax and Values
The syntax for `text-shadow` is `text-shadow: offset-x offset-y blur-radius color;`
- `offset-x`: Required. The horizontal offset of the shadow.
- `offset-y`: Required. The vertical offset of the shadow.
- `blur-radius`: Optional. The blur distance. Default is `0`.
- `color`: Optional. The color of the shadow. Default is the browser's default text color.
.basic-text-shadow { text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5); } .glow-text-shadow { text-shadow: 0 0 8px var(--accent-color); } .embossed-text { color: #ccc; text-shadow: 1px 1px 0 #fff, -1px -1px 0 #333; }
Shadowed Text
Glowing Text
Embossed Text