Consider this snippet:
.wrapper {
display: flex;
gap: 5px;
border: 1px solid black;
border-radius: 3px;
width: 80px;
padding: 5px;
}
<div class="wrapper">
<div class="icon">🖼️</div>
<div class="text">paintings</div>
</div>
The icon may or may not be present.
What I’m trying to achieve is that, if the icon is not present, some left padding is added to .text
.
How do I do that? I tried a few combinations of :has
, :not
and +
(the next-sibling combinator), to no avail.
Essentially, I want to select .text
, but only if it is not preceded by .icon
.
2
Answers
.wrapper:not(:has(div.icon)) div.text
should do it.
Example 1 (with icon)
Example 2 (without icon)
.text:not(.icon + .text)
should do the trick