Skip to content

Apply style to Child if parent has class #1188

Answered by mihkeleidast
t-monaco asked this question in Q&A
Discussion options

You must be logged in to vote

You are probably looking for the :is() pseudo-selector instead of :has(): https://developer.mozilla.org/en-US/docs/Web/CSS/:is

import { style } from "@vanilla-extract/css";

export const Parent = style({
    // Parent styles...
})

export const Child = style({
    // Child styles...

    selectors: {
        [`${Parent}:is(.open) &`]: {
            // styles that apply to Child, ONLY if Parent has the "open" class.
        }
    }
})

It should also work without the pseudo selector actually:

import { style } from "@vanilla-extract/css";

export const Parent = style({
    // Parent styles...
})

export const Child = style({
    // Child styles...

    selectors: {
        [`${Parent}.open &`]

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@t-monaco
Comment options

Answer selected by t-monaco
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants