CSS :focus Selector

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Form States

What You’ll Learn

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.

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.

📝 Syntax

The syntax for the :focus pseudo-class is:

syntax.css
element:focus {
  /* CSS properties */
}

Basic Example

focus-input.css
input:focus {
  outline: none;
  border: 2px solid #2563eb;
  background-color: #eff6ff;
}

button:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.45);
}
input:focus button:focus a:focus textarea:focus

Focusable Elements

ElementReceives focus?
<input>, <textarea>, <select>Yes (when not disabled)
<button>Yes (when not disabled)
<a href="...">Yes
<div>, <p>Only with tabindex

Syntax Rules

  • 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

⚡ Quick Reference

QuestionAnswer
Selector typePseudo-class
When it appliesElement currently has focus
Triggered byTab key, click, or programmatic .focus()
Common propertiesoutline, border, box-shadow
Accessibility ruleAlways provide a visible focus indicator
Browser supportAll modern browsers

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.

👀 Live Preview

Tab into the fields below to see :focus styling on inputs and the button:

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>
  <label for="email">Email:</label>
  <input type="email" id="email" placeholder="Enter your email">
  <label for="password">Password:</label>
  <input type="password" id="password" placeholder="Enter your password">
  <button type="submit">Login</button>
</form>
Try It Yourself

How It Works

When a user tabs into or clicks an input, input:focus applies the blue border and light background until focus moves away.

Example 2 — Button focus with box-shadow

Add a visible glow ring around a focused button using box-shadow.

focus-button.css
button {
  background: #2563eb;
  color: #fff;
  border: none;
  padding: 0.6rem 1.2rem;
  border-radius: 0.5rem;
  cursor: pointer;
}

button:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.45);
}
Try It Yourself

How It Works

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 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;
}
Try It Yourself

How It Works

This modern pattern removes rings on mouse click while preserving them for keyboard users — a best practice for accessible, polished UIs.

⚠️ 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.

♿ 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.

🖥 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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All modern versions
Full support
:focus pseudo-class 99% supported

Bottom line: :focus is essential and safe everywhere. Pair with :focus-visible for modern keyboard-friendly focus rings.

🎉 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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about :focus

Use these points when styling focused elements.

5
Core concepts
02

Keyboard

Tab to navigate.

Input
03

Visible ring

Always show it.

A11y
:visible 04

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.

Practice in the Live Editor

Open the HTML editor and experiment with :focus, form styling, and keyboard navigation.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful