Shadows and visual depth
Shadows can separate a card from its background or add emphasis to a control. Subtle, consistent depth is usually more useful than many competing effects.
✓ What you will learn in this topic
- Explain the key idea: Use restrained shadows to support hierarchy and preserve non-shadow cues.
- Apply the concept: A soft shadow gives the demo a small amount of separation from the page without replacing its visible edge.
- Avoid this common mistake:
Concept explanation
box-shadow accepts horizontal and vertical offsets, blur, spread, and color. Multiple shadows can be separated by commas. A shadow should reinforce hierarchy rather than carry the only indication that an element is interactive.
Check that shadow edges are visible on the actual surface and do not create a false focus state. For high-contrast modes, preserve borders or other cues that remain visible when shadows are reduced.
Syntax
A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.
.demo {
box-shadow: 0 0.5rem 1rem rgb(15 23 42 / 12%);
}Example 1: a small page element
Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.
<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>.demo {
box-shadow: 0 0.5rem 1rem rgb(15 23 42 / 12%);
}Expected result: A soft shadow gives the demo a small amount of separation from the page without replacing its visible edge.
Example 2: a practical interface card
The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.
<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>.card {
border: 1px solid #dbe3ee;
border-radius: 1rem;
box-shadow: 0 0.75rem 2rem rgb(15 23 42 / 10%);
}Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.
Common mistake
Removing a browser focus outline without providing a clear replacement makes keyboard navigation hard to follow.
Best practice
Keep a visible focus indicator, use shadows sparingly, and do not use an outline as a substitute for a layout border.
Practice exercise
Style a card with a subtle border and shadow, then style its keyboard focus state with a high-contrast outline. Apply it to the example above and inspect the result at a narrow and a wide viewport.
Section summary
- Use restrained shadows to support hierarchy and preserve non-shadow cues.
- Keep a visible focus indicator, use shadows sparingly, and do not use an outline as a substitute for a layout border.
Knowledge check
What is a sensible role for a component shadow?
Reveal the explanation
Shadows are decorative hierarchy cues; retain borders, labels, and visible focus states where needed.
Finish the module lessons, then take the server-scored module quiz to check your understanding.
Practical example
css.demo {
box-shadow: 0 0.5rem 1rem rgb(15 23 42 / 12%);
}
Expected result:
A soft shadow gives the demo a small amount of separation from the page without replacing its visible edge.
Hands-on practice task
Style a card with a subtle border and shadow, then style its keyboard focus state with a high-contrast outline.
In this section
Other lessons in Borders, Outlines, and Shadows
Broaden your knowledge
Related learning topics
HTML
Learn to structure accessible, meaningful web pages with a complete HTML course from first document to professional application patterns.
JavaScript
Build strong JavaScript foundations with browser examples, focused practice, beginner projects, and module quizzes.