All browsers adopted :focus-visible in their UA stylesheets (#tilPost)

I’ve had many discussions with folks bothered by focus outlines on link and button clicks. "Can we remove these click lines?" was a sentence I often heard. But, at the time, the :focus pseudo-class was the only thing we ha…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

I've had many discussions with folks bothered by focus outlines on link and button clicks. "Can we remove these click lines?" was a sentence I often heard. But, at the time, the :focus pseudo-class was the only thing we had, and removing focus outlines entirely would break keyboard accessibility. So we had to push back and fight for proper focus outlines!

Luckily, the :focus-visible pseudo-class helped out. Using :focus-visible, you could only show outlines when a person navigated links and buttons via the keyboard. Win-win! 💪

But then, a tricky transition period started. How should we work around browsers that don't support the pseudo-class yet? There are two ways.

/* 
  Enable focus styles if 
  :focus-visible is not supported
*/
.button {
  outline: 0;
}

.button:focus-visible {
  outline: 3px solid deepskyblue;
}

@supports not selector(:focus-visible) {
  .button:focus {
    /* Fallback for browsers without :focus-visible support */
    outline: 3px solid deepskyblue;
  }
}

/* 
  Disable outlines if :focus-visible is supported
*/
:focus {
  outline: 3px solid deepskblue;
}

:focus:not(:focus-visible) {
  outline: none;
}

Today in 2022, though, :focus-visible is cross-browser supported.

MDN Compat Data (source)
Browser support info for :focus-visible
chrome chrome_android edge firefox firefox_android safari safari_ios samsunginternet_android webview_android
86 86 86 85 15.4 15.4 14.0 86

And after reading one of Michelle Barker's articles, I learned that all major browsers removed the standard :focus outline and went for :focus-visible instead.

And indeed, when I think about it, I haven't seen click outlines for quite some time.

:focus-visible still matches focused input elements, so they always show outlines and work as usual.

I investigated the user agent stylesheets to find out when these changes were applied. Chrome and Firefox adopted :focus-visible in February 2021 and Safari followed in December 2021.

If you want to look at the default user agent stylesheets. Here they are:

And it's just wonderful; now that :focus-visible is cross-browser supported, and it made its way into the user agent stylesheets, we don't have to worry about it 99% of the time.

The web developer community requested a feature. Browser makers came up with a solution, which we all happily applied. And then, at a later stage, the browsers adopted the solution so that we don't have to deal with it. I love it!


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2022-09-13T22:00:00+00:00) All browsers adopted :focus-visible in their UA stylesheets (#tilPost). Retrieved from https://www.scien.cx/2022/09/13/all-browsers-adopted-focus-visible-in-their-ua-stylesheets-tilpost/

MLA
" » All browsers adopted :focus-visible in their UA stylesheets (#tilPost)." Stefan Judis | Sciencx - Tuesday September 13, 2022, https://www.scien.cx/2022/09/13/all-browsers-adopted-focus-visible-in-their-ua-stylesheets-tilpost/
HARVARD
Stefan Judis | Sciencx Tuesday September 13, 2022 » All browsers adopted :focus-visible in their UA stylesheets (#tilPost)., viewed ,<https://www.scien.cx/2022/09/13/all-browsers-adopted-focus-visible-in-their-ua-stylesheets-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » All browsers adopted :focus-visible in their UA stylesheets (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/13/all-browsers-adopted-focus-visible-in-their-ua-stylesheets-tilpost/
CHICAGO
" » All browsers adopted :focus-visible in their UA stylesheets (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2022/09/13/all-browsers-adopted-focus-visible-in-their-ua-stylesheets-tilpost/
IEEE
" » All browsers adopted :focus-visible in their UA stylesheets (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2022/09/13/all-browsers-adopted-focus-visible-in-their-ua-stylesheets-tilpost/. [Accessed: ]
rf:citation
» All browsers adopted :focus-visible in their UA stylesheets (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2022/09/13/all-browsers-adopted-focus-visible-in-their-ua-stylesheets-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.