One more touch on that rounded card. Just add a little shadow and it looks like it lifts gently off the page. From a look printed flat on paper to a texture you could almost pick up — box-shadow is what makes that happen.
Let’s write one
.card {
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
}The four things lined up after box-shadow each mean this:
| Value | Role |
|---|---|
0 | The shadow’s horizontal offset (0 for straight down) |
2px | The shadow’s vertical offset (2 pixels down) |
10px | The width of the shadow’s blur (bigger means softer) |
rgba(0, 0, 0, 0.06) | The shadow’s color (a faint black) |
“A faint black shadow, offset 2 pixels straight down, blurred by 10 pixels” — and the card looks like it lifts up gently.
Line up with-shadow and without-shadow and the difference in how it floats is easy to see.
Making a “color with transparency” using rgba()
A common choice for shadow color is rgba(). It’s a way of writing that adds one more thing — transparency — to the color names and codes you’ve used so far.
color: rgba(0, 0, 0, 0.06);In the order rgba(red, green, blue, transparency), the first three are the number version of a color code, and the last, 0.06, is the transparency. 0 is fully transparent, 1 is fully opaque. A faint black at around 0.05–0.15 makes a shadow look soft rather than too harsh.
Layering shadows and denting them inward
box-shadow, separated by commas, can apply several shadows at once.
.card {
box-shadow:
0 1px 2px rgba(0, 0, 0, 0.08),
0 8px 24px rgba(0, 0, 0, 0.06);
}A small, crisp one up close and a wide, soft one further out — layering two gives more natural depth than just one.
Put inset at the start of the value and the shadow forms on the inside.
.input {
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
}Opposite to a shadow that floats up, it gives a slightly dented look. It’s a way of writing often used to represent input fields and sunken buttons.
inset added, looks dented inward rather than floating out.Summary of this lesson
- A shadow is
box-shadow. Four things: horizontal offset, vertical offset, blur, and color - Use
rgba()for the color to make a color with transparency - Keep shadows faint — low
rgbatransparency looks elegant - Layer several shadows with commas.
insetmakes an inner shadow too
