Learn how to visually indicate which input field is active using CSS focus states and integrate icons for better user experience.
What it is
The :focus pseudo-class applies styles to an element when it receives keyboard or mouse focus. In forms, this highlights the currently active input field, guiding the user’s attention. Combining this with icon placement (often via background images or adjacent elements) creates intuitive form fields where users know exactly what they are typing into.
Related terms include :active, :hover, and accessibility standards like WCAG contrast ratios.
Why it matters
- Usability: Users instantly see which field they are editing, reducing errors in long forms.
- Accessibility: Clear focus indicators help keyboard-only navigators track their position on the page.
- Aesthetics: Custom styling replaces default browser outlines, allowing brand-consistent design.
- Clarity: Icons inside or next to inputs provide visual cues about expected data types (e.g., email, password).
Syntax or steps
- Select the input element using a class or attribute selector.
- Add the
:focuspseudo-class to target the active state. - Define properties such as
border-color,box-shadow, oroutline. - For icons, use
background-imagewith positioning or place an SVG/icon font element absolutely positioned relative to the input wrapper.
Example
<div class="input-group">
<span class="icon">🔒</span>
<input type="password" placeholder="Enter password" />
</div>
<style>
.input-group {
position: relative;
display: inline-block;
}
.input-group .icon {
position: absolute;
left: 10px;
top: 50%;
transform: translateY(-50%);
pointer-events: none; /* Allows clicking through the icon */
}
.input-group input {
padding-left: 35px; /* Space for the icon */
border: 1px solid #ccc;
border-radius: 4px;
outline: none; /* Remove default browser outline */
}
.input-group input:focus {
border-color: #185FA5;
box-shadow: 0 0 0 3px rgba(24, 95, 165, 0.2);
}
</style>
This example wraps the input and icon in a container. The icon is absolutely positioned inside the input area. When the input gains focus, the border changes color and a subtle glow appears, clearly indicating activity.
Common mistakes
- Removing outlines without replacement: Setting
outline: nonewithout adding a visible alternative breaks accessibility. Always add a custom border or shadow. - Poor contrast: Focus colors must meet WCAG contrast requirements against the background. Light blue on white may be invisible to some users.
- Icon blocking interaction: If the icon overlaps the text area, ensure
pointer-events: noneis set so clicks pass through to the input. - Inconsistent padding: Forgetting to adjust
padding-leftwhen adding an icon causes text to overlap the graphic.
When to use it
| Approach | Best For | Considerations |
|---|---|---|
CSS :focus + Background Icon |
Simple, single-line inputs with static icons. | Easy to maintain; limited dynamic control over icon state. |
| JavaScript Toggle Class | Complex interactions (e.g., showing/hiding passwords). | More flexible but adds JS dependency and complexity. |
Use pure CSS for standard highlighting. Use JavaScript only if the icon itself needs to change based on input content or state beyond simple focus.
Practice
Guided Exercise: Create an email input field with a mail icon. Style the focus state to change the border to green (#2E7D32) and add a light green shadow.
Challenge: Modify the example so that the icon color also changes when the input is focused. Hint: You cannot style a sibling directly from the input's focus state in older CSS, but you can use .input-group:focus-within .icon to target the parent group when any child has focus.
Quick check
Question: Why is outline: none alone dangerous for accessibility?
Answer: It removes the default visual indicator of focus, making it impossible for keyboard users to see which element is active unless a custom, high-contrast style is provided.
Summary
Effective form design relies on clear visual feedback. By combining :focus styles with properly positioned icons, you create accessible, user-friendly interfaces. Always prioritize visibility and contrast over decorative minimalism.