Is it possible to select element based on its neighbour's child class ? (maybe :has???)

So i am wondering. Structure where I would like to select all div's based on its neighbour's child class but is seems that it does not work. Here is an example: https://codepen.io/H4ds0n/pen/WNYVEjX My goal is to make all div's background red if any of its neighbour's child has class selected
H4ds0n
CodePen
WNYVEjX
...
5 Replies
Mannix
Mannixβ€’16mo ago
you want all of them to be red from a to d?
interpod
interpodOPβ€’16mo ago
yup but the red color should be on div directly this is a little bit simplified example so it could make no sense but all i want is that if any of its neighbour's child has class "X" then do something with it
Mannix
Mannixβ€’16mo ago
try
body:has(.selected) div {
background-color: red;
}
body:has(.selected) div {
background-color: red;
}
interpod
interpodOPβ€’16mo ago
Perfect, seems that it is working. Thanks for the help
Chooβ™šπ•‚π•šπ•Ÿπ•˜
If you want to select only the siblings and not the element that has the .selected child:
div:has(.selected) ~ div {
background-color: red;
}
div:has(.selected) ~ div {
background-color: red;
}
Want results from more Discord servers?
Add your server