CSS3 Shadows
CSS shadows add depth by drawing a soft or sharp shade behind an element or its text. There are two properties: box-shadow for the element's box, used on cards, buttons and menus, and text-shadow for the letters themselves. Used lightly, shadows show which items sit above others and which can be clicked.
Reading the box-shadow values
A box shadow takes up to six parts in this order:
- inset (optional): draws the shadow inside the box instead of outside.
- offset-x: horizontal shift; positive moves right.
- offset-y: vertical shift; positive moves down.
- blur radius (optional): how soft the edge is; 0 is sharp.
- spread radius (optional): grows or shrinks the shadow before blurring.
- colour: usually a dark colour with transparency.
.card {
border-radius: 12px;
background: #fff;
/* two layers: a tight contact shadow and a soft ambient one */
box-shadow:
0 1px 2px rgba(15, 23, 42, 0.12),
0 8px 24px rgba(15, 23, 42, 0.10);
transition: box-shadow 0.2s ease, transform 0.2s ease;
}
.card:hover {
transform: translateY(-2px);
box-shadow:
0 2px 4px rgba(15, 23, 42, 0.14),
0 16px 32px rgba(15, 23, 42, 0.14);
}
.input:focus {
outline: none;
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.45); /* focus ring */
}
.well {
box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.15);
}
h2.glow {
text-shadow: 0 2px 8px rgba(37, 99, 235, 0.5);
}
Why layered shadows look more natural
Real objects cast a small, darker shadow right where they touch a surface and a larger, lighter one further away. A single heavy shadow often looks muddy. Separating multiple shadows with commas lets you copy that effect: one tight layer with little blur, and one wide layer with low opacity. The first shadow in the list is drawn on top.
Useful tricks with spread and inset
- Focus rings. Zero offsets, zero blur and a spread of a few pixels make a solid ring that follows rounded corners, as the
.input:focusrule shows. If you replace the outline like this, make sure the ring is clearly visible. - Pressed buttons. An
insetshadow makes an element look pushed into the page, which works for an active button state or a sunken input area. - One-sided shadows. A negative spread with a vertical offset, such as
0 6px 6px -4px, shows the shadow only under the bottom edge, handy for sticky headers.
text-shadow in practice
text-shadow takes offset-x, offset-y, blur and colour, with no spread and no inset. A soft dark shadow can help white text stay readable on a busy photo. Avoid strong shadows on body text; they make small letters blurry and harder to read.
Mistakes that make shadows look cheap
The most common is using pure black at full strength, like 5px 5px 10px #000. It looks heavy and dated. Use a transparent dark colour, often a tint of your text or brand colour, and keep opacity low. A second mistake is inconsistent light direction: if one card's shadow falls down and another's falls to the left, the page feels off. Pick one direction, usually straight down, and use it everywhere.
Also note that box-shadow never changes layout. It does not take up space, so a large shadow can be cut off by a parent with overflow: hidden. Add padding to the parent if the shadow gets clipped. Animating shadows on many elements at once can also be slow; animating transform and a pseudo-element's opacity is a lighter alternative.
Shadow checklist
Use box-shadow for boxes and text-shadow for letters, layer a tight and a soft shadow for realism, keep colours transparent, choose one light direction for the whole design, and watch for parents that clip the shadow.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.