Question
When a text input receives focus—for example, after being clicked or reached with the Tab key—Chrome and Safari display a blue highlight or outline around it. How can I remove or customize this focus indicator with CSS?
<input
type="text"
name="user"
class="middle"
id="user"
tabindex="1"
/>
I would like the input to fit the layout without the default browser focus styling, while understanding how this behaves across browsers such as Safari, Chrome, Firefox, and Internet Explorer.
Short Answer
You will learn the difference between an input's border and its keyboard-focus outline, how to style :focus and :focus-visible, and why a visible focus indicator is important for accessibility.
Concept
Browsers apply default styles to interactive elements when they receive focus. Focus means the element is ready to receive keyboard input or interaction. A text input becomes focused when a user clicks it, tabs to it, or selects it through a script.
The blue ring seen in many browsers is often an outline, not a border. This is why changing the normal border property may not remove it.
CSS provides pseudo-classes for focus state:
:focusmatches any focused element.:focus-visiblematches focus when the browser decides a visible indicator is helpful, usually during keyboard navigation.
It is technically possible to remove the default indicator with outline: none, but doing so without an alternative makes a page difficult to use with a keyboard. Users need to see where the keyboard focus currently is.
A better approach is to replace the browser default with a focus style that matches the design while remaining clear and high-contrast.
Mental Model
Think of focus as a cursor or pointer that moves between controls on a form. When someone presses Tab, the browser moves that pointer from one interactive element to the next.
The focus outline is like a brightly colored frame around the item currently selected by that pointer. You may change the frame to match your design, but removing it completely is like hiding the pointer: keyboard users can no longer tell which control will receive their next keystroke.
Syntax and Examples
Use :focus to style an element whenever it receives focus.
input:focus {
outline: 2px solid #2563eb;
outline-offset: 2px;
}
outlinedraws a line outside the element's box.outline-offsetcreates space between the element and the outline.- Unlike a border, an outline usually does not change the element's layout size.
For most modern interfaces, use :focus-visible to show a strong indicator primarily for keyboard users:
input:focus {
border-color: #94a3b8;
}
input:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 2px;
}
If a design system requires no browser outline, replace it with another obvious focus signal rather than simply hiding it:
.middle:focus {
outline: none;
border-color: #2563eb;
box-shadow: 0 0 0 3px rgb(37 99 235 / 30%);
}
This removes the default outline but provides a blue border and outer glow so the focused input remains visible.
Step by Step Execution
Consider this page:
<label for="user">Username</label>
<input id="user" class="middle" type="text" name="user">
.middle {
border: 1px solid #9ca3af;
border-radius: 4px;
padding: 8px;
}
.middle:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 2px;
}
When a user presses Tab:
- The browser moves focus to the input.
- The input now matches the
.middle:focus-visibleselector. - The browser draws a
3pxblue outline outside the input. outline-offset: 2pxleaves a small gap so the ring is easy to see.- When focus moves elsewhere, the selector no longer matches and the focus ring disappears.
When the input is clicked, modern browsers may decide not to show :focus-visible. The input is still focused and can receive typing; the browser is simply avoiding an unnecessary keyboard-style indicator for pointer interaction.
Real World Use Cases
- Login forms: Make the active email or password field clear when users tab through the form.
- Checkout forms: Help keyboard users identify the current address, payment, or coupon field.
- Search boxes: Apply a branded but visible ring when the search input is ready for typing.
- Admin dashboards: Use a consistent focus token across inputs, buttons, links, and custom controls.
- Error correction: Combine focus styling with error styling so users can find and fix an invalid field.
- Design systems: Define one reusable focus-ring style that works consistently across components and browsers.
Real Codebase Usage
In real projects, developers usually avoid one-off rules such as input:focus { outline: none; } scattered across files. Instead, they define a reusable focus treatment.
:root {
--focus-color: #2563eb;
--focus-ring: 0 0 0 3px rgb(37 99 235 / 35%);
}
:where(input, textarea, select, button, a):focus-visible {
outline: 2px solid var(--focus-color);
outline-offset: 2px;
}
The :where(...) selector applies the same keyboard-focus rule to common interactive controls. It has low CSS specificity, making component-level overrides easier.
For custom components, developers must ensure the actual focusable element receives the style:
.search-field input:focus-visible {
outline: none;
}
.search-field:focus-within {
box-shadow: 0 0 0 3px rgb(37 99 235 / 35%);
border-color: #2563eb;
}
:focus-within is useful when a wrapper should highlight whenever one of its children, such as an input, has focus.
Common Mistakes
Removing focus without a replacement
/* Avoid this on its own */
input:focus {
outline: none;
}
This hides an important keyboard-navigation indicator. Add a clearly visible border, box shadow, or replacement outline.
Styling border when the visible ring is an outline
input {
border: 0;
}
This changes the normal border but does not necessarily affect the browser's focus outline. Target the focus state with :focus or :focus-visible.
Using border and causing layout movement
input:focus {
border: 3px solid #2563eb;
}
If the unfocused border is 1px, the input becomes larger on focus and nearby content can shift. Reserve the same border width initially, or use an outline or box shadow instead.
input {
border: 3px solid transparent;
}
input:focus-visible {
border-color: #2563eb;
}
Removing outlines globally
/* Avoid */
*:focus {
outline: none;
}
This affects links, buttons, form controls, and custom widgets across the entire site. It can make the site unusable by keyboard. Use a global replacement rule only if it adds an equally visible focus treatment.
Forgetting non-input controls
Keyboard users also tab to buttons, links, selects, checkboxes, and custom controls. A focus design should cover all interactive elements, not only text inputs.
Comparisons
| Feature | border | outline | box-shadow |
|---|---|---|---|
| Draws outside the element | No | Usually | Yes |
| Affects layout size | Yes | No | No |
| Supports rounded corners | Yes | Browser-dependent appearance | Yes |
| Useful for focus rings | Sometimes | Yes | Yes |
| Can be offset from the element | No | Yes, with outline-offset | Yes, by spread/blur settings |
| Selector | When it matches | Typical use |
|---|---|---|
:focus | Any time the element has focus | General focus state or fallback support |
:focus-visible | When the browser determines a visible indicator is needed | Keyboard-friendly focus styling |
:focus-within | The element or one of its descendants has focus | Highlighting a form-field wrapper |
Use :focus-visible when possible. It preserves an obvious keyboard cue without necessarily showing a ring after every mouse click.
Cheat Sheet
/* Style every focused input */
input:focus {
outline: 2px solid #2563eb;
}
/* Preferred keyboard-focus indicator */
input:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 2px;
}
/* Replace the browser ring with a custom ring */
input:focus {
outline: none;
box-shadow: 0 0 0 3px rgb(37 99 235 / 35%);
}
/* Highlight a wrapper containing the focused input */
.field:focus-within {
border-color: #2563eb;
box-shadow: 0 0 0 3px rgb(37 99 235 / 25%);
}
- The browser focus ring is commonly an
outline, not aborder. - Do not hide focus styling unless you provide a clear replacement.
outlinedoes not normally alter layout; a thickerborderdoes.- Use
:focus-visiblefor keyboard-oriented focus styles. - Test with the Tab and Shift+Tab keys, not only with a mouse.
FAQ
How do I remove the blue outline around an HTML input?
You can use input:focus { outline: none; }, but you should add a visible replacement such as a border or box shadow so keyboard users can see the focused field.
Why does changing border not remove the focus ring?
The browser often draws the ring with the outline property or an internal focus style. A border and an outline are separate visual layers.
Should I use :focus or :focus-visible?
Use :focus-visible for a keyboard-focused ring in modern browsers. Use :focus when every focus method needs the same style or when supporting an appropriate fallback strategy.
Does outline: none prevent an input from being focused?
No. It only changes appearance. The input still receives focus, accepts text, and participates in keyboard navigation.
How can I avoid the input moving when it receives focus?
Use outline or box-shadow, which do not affect layout. Alternatively, give the input the same border width in both normal and focused states.
Is a focus ring required for accessibility?
A visible focus indicator is essential for keyboard operation. You may customize its appearance, but it should remain noticeable against the surrounding interface.
Can I style the parent of a focused input?
Yes. Use :focus-within on the parent container. It matches while any descendant element has focus.
Mini Project
Description
Create a small contact form with text fields and a submit button. Give each control a consistent, visible keyboard-focus ring that fits the interface rather than relying on the browser default.
Goal
Build a form whose focused controls are easy to identify with the keyboard and do not shift the page layout.
Requirements
Create fields for name, email, and message.
Add a visible focus treatment for text inputs, the textarea, and the submit button.
Use :focus-visible for the primary focus ring.
Ensure the focus treatment does not change an element's layout size.
Associate each field with a visible label.
Keep learning
Related questions
Angular ngClass Conditional Class Binding Explained
Learn how to use Angular ngClass for conditional classes, fix common binding mistakes, and understand why this template error happens.
Apply CSS to an iframe: Same-Origin Styling in HTML
Learn why parent-page CSS does not style iframe content, how same-origin rules work, and practical ways to style embedded pages safely.
Apply Multiple CSS Transforms: rotate() and translate()
Learn how to combine multiple CSS transform functions such as rotate() and translate(), understand transform order, and avoid overwritten rules.