Box Shadow Basics
You'll learn how to add realistic depth and elevation to elements using the box-shadow property.
What Is a Box Shadow?
A box shadow creates a shadow effect around an element's frame, making it appear to float above the page or sit in a recessed area. Think of it like holding a piece of paper under a lamp — the paper casts a shadow on the desk below, giving it dimension.
The Box Shadow Syntax
The box-shadow property takes several values in a specific order:
box-shadow: offset-x offset-y blur-radius spread-radius color;
Breaking Down the Values
Offset-x and Offset-y: These first two values position the shadow horizontally and vertically. Positive values push the shadow right and down; negative values push it left and up.
Blur-radius (optional): Controls how soft or sharp the shadow appears. 0 creates a hard edge; larger values create a softer, more diffused shadow.
Spread-radius (optional): Expands or contracts the shadow's size before blurring. Positive values make it larger; negative values make it smaller.
Color (optional): The shadow's color. Defaults to black, but you can use any color value including transparent colors with alpha channels.
Example
.card {
width: 200px;
height: 150px;
background-color: white;
box-shadow: 4px 4px 8px 0px rgba(0, 0, 0, 0.2);
}
This creates a subtle shadow 4px to the right, 4px down, with an 8px blur and no spread, using semi-transparent black.
Key Takeaway: Box shadows add depth using horizontal/vertical offsets, blur amount, spread distance, and color — creating visual hierarchy without additional markup.