Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I achieve a consistent focus outline color with shadow-dom web components?

I'm using web components, mostly written with lit-element and lit-html, and they use the Shadow DOM.

Some components have buttons and other interactive parts which have the default focus outline.

When I build an app, i like to set a page-wide rule to change the focus outline to a color that clearly stands out from the page's background colors.

*:focus {
  outline: 2px solid lime;
}

i was expecting this to flow over shadow boundaries into the web components, similar to color and font-size, but it does not

considering that outline does not cross the shadow boundaries — what options do i have to achieve my consistent focus outline color?

like image 945
ChaseMoskal Avatar asked Nov 26 '25 00:11

ChaseMoskal


1 Answers

Standardize CSS variable --focus-outline in your app

application-level:

* {
  --focus-outline: 2px solid lime;
}

*:focus {
  outline: var(--focus-outline);
}

example component usage:

*:focus {
  outline: var(--focus-outline, 2px solid #0af);
}

every component must be refactored to accept this variable

each component will have to define its own fallback like 2px solid #0af (unfortunately there is not yet any standardized default-focus-outline css value)

like image 150
ChaseMoskal Avatar answered Nov 27 '25 14:11

ChaseMoskal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!