Table of Contents
Meet WCAG focus style standards
Manage focus styles with CSS custom attributes
Contour appearance improvement
Focus style demonstration
Home Web Front-end CSS Tutorial Standardizing Focus Styles With CSS Custom Properties

Standardizing Focus Styles With CSS Custom Properties

Mar 16, 2025 am 11:13 AM

Standardizing Focus Styles With CSS Custom Properties

Quickly test your project: Open your browser, use the Tab key only, and try navigating between interactive elements such as buttons, links, and form elements.

If you are a user with normal vision, you should be able to see the visual changes when the focus jumps between DOM elements. But if there is no visual change, or the change is minimal, then you can find a way to make a huge difference for your visitors.

This article will introduce a technique to more efficiently manage project focus styles using CSS custom properties and learn modern CSS focus selectors. First, let's understand why visible focus styles are so important.

Meet WCAG focus style standards

The visible focus status complies with the Web Content Accessibility Guidelines (WCAG) Success Standard 2.4.7 – Focus is visible. The "Understanding" document in 2.4.7 states the intention of this standard:

The purpose of this success criterion is to help the user understand which element has keyboard focus. It must be able to let the user know which element of multiple elements has keyboard focus.

In the upcoming WCAG 2.2, a new standard will be added to clarify “how obvious the focus indicator should be.” While still in the draft stage, getting familiar with and applying the guide in 2.4.11 – Focus Appearance (Minimum) is undoubtedly an active step to improve focus styles you can take today.

Manage focus styles with CSS custom attributes

One technique I started using this year is to include the following settings as early as possible on the main interactive basic elements in my cascading stylesheet:

 <code>:is(a, button, input, textarea, summary) { --outline-size: max(2px, 0.08em); --outline-style: solid; --outline-color: currentColor; } :is(a, button, input, textarea, summary):focus { outline: var(--outline-size) var(--outline-style) var(--outline-color); outline-offset: var(--outline-offset, var(--outline-size)); }</code>
Copy after login

This attaches custom properties, allowing you to flexibly customize certain parts of the outline style as needed to ensure that the focus remains visible when the element context changes.

For --outline-size , we use max() to ensure a value of at least 2px while allowing scaling based on 0.08em based on components (e.g., large buttons or links within the title).

The property you may not be familiar with here is outline-offset , which defines the space between the element and the outline. You can even provide a negative number to embed the outline, which is very useful for ensuring the contrast of the focus style. In our rule set, we set this property to accept an optional custom property --outline-offset so that it can be customized if needed, otherwise it will fall back to match --outline-size .

Contour appearance improvement

During my career, I have been asked to remove contours and have also removed contours myself because they are considered “unsightly.”

There are now two reasons why the contour should never be removed (except for accessibility effects):

  1. In Chromium and Firefox, outline now follows border-radius ! ? This means you can consider removing any hack you may have used, such as using box-shadow to simulate it (this also positively affects accessibility, as it ensures that focus styles are not removed for Windows high contrast theme users).
  2. With :focus-visible , we can ask the browser to use a heuristic to display the focus style only when an input pattern requiring visible focus is detected. In short, this means that the mouse user won't see them when clicked, and the keyboard user will still see them when Tab keys operate.

It should be noted that form elements always display focus styles—they are not limited by :focus-visible behavior.

So let's enhance our ruleset, adding the following to include :focus-visible . We will keep the initial :focus style we have defined for the old browser in case it won't be lost.

 <code>:is(a, button, input, textarea, summary):focus-visible { outline: var(--outline-size) var(--outline-style) var(--outline-color); outline-offset: var(--outline-offset, var(--outline-size)); }</code>
Copy after login

Due to the way browsers handle selectors they don't understand, we need to separate these rules, even if they define the same outline properties, we can't combine them together.

Finally, we also need this weird-looking :focus:not(:focus-visible) rule, which removes the regular focus style for browsers that support :focus-visible :

 <code>:is(a, button, input, textarea, summary):focus:not(:focus-visible) { outline: none; }</code>
Copy after login

It is worth noting that the latest versions of Chromium and Firefox have switched to using :focus-visible as the default way to apply focus styles on interactive elements and have recently been enabled as the default way in webkit, so it should be coming soon in Safari stable version! Our rules are still valid because we are customizing the outline appearance.

For more guidance on visible focus styles, I recommend Sara Soueidan’s wonderful and comprehensive guide on focus indicators, as it takes into account the upcoming 2.4.11 standard.

Focus style demonstration

This example demonstrates each of these interactive elements and how to apply custom settings using custom properties, including some changes to dark mode. Depending on your browser support, you may not see the focus style unless you use the Tab key, because :focus-visible is used.

Last thing: In terms of focus styles, button is a unique interactive element because it has additional considerations between its states, especially if you rely only on colors. For help, try using the palette generator in my project ButtonBuddy.dev.

The above is the detailed content of Standardizing Focus Styles With CSS Custom Properties. 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&#039;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.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

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&#039;s like this.

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.

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

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

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

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?

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles