
CSS Topics
- CSS Intro
- CSS How To
- CSS Editors
- CSS Properties
- CSS Selectors
- .class
- .class1.class2
- .class1 .class2
- #id
- * (all)
- element
- element.class
- element,element
- element element
- element>element
- element+element
- element1~element2
- [attribute]
- [attribute=value]
- [attribute~=value]
- [attribute|=value]
- [attribute^=value]
- [attribute$=value]
- [attribute*=value]
- :active
- ::after
- ::before
- :checked
- :default
- :disabled
- :empty
- :enabled
- :first-child
- ::first-letter
- ::first-line
- :first-of-type
- :focus
- :fullscreen
- :has()
- :hover
- :in-range
- :indeterminate
- :invalid
- :lang()
- :last-child
- :last-of-type
- :link
- ::marker
- :not()
- :nth-child()
- :nth-last-child()
- :nth-last-of-type()
- :nth-of-type()
- :only-of-type
- :only-child
- :optional
- :out-of-range
- ::placeholder
- :read-only
- :read-write
- :required
- :root
- ::selection
- :target
- :valid
- :visited
- CSS Comments
- CSS Length
- CSS Image Sprites
- CSS Grid Layout
- CSS Grid Flexbox
- CSS @charset Rule
- CSS @font-face Rule
- CSS @import Rule
- CSS @keyframes Rule
- CSS @media Rule
CSS :focus Selector
Photo Credit to CodeToFun
π Introduction
The :focus selector in CSS is used to style an element that is currently focused by the user.
When an element, such as an input field or a button, receives focus (typically through keyboard interaction or mouse clicks), this pseudo-class allows you to apply specific styles to enhance usability and accessibility.
π‘ Syntax
The signature of the :focus Selector is as follows:
:focus {
/* CSS properties */
}This selector is mainly applied to interactive elements like <input>, <textarea>, <button>, and links (<a>), among others.
π Example
Letβs see how the :focus selector can be used to style focused elements:
β οΈ HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS :focus Selector Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<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>
</body>
</html>π¨ CSS
/* Style for focused input fields */
input:focus {
outline: none;
border: 2px solid blue;
background-color: #e0f7ff;
}
/* Style for focused button */
button:focus {
outline: none;
box-shadow: 0 0 5px blue;
}In this example:
- When the user focuses on the input fields, the border changes to blue, and the background color lightens.
- The button, when focused, gets a shadow around it for better visibility.
π¬ Usage Tips
- Always use the
:focusselector in combination with other states like:hoverand:activeto provide a comprehensive user experience across devices (especially for keyboard navigation). - You can customize the focus outline to better match your design, but be cautious about entirely removing the focus indicator, as it can hinder accessibility for users who rely on keyboard navigation.
β οΈ Common Pitfalls
- Overriding default focus styles: While customizing the focus style, avoid removing the
outlineproperty without providing an alternative, as this can make navigation difficult for keyboard users. - Focus traps: Ensure that all interactive elements on the page are accessible via the keyboard, and users should be able to exit focusable elements easily. Avoid creating focus traps where the user canβt navigate away from an element.
π Conclusion
The :focus selector is a key part of making web pages accessible and user-friendly.
It ensures that users can interact with elements visually when navigating via keyboard or other input devices. Proper usage of :focus not only enhances the user experience but also improves web accessibility.
π¨βπ» Join our Community:
Author

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee












If you have any doubts regarding this article (CSS :focus Selector), please comment here. I will help you immediately.