Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the fallback example for :focus-visible #19993

Merged
merged 7 commits into from
Aug 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions files/en-us/web/css/_colon_focus-visible/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,43 +57,39 @@ input, button {

{{EmbedLiveSample("Basic_example", "100%", 300)}}

### Selectively showing the focus indicator
### Providing a :focus fallback

A custom control, such as a custom element button, can use `:focus-visible` to selectively apply a focus indicator only on keyboard-focus. This matches the native focus behavior for controls like {{htmlelement("button")}}.
If your code has to work in old browser versions that do not support `:focus-visible`, check supports of `:focus-visible` with {{cssxref("@supports")}} and repeat the same focus styling in it, but inside a `:focus` rule. Note that even if you do not specify anything at all for `:focus`, old browsers will simply display the native outline, which can be enough.

```html
<button class="custom-button">Click Me</button>
<button class="button with-fallback" type="button">
Button with fallback
</button>
<button class="button without-fallback" type="button">
Button without fallback
</button>
```

```css
.custom-button {
display: inline-block;
.button {
margin: 10px;
border: 2px solid darkgray;
border-radius: 4px;
}

.custom-button:focus {
/* Provide a fallback style for browsers
that don't support :focus-visible */
outline: 2px solid red;
background: lightgrey;
.button:focus-visible {
/* Draw the focus when :focus-visible is supported */
outline: 3px solid deepskyblue;
outline-offset: 3px;
}

@supports selector(:focus-visible) {
.custom-button:focus {
/* Remove the focus indicator on mouse-focus for browsers
that do support :focus-visible */
outline: none;
background: transparent;
@supports not selector(:focus-visible) {
.button.with-fallback:focus {
/* Fallback for browsers without :focus-visible support */
outline: 3px solid deepskyblue;
outline-offset: 3px;
}
}

.custom-button:focus-visible {
/* Draw a very noticeable focus style for
keyboard-focus on browsers that do support
:focus-visible */
outline: 4px dashed darkorange;
background: transparent;
}
```

{{EmbedLiveSample("Selectively_showing_the_focus_indicator", "100%", 72)}}
Expand Down