The :focus pseudo-class styles elements that currently have focus. It is essential for keyboard navigation, form usability, and accessible web design.
01
Has focus
Active element.
02
Keyboard
Tab navigation.
03
Forms
input, button.
04
Outline
Visible ring.
05
:focus-visible
Smart ring.
06
A11y
Keep indicators.
Fundamentals
Introduction
The :focus selector in CSS is a pseudo-class used to style an element that is currently focused by the user.
When an input, button, link, or other focusable element receives focus through keyboard interaction (Tab key) or a mouse click, :focus styles help users see which element is active.
Definition and Usage
Append :focus to a selector with no space before the colon: input:focus, button:focus, or a:focus. Styles apply while the element retains focus and disappear when focus moves elsewhere.
💡
Beginner Tip
Never use outline: none on :focus without adding a visible alternative like a border or box-shadow. Keyboard users depend on focus indicators to navigate your site.
Focus applies while the element is the active focus target.
Combine with type selectors: input[type="email"]:focus.
Chain with other states: button:focus:hover.
Replace default outline with an equally visible custom indicator.
Consider :focus-visible for keyboard-only focus rings.
Related Selectors
:focus-visible — shows focus ring when browser deems it appropriate (often keyboard)
:focus-within — matches a parent when any descendant has focus
:hover — matches on mouse-over
:active — matches while being pressed
:disabled — disabled elements cannot receive focus
Cheat Sheet
⚡ Quick Reference
Question
Answer
Selector type
Pseudo-class
When it applies
Element currently has focus
Triggered by
Tab key, click, or programmatic .focus()
Common properties
outline, border, box-shadow
Accessibility rule
Always provide a visible focus indicator
Browser support
All modern browsers
Context
When to Use :focus
:focus is critical for interactive, accessible interfaces:
Form fields — Highlight the active input so users know where they are typing.
Buttons — Show a ring or shadow when a button receives keyboard focus.
Navigation links — Style focused menu items in header and sidebar nav.
Custom components — Style focus on elements made focusable with tabindex.
Combined states — Pair with :hover and :active for complete interaction feedback.
Preview
👀 Live Preview
Tab into the fields below to see :focus styling on inputs and the button:
Hands-On
Examples Gallery
Practice :focus with form inputs, buttons, links, and the modern :focus-visible pattern.
📜 Core Patterns
Style interactive elements when they receive focus.
Example 1 — Focused form inputs
Change border and background when an input field is focused.
focus-form.html
<style>input:focus{outline:none;border:2px solid #2563eb;background-color:#eff6ff;}</style><form><labelfor="email">Email:</label><inputtype="email"id="email"placeholder="Enter your email"><labelfor="password">Password:</label><inputtype="password"id="password"placeholder="Enter your password"><buttontype="submit">Login</button></form>
box-shadow creates a visible focus ring without relying on the browser default outline. The semi-transparent blue glow meets accessibility contrast needs.
📄 Links & Modern Focus
Extend focus styling to navigation and keyboard-only indicators.
Example 3 — Focused link styling
Style navigation links when they receive keyboard focus.
outline-offset adds space between the link text and the focus ring, making it easier to see on tight navigation layouts.
Example 4 — :focus-visible for keyboard users
Show a focus ring only when the browser determines it is needed — typically for keyboard navigation.
focus-visible.css
/* Remove default outline globally */:focus{outline:none;}/* Show ring only when appropriate (keyboard) */:focus-visible{outline:2px solid #2563eb;outline-offset:2px;}
Mouse clicks may skip the ring; keyboard Tab shows :focus-visible.
How It Works
This modern pattern removes rings on mouse click while preserving them for keyboard users — a best practice for accessible, polished UIs.
Watch Out
⚠️ Common Pitfalls
Removing outline entirely — *:focus { outline: none; } without a replacement harms keyboard accessibility.
Low-contrast focus rings — Pale gray outlines on white backgrounds are hard to see.
Focus traps — Ensure users can Tab out of modals and custom widgets.
Missing focus on custom controls — Div-based buttons need tabindex="0" and keyboard handlers.
Confusing :focus with :hover — Focus persists after click; hover does not.
A11y
♿ Accessibility
WCAG requirement — Focusable elements must have a visible focus indicator (Success Criterion 2.4.7).
Keyboard testing — Tab through your entire page and verify every interactive element shows focus.
Use :focus-visible — Prefer this for keyboard-only rings when mouse clicks should not show outlines.
Logical tab order — Focus order should follow visual reading order; use tabindex carefully.
Do not disable focus — Avoid tabindex="-1" on elements users need to reach via keyboard.
🧠 How :focus Works
1
User navigates to element
Tab key, click, or JavaScript .focus() moves focus to an element.
Input
2
Element matches :focus
The browser marks the element as focused and applies :focus styles.
Match
3
Focus indicator renders
Outline, border, or shadow shows which element is active.
Render
=
⌨
Accessible navigation
Users always know where they are in the interface.
Compatibility
🖥 Browser Compatibility
The :focus pseudo-class is supported in all modern browsers. :focus-visible is supported in current browser versions.
✓ Baseline · Universal support
Focus styling everywhere
:focus works reliably in Chrome, Firefox, Safari, Edge, and Opera.
99%Universal support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll modern versions
Full support
:focus pseudo-class99% supported
Bottom line::focus is essential and safe everywhere. Pair with :focus-visible for modern keyboard-friendly focus rings.
Wrap Up
🎉 Conclusion
The :focus pseudo-class is a key part of accessible, user-friendly web design. It ensures users can see which element is active when navigating with a keyboard or other input devices.
Always provide a visible focus indicator, combine with :hover and :active for complete feedback, and consider :focus-visible for polished keyboard-only rings.
Provide a visible focus ring on all interactive elements
Use box-shadow or outline with good contrast
Test keyboard navigation with the Tab key
Use :focus-visible for keyboard-only rings
Combine with :hover and :active
❌ Don’t
Remove outline without a replacement
Use low-contrast focus colors
Forget focus styles on links and buttons
Create focus traps in modals
Confuse :focus with :hover behavior
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :focus
Use these points when styling focused elements.
5
Core concepts
◉01
Active element
Has focus now.
Purpose
⌨02
Keyboard
Tab to navigate.
Input
◯03
Visible ring
Always show it.
A11y
:visible04
focus-visible
Keyboard ring.
Modern
🌐05
99% support
All browsers.
Compat
❓ Frequently Asked Questions
The :focus pseudo-class matches an element that currently has keyboard or programmatic focus — such as an input field being tabbed to or a button clicked into.
Interactive elements including input, textarea, button, select, a[href], and any element with tabindex="0" or higher. Non-interactive elements like div do not receive focus unless tabindex is added.
Avoid removing outline without a visible replacement. Keyboard users rely on focus indicators. Use :focus or :focus-visible with a clear outline, border, or box-shadow instead of outline: none alone.
:focus matches whenever an element is focused, including mouse clicks. :focus-visible matches when the browser decides a focus ring should be shown — typically for keyboard navigation.
Yes. All modern browsers support :focus on interactive elements. :focus-visible is also supported in current browsers for smarter keyboard-only focus rings.