PHPnews.io

Focus ring default styles [til/css]

Written by Remy Sharp / Original link on Jul. 28, 2020

I've been tinkering on a tool that has a custom UI but I wanted to still support keyboard navigation and with keyboard navigation comes tabbing to focus elements.

I have an element that is an underlying checkbox, but toggles between the label of "T" and "-", so I'm using CSS to change visual state:

.bool input:checked + label.t::before {
  content: 'T';
}
.bool input + label::before {
  content: '-';
  width: 100%;
  font-size: inherit;
  line-height: inherit;
}

The problem was that when I tabbed focus to the checkbox, the label text didn't look focused. I could add a border (or something) to the label, but I wanted it to look the same as the browser default focus ring.

TIL: Firefox has a Highlight that compliments webkit's focus ring colour, and (thankfully) Firefox ignores the -webkit- prefixed colour:

.bool input:focus + label::before {
  outline: 5px auto Highlight;
  outline: 5px auto -webkit-focus-ring-color;
}

Now it looks right:

focus-rings.png

Originally published on Remy Sharp's b:log

remysharp

« SymfonyCon Disneyland Paris 2020 is postponed to 2021 - Oh Mummy - 28-Jul 2020 [devlog/oh-mummy] »