Focus Within lets you style elements when they are focused or contain a focused element, following the Selectors Level 4 specification.
Usage
From the command line, transform CSS files that use :focus-within
selectors:
npx focus-within SOURCE.css TRANSFORMED.css
Next, use your transformed CSS with this script:
That’s it. The script is 379 bytes and works in all browsers, including Internet Explorer 9.
How it works
The PostCSS plugin duplicates rules containing
:focus-within
, replacing them with an alternative [focus-within]
selector.
/* becomes */
Next, the JavaScript library adds a focus-within
attribute to elements otherwise matching :focus-within
natively.
Field Field Some sibling text element.
GOTCHA!
One cannot simply add the
[focus-within]
selector to an existing rule:Browsers that don't support
:focus-within
will throw the entire rule away! This is why you should follow the Usage instructions.