Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override `outline:0` and revert to user agent stylesheet's focus styles?

We use a third-party styling library that removes focus styles from all focus-able elements, like this:

:focus {
  outline: 0;
}

This is shitty for accessibility, but we're not in any position to remove or fork the library.

When I toggle the rule off in devtools, we revert to the useragent stylesheet, which in Chrome is:

:focus {
    outline: -webkit-focus-ring-color auto 5px;
}

I don't want to set custom outline styles to override the useragent if I can avoid it. I'd also really like to avoid having to hardcode styles for every possible useragent stylesheet.

But the docs for outline aren't really giving me any clues here. For example, outline: initial doesn't seem to restore the browser default. Anyone know how to solve this?

like image 821
Bryce Johnson Avatar asked Mar 20 '18 17:03

Bryce Johnson


People also ask

Can you override user agent stylesheet?

You can override user agent stylesheet declarations with your author styles. And indeed, you do that every day when you write your CSS. Author styles override user styles, which override user agent styles.

What is the user agent stylesheet?

User-agent stylesheets User-agent, or browsers, have basic style sheets that give default styles to any document. These style sheets are named user-agent stylesheets. Most browsers use actual stylesheets for this purpose, while others simulate them in code. The end result is the same.


1 Answers

The best solution is to remove the focus selector from your third-party styling library CSS.

If this is not possible, then I would recommend adding an override to a stylesheet that is loaded after your third-party CSS.

Something like this should do:

    :focus {
        outline: 4px solid red;
    }

You can even add an !important declaration for good measure, if you want to.

There's no need to bother with user-agent specific values.

like image 104
Josh Avatar answered Sep 30 '22 05:09

Josh