Favorites

Remove Dotted Outlines in CSS

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

What are those dotted outlines?

We've all seen them: a dotted line, or in newer browsers a solid blue or black ring, that appears around a link or button. Many designers' first instinct is to get rid of it. But before you do, it helps to know what it's for.

That line is a focus indicator. An element has focus when it's the one that will respond to the keyboard. Press the Tab key on almost any web page and you'll see focus jump from link to link, button to button. Press Enter and the focused link opens.

Try it here. Click just above this box, then press Tab a few times:

Results

Lots of people navigate this way: people who can't use a mouse because of a disability, people using screen readers, power users who prefer the keyboard, and anyone whose trackpad has just stopped working. For all of them, the focus indicator is like the mouse pointer: it shows where they are on the page.

Why not just remove them?

The original version of this post showed how to remove the outline completely:

/* Please don't do this */
a, a:active, a:focus {
  outline: none;
}

This was common advice for years, and it's still all over the web. The problem is what it does to keyboard users. Try tabbing through these, which have had their outline removed:

Results

Focus is still moving, but there's no way to see where it is. Imagine trying to use a website where the mouse pointer was invisible: that's what outline: none does to keyboard users.

Key rule: the WCAG accessibility guidelines require that keyboard focus is always visible (success criterion 2.4.7, "Focus Visible"). Removing the outline without adding something else in its place fails this requirement.

Why did people want them gone?

The real complaint was never about keyboard users. It was that older browsers also showed the outline when you clicked a link with the mouse. The outline then lingered around the link after clicking, which looked untidy. Fortunately, modern CSS solves exactly that problem.

The modern fix: :focus-visible

CSS has two focus pseudo-classes. A pseudo-class styles an element when it's in a particular state:

Pseudo-classMatches when...
:focusThe element has focus, however it got it: keyboard, mouse click or tap
:focus-visibleThe element has focus and the browser thinks a visible indicator would help, which mainly means keyboard use

So :focus-visible gives you exactly what designers wanted all along: no ring when someone clicks with a mouse, but a clear ring when someone uses the keyboard.

The good news: browsers already do this

All modern browsers now use :focus-visible for their own built-in focus rings. Click a link or button with your mouse and, in most cases, no ring appears. Tab to it with the keyboard and the ring shows up. That means:

Tip: for most websites, you don't need to remove the outline at all. The behaviour that annoyed people in the past has already been fixed by browsers.

If you've inherited outline: none

Old stylesheets, themes and resets often still contain outline: none. If yours does, the safest fix is to remove that rule. If you can't, add a :focus-visible rule after it to bring the indicator back for keyboard users:

/* An old theme removed the outline... */
a:focus {
  outline: none;
}

/* ...so bring it back for keyboard users */
a:focus-visible {
  outline: 3px solid #1d4ed8;
  outline-offset: 3px;
}
Results

Click these with your mouse and nothing changes. Tab to them with the keyboard and a clear blue ring appears.

Designing a better focus ring

Rather than removing the default ring, most designers now replace it with one that matches their site. The outline property is ideal for this. Unlike a border, an outline doesn't take up space, so adding one never pushes the layout around.

:focus-visible {
  outline: 3px solid #f59e0b;
  outline-offset: 3px;
}

Writing :focus-visible on its own, with no element in front, applies it to everything that can receive focus: links, buttons, form fields and more.

Results

Line by line

PropertyWhat it does
outlineShorthand for the ring's width, style and colour, just like border
outline-offsetThe gap between the element and the ring. A few pixels makes the ring easier to see.

Tips for a good focus ring

  • Make it thick enough. At least 2px, and 3px is easier to spot.
  • Give it contrast. The ring should stand out against the background with a contrast ratio of at least 3 to 1.
  • Keep it consistent. Use the same style everywhere, so visitors learn what focus looks like on your site.
  • Mind dark sections. A dark blue ring vanishes on a navy header. Use a lighter colour there, or a ring with two colours (see below).

Good to know: outlines used to be square even on rounded buttons. Modern browsers now make the outline follow the element's border-radius, so a rounded button gets a rounded ring automatically.

Other focus styles and when to use them

A two-colour ring with box-shadow

box-shadow can draw several rings at once. A white inner ring with a coloured outer ring stays visible on both light and dark backgrounds:

.button:focus-visible {
  outline: 2px solid transparent;
  box-shadow: 0 0 0 3px #ffffff, 0 0 0 6px #1d4ed8;
}
Results

Why keep a transparent outline? Some people use Windows high contrast mode, which replaces your colours with their own and removes box shadows completely. A transparent outline is invisible normally, but in high contrast mode Windows gives it a visible colour, so the focus indicator still shows.

Highlighting a group with :focus-within

:focus-within matches an element when anything inside it has focus. It's handy for highlighting a whole card or form section while someone is filling it in:

.form-group:focus-within {
  border-color: #1d4ed8;
  background: #eff6ff;
}
Results

This is an extra, not a replacement: the field itself still has its own focus ring.

Making sure focus isn't hidden

A focus ring is no use if something covers it. Sticky headers and cookie banners often hide the focused element as the page scrolls. The scroll-padding property tells the browser to leave room when it scrolls focused items into view:

html {
  scroll-padding-top: 80px;  /* the height of your sticky header */
}

Testing your focus styles

Testing is simple and takes only a few minutes. Put your mouse aside and try your site with the keyboard alone:

  1. Click in the browser's address bar, then press Tab to move into the page.
  2. Keep pressing Tab and check you can always see which item has focus.
  3. Press Shift + Tab to move backwards.
  4. Press Enter on links and Space on buttons and checkboxes to check they work.
  5. Watch for focus disappearing behind headers, banners or popups.

Tip: on a Mac, Safari skips links when tabbing by default. Turn on "Press Tab to highlight each item on a webpage" in Safari's Advanced settings, or use Option + Tab.

Wrapping up

Those dotted outlines are there for a good reason: they're how keyboard users see where they are. Instead of removing them, let browsers' built-in :focus-visible behaviour hide them for mouse users, and style :focus-visible yourself if you want a ring that matches your design. Your site will look tidy for everyone and stay usable for everyone too.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee