I have HTML Like this
<element1 class="class__1">
<element2 removePadding></element2>
<element3 class="class-3"></element3>
</element>
I want to change the margin-bottom of class-3 only when element2 has removePadding
attribute, I tried like this
element1 element2[removePadding] element1 element3.class-3 {
margin-top: 20px;
}
but this didn’t work.
I want to target that class-3
only when element2 has removePadding
attribute
3
Answers
element1 element2[removePadding] element1
– The lastelement1
shouldn’t be there, CSS selectors only can go down, not up. Furthermoreelement3
is not child ofelement2
but is a sibling so you could use the+
(next-sibling) selectorTry using a combination of the sibling selector with the class-3 selector:
As the other comment mentioned, CSS selectors can only go down/next and not up/previous.
You can use
:has
pseudo-class and the child combinator (>) to achieve the wanted result!