trying to solve css

so if I have
<div>
I work fine
<h2>
<span>
I'm effected
</span>
</h2>
</div>

<h1>I don't work fine</h1>

<div>
I work fine
<h2>
<span>
I'm effected
</span>
</h2>
</div>

<h1>I don't work fine</h1>

and I do
:root{
--color : red;
}

span{
color : var(--color);
}
:root{
--color : red;
}

span{
color : var(--color);
}
doing
div:hover {
--color : black;
}
div:hover {
--color : black;
}
would work but if I do
h1:hover{
--color : black;
}
h1:hover{
--color : black;
}
it doesn't work. why?? don't they both change the value of the variable?
32 Replies
Jochem
Jochem13mo ago
"it doesn't work" is a bit vague. Make a codepen? also, I know this is just a quick example, but an h1 shouldn't come after an h2 like that. They should have a logical progression, and likely you should only have one per section
glutonium
glutonium13mo ago
yaa ik that, but i just made a quick example xD
Chris Bolson
Chris Bolson13mo ago
I believe that this is due to inheritance. The div:hover only changes the value of —color for itself and its children. As the h1 is outside of the div it is not affected.
Jochem
Jochem13mo ago
you're right
glutonium
glutonium13mo ago
here's a pen hmm..but since it's a global var , shouldn't it affect everywhere?
Jochem
Jochem13mo ago
yeah, it's definitely what Chris said
glutonium
glutonium13mo ago
damn, and I thought i just solved the whole css ;-;
Jochem
Jochem13mo ago
the global value is what's set in root, then that's overridden locally inside <div> by the div:hover
13eck
13eck13mo ago
h1 doesn't have the colour set to the variable
glutonium
glutonium13mo ago
aaah i see..
13eck
13eck13mo ago
So changing the variable doesn't do anything for it
Jochem
Jochem13mo ago
oh hey, that too!
glutonium
glutonium13mo ago
what do you mean
13eck
13eck13mo ago
I mean in your CSS the h1 has no color set, so changing the --color doesn't matter Only the span has color: var(--color); set
glutonium
glutonium13mo ago
it has --color : black;
13eck
13eck13mo ago
So?
Jochem
Jochem13mo ago
I think the intention might be that hovering over the h1 causes an effect on the h2
13eck
13eck13mo ago
--color doesn't mean anything by itself
glutonium
glutonium13mo ago
it's the same as the div
13eck
13eck13mo ago
You need to assign it like the span
Jochem
Jochem13mo ago
the h1 isn't supposed to change colors, the span is supposed to when you hover over h1
glutonium
glutonium13mo ago
i thought it'll override the initial value of the root variable yaa
13eck
13eck13mo ago
No, it only overrides the scoped variable
glutonium
glutonium13mo ago
the span was supposed to be effected or so I thought okeey , makes sense
13eck
13eck13mo ago
Variables are scoped to the element they're declared on. So the h1 now has an unused variable --color.
glutonium
glutonium13mo ago
yaa i get it now tnx ❤️
13eck
13eck13mo ago
I'm 80% sure that you can't do what you want with CSS alone. I'm almost positive you need JS to detect the hover and apply the color change to the span. But I'm not that used to the new :has(), :not(), etc so there may be a way 🤷
glutonium
glutonium13mo ago
i guess 😅
Chris Bolson
Chris Bolson13mo ago
Yes, you could do it with :has body:has(h1:hover) {
13eck
13eck13mo ago
And there's the 20% doubt! lawl
Kevin Powell
Kevin Powell13mo ago
I was hoping to be the one to bring up :has() 🤣 @cvanilla13eck if you think it's not possible with CSS, you can probably do it with :has() now 😅 - it's unbelievably powerful... you can even count siblings/children and change layouts and stuff, it's wild! https://ishadeed.com/article/conditional-css-has-nth-last-child/#timeline (that goes to a pretty cool example within the article, but the entire thing is worth a read). Also, sorry for sort of going off topic, lol