CSS Portal

:focus CSS Pseudo Class

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

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

<div class="container">
<label for="username">Username:</label>
<input type="text" id="username" placeholder="Enter your name...">

<button type="submit">Login</button>
</div>
/* Standard styling */
input {
padding: 10px;
border: 2px solid #ccc;
border-radius: 5px;
outline: none; /* Removes the default browser ring */
transition: all 0.3s ease; /* Smooths the transition */
}

/* The :focus state */
input:focus {
border-color: #3498db;
box-shadow: 0 0 8px rgba(52, 152, 219, 0.5);
background-color: #f0f8ff;
}

/* Button focus state for keyboard users */
button:focus {
background-color: #2ecc71;
color: white;
outline: 3px solid #27ae60;
outline-offset: 2px;
}

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
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 31st December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!