Back to CSS Notes
Topic #259

Input Focus and Icons

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

  1. Select the input element using a class or attribute selector.
  2. Add the :focus pseudo-class to target the active state.
  3. Define properties such as border-color, box-shadow, or outline.
  4. For icons, use background-image with 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: none without 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: none is set so clicks pass through to the input.
  • Inconsistent padding: Forgetting to adjust padding-left when adding an icon causes text to overlap the graphic.

When to use it

ApproachBest ForConsiderations
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.

Want to go beyond the notes?

Join Coding Now Tech Institute's CSS course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Input Focus and Icons – FAQs

Quick answers about learning Input Focus and Icons in CSS.

This free note from Coding Now Tech Institute explains Input Focus and Icons in CSS — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every CSS topic on Coding Now Tech Institute, including Input Focus and Icons, is 100% free with no signup required.
With focused practice, most students grasp Input Focus and Icons in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now