Shadows & Filters — CSS

box-shadow: 2px 4px 8px rgba(0, 0, 0, 0.2) gives an element's box a shadow: it moves 2px right and 4px down, its edge is blurred by 8px, and the last value is…

Cards in a shopping app usually have a faint shadow under them. The shadow makes the card look slightly raised above the page, so your eye knows right away which part is on top. CSS has three properties for shadows and similar effects:

A box shadow with box-shadow

This rule gives a card a shadow:

.card {
  box-shadow: 2px 4px 8px rgba(0, 0, 0, 0.2);
}

Read the values from left to right, separated by spaces:

The result: a faint grey shadow appears a little to the bottom right of the card, with a soft edge.

Before the colour, there can be a fourth number called the spread: 2px 4px 8px 2px makes the shadow 2px bigger on every side.

The word inset at the start of the value moves the shadow to the inside of the element, for example box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2). The element then looks pressed into the page. This effect suits an input box. Shadows, outside or inside, always follow corners rounded with border-radius.

A letter shadow with text-shadow

This rule gives the letters of a heading a shadow:

h1 {
  text-shadow: 2px 2px 4px gray;
}

The values come in the same order as in box-shadow: offset-x, offset-y, blur, then colour. The difference: a text-shadow follows the shape of each letter, not the element's box. The result: every letter of the heading has a faint grey shadow to its bottom right.

Visual effects with filter

This rule blurs every image:

img {
  filter: blur(3px);
}

filter changes how an element looks without changing the original file. Its value is a function. A few common ones:

Several filters can be combined, separated by spaces: filter: grayscale(100%) blur(2px);.

⚠️ Where shadows and filters go wrong

TL;DR: box-shadow: 2px 4px 8px rgba(0, 0, 0, 0.2) gives an element's box a shadow: it moves 2px right and 4px down, its edge is blurred by 8px, and the last value is its colour. A fourth number (the spread, extra size for the shadow) can go before the colour, and the word inset moves the shadow to the inside of the element. text-shadow uses the same order for a shadow on letters. filter adds effects like blur (blur()), black and white (grayscale()), more brightness (brightness()) and a shadow that follows an image's shape (drop-shadow()).

⚡ The four parts of a shadow

Write:

.card {
  box-shadow: 2px 4px 8px rgba(0, 0, 0, 0.2);
}

Meaning: box-shadow gives an element's box a shadow. Read the values from left to right: the shadow moves 2px to the right, moves 4px down, its edge is blurred by 8px, then comes the colour. rgba(0, 0, 0, 0.2) is black that is only 20% visible, so the shadow is faint. The values are separated by spaces, not commas.

⚡ A shadow on the inside

Guess first: if you put the word inset at the start of a box-shadow value, where does the shadow go?

To the inside of the element. The element then looks pressed into the page. This effect suits an input box.

Meaning: The word inset moves the shadow to the inside of the element, for example box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2). Shadows, outside or inside, always follow corners rounded with border-radius.

⚡ Effects like a photo filter

Write:

img {
  filter: blur(3px);
}

Meaning: filter changes how an element looks, like a photo filter on your phone, without changing the original file. Here blur(3px) makes every image blurry. Other values: grayscale(100%) makes an element black and white, and drop-shadow() makes a shadow that follows the shape of an image, not its box.

What you will learn