Home Web Front-end CSS Tutorial Understanding the Difference Between `:focus` and `:focus-visible` in CSS

Understanding the Difference Between `:focus` and `:focus-visible` in CSS

Nov 24, 2024 am 07:59 AM

Understanding the Difference Between `:focus` and `:focus-visible` in CSS

Understanding the Difference Between :focus and :focus-visible in CSS

I have learned my fair share about the importance of keyboard accessibility, so I know that visual indication of the focused element is very important. But the well-known :focus pseudo-class is not always the best fit for this job. That's where :focus-visible comes in. Let's look at the differences between these two pseudo-classes and explore the best practices for using them effectively.

What is the :focus Pseudo-Class?

The :focus pseudo-class is a CSS selector that applies styles to any element that receives focus, regardless of how that focus was triggered. This includes focus events from keyboard navigation, mouse clicks, and touch interactions.

Example Usage of :focus

button:focus {
  outline: 2px solid blue;
}
Copy after login
Copy after login

In this example, the button will display a blue outline whenever it is focused, whether the user clicks on it with a mouse, taps it on a touchscreen, or navigates to it using the keyboard.

What is the :focus-visible Pseudo-Class?

The :focus-visible pseudo-class is more specialized. It only applies styles to an element when the browser determines that the focus should be visible. This typically occurs when the user navigates via the keyboard or assistive technologies rather than through mouse or touch input.

Example Usage of :focus-visible

button:focus-visible {
  outline: 2px solid blue;
}
Copy after login

Here, the button will only show a blue outline when focused through keyboard navigation or another input method that usually requires visible focus indicators.

Key Differences Between :focus and :focus-visible

:focus

  • Behavior: Applies to any element that receives focus, regardless of the input method.
  • Use Cases: Ensures that all interactions with the element are visually indicated, whether by mouse, keyboard, or touch.

:focus-visible

  • Behavior: Applies styles only when the focus should be visible, such as using a keyboard or assistive technology.
  • Use Cases: Ideal for scenarios where you want to provide focus indicators only to keyboard and assistive technology users while avoiding unnecessary outlines for mouse and touch users, typically required by design.

Accessibility Implications

:focus

  • Pros:
  • Guarantees that all users can see when an element is focused, which is critical for accessibility.
  • Cons:
  • Can lead to a suboptimal experience for mouse users, as focus styles may appear unnecessarily during mouse interactions.

:focus-visible

  • Pros:
  • Enhances user experience by showing focus indicators only when necessary, thus keeping the interface clean for mouse and touch users.
  • Tailors the experience for keyboard and assistive technology users, providing them with clear visual cues.
  • Cons:
  • Additional considerations may be required to ensure that focus indicators are not accidentally omitted, especially in older browsers that do not support :focus-visible.
    • There may be cases where you want to show focus indicators for all users, regardless of input method.

Best Practices for Using :focus and :focus-visible

To achieve the best accessibility and user experience, combining both :focus and :focus-visible in your CSS is often a good idea.

Combining :focus and :focus-visible

button:focus {
  outline: 2px solid blue;
}
Copy after login
Copy after login

Here is a Stackblitz example of what such styling could look like for you to try out and play with:

Additional Tips

  • Test with Keyboard and Assistive Technology: Ensure that your web application is navigable using a keyboard (Tab, Shift Tab, etc.) and that focus indicators are visible for those who rely on them. It's never a bad idea to include accessibility testing in your e2e testing suite.
  • Provide Clear Focus Indicators: Make sure that focus indicators are prominent and easy to see. A subtle or hard-to-spot focus indicator can severely impact accessibility for users who rely on keyboard navigation.

Conclusion

The :focus-visible pseudo-class offers a more refined way to manage focus indicators, improving accessibility and user experience, particularly for keyboard and assistive technology users. By understanding the differences between :focus and :focus-visible, and applying best practices in your CSS, you can create more accessible and user-friendly web applications.

Remember, accessibility should never be an afterthought. By thoughtfully applying focus styles, you ensure that all users, regardless of how they interact with your site, can easily navigate and interact.

The above is the detailed content of Understanding the Difference Between `:focus` and `:focus-visible` in CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

How to Use CSS Grid for Sticky Headers and Footers How to Use CSS Grid for Sticky Headers and Footers Apr 02, 2025 pm 06:29 PM

CSS Grid is a collection of properties designed to make layout easier than it’s ever been. Like anything, there's a bit of a learning curve, but Grid is

Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

See all articles