Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do :focus-visible and :focus style keyboard focus differently?

Quick answer

:focus matches an element whenever it has focus. :focus-visible matches a focused element when the browser determines that a visible focus indicator is appropriate. It is a more selective styling hook, not a different kind of focus: an element can have focus without matching :focus-visible. Keep a clear visible focus indication so users can locate the active control. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What is the practical difference between :focus and :focus-visible?

Geez

Both selectors apply to elements that have focus. :focus matches whenever an element has focus. :focus-visible matches when the browser determines that focus should be visibly indicated. So an element can have focus without matching :focus-visible; the selector is more selective about when its styling applies. [S1]

For a visible indicator, you could start with:

.control:focus-visible {
  outline: 3px solid #175cd3;
  outline-offset: 3px;
}

This is an example appearance, not a guarantee that these colors or dimensions suit every control. Check that the indicator is easy to see in your design. [S2]

Follow-up question

Should I also write a :focus rule for my buttons and custom control?

Geez

Use :focus if you intend to style every focused state. Use :focus-visible for a style when the browser identifies a visible focus indicator as appropriate. The distinction does not mean the control lacks focus when it does not match :focus-visible. [S1]

Standard focusable elements may already have browser focus styling. Check your custom control too: custom focusable components need focus styling. Avoid removing an outline if it is the only visible indication; if you change the styling, keep a visible indicator. [S2]

For example, this applies the same treatment to both kinds of control when they match :focus-visible:

button:focus-visible,
.custom-control:focus-visible {
  outline: 3px solid #175cd3;
  outline-offset: 3px;
}

Follow-up question

How can I check the result in a browser without assuming the CSS works?

Geez

Try the actual controls on the page, including the custom one:

  1. Use the keyboard to move through the controls. Check that you can see which one currently has focus.
  2. Continue through the custom control and any components with reset or custom styles. Look for rules that remove the only visible focus indication. [S2]
  3. Use a pointer with the controls and observe whether :focus-visible styling appears. The browser determines when the selector matches, so do not assume pointer and keyboard interactions produce identical results. [S1]

If a control is hard to locate during the keyboard check, review its focus styling and repeat the check. This is a practical review of the component, not proof that an entire interface has been evaluated.

Follow-up question

Can you give me a compact rule and checklist I can reuse when reviewing a component?

Geez

Start with a visible style for the component’s focused state, such as:

.component:focus-visible {
  outline: 3px solid #175cd3;
  outline-offset: 3px;
}

Then check the component in context. The key distinction is that :focus matches focused elements generally, while :focus-visible applies when the browser considers a visible indication appropriate. [S1]

Checklist:

  1. Identify the standard and custom controls that can receive focus.
  2. Check that focused controls have a visible indicator; do not remove the only one. [S2]
  3. Navigate through the controls by keyboard and confirm the active control is easy to locate.
  4. Check pointer interactions and review any styles that reset outlines.
  5. Repeat the checks on custom controls and components with their own styles.

Treat the example as a starting point: check the appearance on your actual controls and page.

A practical checklist

  • Identify standard and custom controls that can receive focus.

  • Check that focused controls have a visible indicator; do not remove the only one.

  • Navigate through the controls by keyboard and confirm the active control is easy to locate.

  • Check pointer interactions and review any styles that reset outlines.

  • Repeat the checks on custom controls and components with their own styles.

A few more useful answers

Does an element lose focus when it does not match `:focus-visible`?

No. It can still have focus; :focus-visible is more selective about when visible-focus styling applies. [S1]

Can I remove the browser’s default outline?

Do not remove the only visible focus indication. If you change the styling, keep a visible indicator for focused controls. [S2]

Does the CSS example prove the style works in every browser?

No. It is a styling example, not a browser compatibility test. Check the behavior on the browsers and components relevant to your page.

Sources

References used for this guide.

  1. [S1] MDN: CSS pseudo-classes, including :focus and :focus-visible
    developer.mozilla.org · Checked
  2. [S2] MDN: Keyboard accessibility and visible focus styling
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work