Magic PostCSS Custom Combinators using :has()

Similar to how you can (ab)use :nth-child() to create “new” CSS selectors, you can leverage :has() to create some typical combinators. Brandon McConnell did just that: y:has(+ x) selects the first preceding y sibling of x: y:has(~ x) selects all preceding y sibling of x: x + y, y:has(+ x) selects the first preceding and …


This content originally appeared on Bram.us and was authored by Bramus!

Similar to how you can (ab)use :nth-child() to create “new” CSS selectors, you can leverage :has() to create some typical combinators. Brandon McConnell did just that:

  • y:has(+ x) selects the first preceding y sibling of x:

  • y:has(~ x) selects all preceding y sibling of x:

  • x + y, y:has(+ x) selects the first preceding and first succeeding y siblings of x:

And those are just the simple ones … what about selecting “all preceding y elements up to the third one”? That would be y:has(~ * + * + x) 😅

🤔 Not familiar with the CSS :has() selector? Read up on it right here.

~

To more easily work with that, Brandon also created a (still unpublished?) PostCSS plugin that supports fictitious CSS Selectors which automatically translate to their working counterparts. For example:

  • x - y translates to y:has(+ x)
  • x -- y translates to y:has(~ x)
  • x % y translates to x + y, y:has(+ x)

Here’s a pen with all combinators he came up with:

Oh that’s clever! Perhaps we might one day see these selectors land in CSS itself?

👨‍🔬 Only Safari Technology Preview 137 supports :has() at the time of writing, so you’ll need to use that browser to see anything get colored in the “Actually Rendered” rows there.

During his work Brandon noticed a bug in Safari’s implementation in which it selects some non-targeted elements, so you might see some faulty ones.


This content originally appeared on Bram.us and was authored by Bramus!


Print Share Comment Cite Upload Translate Updates
APA

Bramus! | Sciencx (2022-01-02T00:08:31+00:00) Magic PostCSS Custom Combinators using :has(). Retrieved from https://www.scien.cx/2022/01/02/magic-postcss-custom-combinators-using-has/

MLA
" » Magic PostCSS Custom Combinators using :has()." Bramus! | Sciencx - Sunday January 2, 2022, https://www.scien.cx/2022/01/02/magic-postcss-custom-combinators-using-has/
HARVARD
Bramus! | Sciencx Sunday January 2, 2022 » Magic PostCSS Custom Combinators using :has()., viewed ,<https://www.scien.cx/2022/01/02/magic-postcss-custom-combinators-using-has/>
VANCOUVER
Bramus! | Sciencx - » Magic PostCSS Custom Combinators using :has(). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/02/magic-postcss-custom-combinators-using-has/
CHICAGO
" » Magic PostCSS Custom Combinators using :has()." Bramus! | Sciencx - Accessed . https://www.scien.cx/2022/01/02/magic-postcss-custom-combinators-using-has/
IEEE
" » Magic PostCSS Custom Combinators using :has()." Bramus! | Sciencx [Online]. Available: https://www.scien.cx/2022/01/02/magic-postcss-custom-combinators-using-has/. [Accessed: ]
rf:citation
» Magic PostCSS Custom Combinators using :has() | Bramus! | Sciencx | https://www.scien.cx/2022/01/02/magic-postcss-custom-combinators-using-has/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.