:focus CSS Pseudo Class
Description
The :focus pseudo-class in CSS is used to select and style elements that currently have input focus. Focus is typically applied to interactive elements such as input, textarea, button, or links when they are active or navigated to via keyboard (commonly using the Tab key). This pseudo-class allows designers to provide visual feedback indicating that the element is ready to receive user input, enhancing accessibility and usability for keyboard navigation.
When an element is in focus, it can be styled with properties like border, background-color, outline, or box-shadow to make it stand out from other elements. For example, a common pattern is to add a colored outline or glow effect to input fields when they are focused, helping users identify which field is active in a form.
Focus can also be programmatically set using JavaScript with the element.focus() method. This is often used in form interactions, modals, and custom widgets to guide user interaction.
Here’s a simple example demonstrating the :focus pseudo-class:
<input type="text" placeholder="Enter your name">
input:focus {
outline: 2px solid #007BFF;
background-color: #E0F0FF;
}
In this example, when the input field receives focus, a blue outline appears and the background color changes, visually signaling to the user that the field is active.
The :focus pseudo-class is also often combined with :hover to ensure consistent visual cues for both mouse and keyboard users, improving overall accessibility.
This pseudo-class is crucial in web accessibility standards, as it helps users who rely on keyboard navigation or assistive technologies to understand the current interactive context of a page.
Syntax
:focus {
/* ... */
}
Example
Browser Support
The following information will show you the current browser support for the CSS :focus pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS psuedo class.
This psuedo class is supported by all modern browsers.
Desktop
Tablets & Mobile
Last updated by CSSPortal on: 31st December 2025
