brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: a transparent outline in CSS keeps focus visible in forced colors mode

Css
~/tips/tip-css-forced-colors-transparent-outline

button:focus-visible {

Focus styles made with box-shadow still show up for people who use a high contrast theme.

Windows contrast themes replace the colors on a page with a small system palette, and they drop box-shadow completely. If your focus ring is a shadow, keyboard users in that mode see nothing.

button:focus-visible {
  box-shadow: 0 0 0 3px #0b6bcb;
  outline: 2px solid transparent;
}

@media (forced-colors: active) {
  .icon-button svg { fill: ButtonText; }
}

The transparent outline is invisible in normal mode, so the shadow is all you see. In forced colors mode the browser gives the outline a real system color, and suddenly there is a visible ring. One line, no media query needed.

The media query is for everything else. forced-colors: active matches when such a theme is on. Inside it, use system color keywords like CanvasText, ButtonText and Highlight so your custom parts follow the user’s palette. Inline SVG icons with a hard coded fill are a typical thing that vanishes otherwise.

Chrome 89, Firefox 89 and Safari 16 support the media feature. In Chrome DevTools I can emulate it from the Rendering panel. The caveat: do not try to undo the user’s colors with forced-color-adjust: none unless a part really breaks, because those colors are the reason the mode exists.

Further reading: MDN: forced-colors, MDN: system colors

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link