Custom Focus Styles with Box Shadow
You'll learn how to create flexible, accessible focus indicators using box-shadow instead of outline.
The Core Idea
While the outline property is the traditional way to style focus indicators, it has limitations—it can't have rounded corners, can't create multi-layered effects, and doesn't respect border-radius. The box-shadow property gives you much more design flexibility while maintaining accessibility.
box-shadow lets you create focus rings that match your design system perfectly. You can make them glow, stack multiple shadows, round the corners, and even create effects that look more integrated with your interface.
Basic Syntax
.button:focus {
outline: none; /* Only remove if replacing with visible alternative */
box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.5);
}
The key pattern is 0 0 0 [width] — no horizontal offset, no vertical offset, no blur, just a solid ring at your chosen width.
Why This Matters
Unlike outline, box-shadow:
- Respects
border-radiusfor rounded elements - Allows multiple layered shadows for richer effects
- Can be animated smoothly with transitions
- Works well with modern design systems
.card:focus {
outline: none;
box-shadow:
0 0 0 3px white,
0 0 0 5px #3b82f6;
border-radius: 8px; /* Shadow follows the curve */
}
This creates a double-ring effect with an inner white ring and outer blue ring, both following the rounded corners.
Key Takeaway: Use box-shadow with the 0 0 0 [width] pattern to create flexible, accessible focus indicators that integrate beautifully with modern designs—but never remove outline without replacing it with an equally visible alternative.