Definition and Usage
The :focus pseudo-class selector selects an element while it has focus.
- Focus
- Focus refers to the state of an element having focus. When a user navigates to a focusable element by pressing the Tab key or clicks it using a mouse or another pointing device, the element receives focus. When an element that supports keyboard input or other interactions receives focus, it enters a state in which it can be operated by the user.
By applying predefined styles only while an element has focus, you can provide users with an interactive and dynamic user experience.
Syntax
:focus {
/* ... */
}
Basic Example
The following is a simple example of using styles while an element has focus:
<form>
<fieldset>
<legend>Enter your name.</legend>
<label for="user-name">Name</label>
<input type="text" id="user-name" placeholder="Enter your name...">
</fieldset>
</form>
fieldset {
padding: 1em;
}
input:focus { /* Selects the element while it has focus. */
background-color: yellow;
}
<input> tag to enter a name, its background turns yellow.
Keep in mind!
The :focus pseudo-class selector applies only to the focused element itself.
If you need to select an element when any of its descendants has focus, use the :focus-within pseudo-class selector.
Accessibility Considerations
When applying focus styles, ensure they are sufficiently large and have clear contrast to make them easily visible. In particular, using high-contrast colors is recommended to accommodate users with color blindness or low vision.
WCAG 2.1 SC 1.4.11 Non-Text Contrast requires visual information necessary to identify focus states to have a contrast ratio of at least 3:1 against adjacent colors.
:focus {
outline: none;
}
Code Explanation
The outline property specifies the width, style, and color of the outline outside an element's border.
Also, as shown in the example above, the default outline provided by :focus visually indicates which element currently has focus. It is very important for visually distinguishing the focused element, so it should not be removed carelessly.
If you want to change the style of the default outline, you should use a replacement outline instead of removing it. The replacement outline must meet the requirements of WCAG 2.1 SC 1.4.11 Non-Text Contrast.
Styling :focus is essential for web accessibility. When applying :focus styles, developers should follow WCAG (Web Content Accessibility Guidelines) and strive to ensure that all users, including those with visual impairments, can use websites comfortably.
WCAG (Web Content Accessibility Guidelines)
WCAG (Web Content Accessibility Guidelines) provides technical guidelines for making web content accessible and usable by everyone. Its goal is to ensure that everyone can access and use web content equally, regardless of disability or technological limitations.
Browser Compatibility
| Selector |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
:focus
|
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
:focus
|
Selectors Level 4 #focus-pseudo |