Don't Set outline: 0 or outline: none for Focus-Styles
Tags: css, a11y, webdev, react
Published: 2022-07-28 00:00:00

---
title: Don't Set outline: 0 or outline: none for Focus-Styles
published: true
date: 2022-07-28 00:00:00 UTC
tags: css, a11y, webdev, react
canonical_url: https://eevis.codes/blog/2022-07-28/dont-set-outline-0-or-outline-none-for-focus/
---

"The focus indicator is ugly, and I will remove it. No, this is not up for discussion." These are some lines from a conversation with a designer-developer some years back. The theme of the conversation was a website they were working with.

I tried to explain the problem with non-existing focus styles. Still, they insisted their view of beauty was more important than the customer's right to a working website. 

And this has not been the only conversation I've had around focus styles. Some people want, stubbornly, to remove all focus styles because they (as a mouse user) don't like those focus rings.

In this blog post, I'll share why these visible focus styles are so important - and why you should never set `outline`-property to `0` or `none` for focus styles. But let's first talk about the property itself. 

## What is `outline`?

`outline` is a CSS property, or actually, a CSS shorthand property. That means that you can set multiple properties with it. These properties are `outline-color`, `outline-style`, and `outline-width.` You can set one, two, or three attributes simultaneously. Here's an example:

```css
.className {
  outline: 2px solid red;
}
```

In this example, the outline is set to be a solid red line, which is 2px wide.

In the box model, `outline` is set _outside_ the box's border edge and does not add to the element's size, meaning it doesn't take any space from the page layout.

You can modify the looks of the outline with two more properties: `outline-offset`, which affects how far from the border of an element the outline is, and `border-radius`, which modifies the radius of the border. It affects the whole element, not just outline.

You can [read more about the `outline` in MDN.][2]

## `outline` and Focus

The default styles for focus indicators are implemented with the `outline`-property. Every browser has its own default styles for this focus indicator.

These default styles are defined with user agent stylesheets. Jens Oliver Meier has written more about them, if you're interested: ["User Agent Style Sheets: Basics and Samples"][1]. 

Here are examples of default focus indicators for Chrome, Firefox, and Safari, as seen on Mac:

Chrome:

![Link with text "Beauty of Finland's national parks still drawing crowds after pandemic peaks" in blue, and a darker blue outline surrounding the text.](//images.ctfassets.net/sk24pdnnlwhc/4F7kQwDJXFg3k9UE0h9MC9/2c180877e24f5a724f394d785ed674e2/Screenshot_2022-07-28_at_6.28.45.png)

Firefox:

![Link with text "Beauty of Finland's national parks still drawing crowds after pandemic peaks" in blue in two rows, and a semi-blue outline surrounding each of the lines.](//images.ctfassets.net/sk24pdnnlwhc/7vgr80VHtXe4tIKrBrLJhY/8c1da50d8d8395f1eb88360526c69351/Screenshot_2022-07-28_at_6.30.56.png)

Safari:

![Link with text "Beauty of Finland's national parks still drawing crowds after pandemic peaks" in blue, and a light blue outline surrounding the text. The outline has a bit of padding between it and the text.](//images.ctfassets.net/sk24pdnnlwhc/5cjC6GFzzlMpOnJCDRZSrw/7d2d4721f93a8e6c1cedbb01b539cdf5/Screenshot_2022-07-28_at_6.33.10.png)

The examples are from the Finnish Broadcasting Company's (Yle) website. I've disabled the author styles with the Web Developer extension. 

As you might notice from the examples, these styles aren't apparent in most conditions. The default focus indicator styles do pass WCAG criteria about visible focus. Still, I think accessibility shouldn't be just about passing the success criteria. It should be about inclusion, and thus creating more visible focus styles is a must. 

## Why Visible Focus Styles Are Important?

So, why having visible focus styles is so important? It's because not everyone uses a mouse. Many people prefer or need to use tools like keyboards, switch devices, or others to navigate the page. And when they do so, they don't have the cursor of a mouse to tell them where they are on the page - they rely on focus styles.

So, to put it short - not having visible focus styles is like using a website with a mouse, but the cursor is invisible. 

## But What If I Have Better Focus Styles?

The outline completely disappears when you set `outline` to none or 0. Now you might ask, "But what if I have better focus styles? Why can't I remove the outline then?" 

The problem is that it removes the outline from everywhere - also, from, for example, Windows High Contrast Mode (WHCM) users. WHCM works by removing the background colors and images and replacing text color (and some other colors) with the selected theme's colors. That affects things like `box-shadow` - it's not visible at all. And that, in turn, means that most of the enhanced focus styles won't appear.

"So, I'm stuck with a visible outline, then?" I have good news: No, you're not! You can actually use the `transparent`-keyword like this:

```css
.some-element:focus {
  outline: 1px solid transparent;
}
```

That will show up in WHCM as it forces the colors on existing outlines and will be transparent in other cases. And also, because the outline doesn't take up space on the boxes, the transparent outline will be invisible in other cases.

## Wrapping Up

So, the gist of this blog post is: never set `outline`-property to `0` or `none` for focus styles, use the `transparent`-keyword for the color in these cases. And if you do this, remember to add (more) visible focus styles via the chosen alternative method.

## Links 
- [Read more about the `outline` in MDN][2]
- ["User Agent Style Sheets: Basics and Samples"][1]

[1]: https://meiert.com/en/blog/user-agent-style-sheets/
[2]: https://developer.mozilla.org/en-US/docs/Web/CSS/outline