Focus States — Complete Guide
Focus States — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of CSS Tutorial on Toolliyo Academy.
On this page
CSS Tutorial · Lesson 62 of 100
Focus States
Foundations & Layout ✓ → Responsive & Motion ✓ → Architecture & Perf → Projects
Architecture & Perf · 3 — Scale · ~15 min read · CSS — Accessibility & Performance
1. Introduction
Level up: Focus States. Think architecture, performance, and maintainable design systems.
Focus states show which element receives keyboard input — :focus, :focus-visible, and outline or box-shadow rings distinct from hover styles.
2. Real-world story
Trader navigates order form entirely by keyboard during market hours.
Outcome: focus-visible rings meet accessibility spec without hurting mouse UX.
3. Why it matters
StyleVerse power users navigate dashboards via keyboard; invisible focus breaks WCAG and frustrates traders.
4. Visual understanding
Read this diagram — the mental model for Focus States.
keyboard focus visible color contrast ≥ WCAG target prefers-reduced-motion respected do not rely on color alone
5. Key concepts (easy words)
| Idea | Meaning |
|---|---|
| What | Focus states show which element receives keyboard input — :focus, :focus-visible, and outline or box-shadow rings distinct from hover styles. |
| Remember | :focus-visible shows keyboard focus only. outline-offset separates ring from element edge. Never remove focus without custom visible substitute. |
| A11y tip | Test keyboard, contrast, and reduced motion. |
6. How it works
- Mouse click focus suppressed on links via focus-visible only pattern. Buttons hide outline for mouse but show ring for keyboard Tab navigation.
- :focus-visible shows keyboard focus only.
- outline-offset separates ring from element edge.
- Check the result in DevTools → Computed and the box model overlay.
7. Choose wisely
| Option | Notes |
|---|---|
| Do | Practice Focus States on a small StyleVerse card |
| Avoid | Copying huge frameworks before learning core CSS |
8. Try this example
Paste into a <style> block (or use Run Example). Refresh to see StyleVerse UI changes.
a:focus { outline: none; }
a:focus-visible {
outline: 2px solid #2874f0;
outline-offset: 3px;
border-radius: 4px;
}
.btn:focus:not(:focus-visible) { outline: none; }
Line walkthrough
| Code | What it means |
|---|---|
a:focus { outline: none; } | Selector — picks which elements get these declarations. |
a:focus-visible { | Selector — picks which elements get these declarations. |
outline: 2px solid #2874f0; | Declaration — property and value that change appearance. |
outline-offset: 3px; | Declaration — property and value that change appearance. |
border-radius: 4px; | Declaration — property and value that change appearance. |
} | Ends the rule block. |
.btn:focus:not(:focus-visible) { outline: none; } | Selector — picks which elements get these declarations. |
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
9. Another real-world angle
10. Best practices checklist
- Prefer classes over ids for reusable StyleVerse components.
- Use design tokens (CSS variables) for color and spacing.
- Mobile-first media queries; test keyboard focus.
- Keep specificity low — avoid !important except rare overrides.
- Animate transform/opacity when you can; respect prefers-reduced-motion.
11. Common mistakes
- outline: none on all elements globally.
- Focus ring same color as background — invisible.
12. Practice in the browser
- Tab through nav links — ring should appear.
- Click link with mouse — ring should not stick obtrusively.
- Match focus ring color to StyleVerse primary token.
- Test focus within modals and dropdown traps.
Experiments
- Use box-shadow: 0 0 0 3px rgba(40,116,240,0.4) instead of outline.
- Add :focus-within ring on form groups with invalid fields.
13. FAQ
Where should I put CSS for Focus States?
Start in a <style> block or styles.css linked from HTML. Later use CSS Modules, Tailwind, or tokens in a design system.
Why is my rule not applying?
Check selector match, typos, specificity, and whether a more specific rule or inline style wins. DevTools Computed tells the truth.
Flexbox or Grid for this?
Flex for one direction (toolbars, card rows). Grid for two-dimensional page frames. Many UIs use both.
14. Interview questions
Explain Focus States simply.
Focus states show which element receives keyboard input — :focus, :focus-visible, and outline or box-shadow rings distinct from hover styles.
How do you debug CSS?
Inspect element, toggle declarations, read Computed styles, outline the box model, and reduce specificity conflicts.
How does this show up in production UI?
focus-visible rings meet accessibility spec without hurting mouse UX.
15. Remember
- :focus-visible shows keyboard focus only.
- outline-offset separates ring from element edge.
- Never remove focus without custom visible substitute.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!