I have a div block with a class "help-image" that contains an <img>, inside a section with a class "help-section".
Using the :has selector as follows works fine:
section.help-section > div:has(img),
section.help-section > div:has(figure),
section.help-section > div:has(button)
{
background: mistyrose;
}
However, passing a list of elements (as the MDN docs suggest I can) doesn’t work at all:
section.help-section > div:has(img, button, figure)
{
background: mistyrose;
}
I have inspected all elements with the developer tools, and the rule does not appear at all if I use the second example.
section.help-section {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
gap: 10px;
}
div.help-text,
div.help-image {
flex: 1 0 45%;
align-self: flex-start;
min-width: 350px;
}
section.help-section>div:has(img, figure, button) {
background: mistyrose;
/* doesn't work in my output */
}
<section class="help-section">
<div class="help-text">
<p>This is just an example paragraph, the help-text block will only ever contain text.</p>
</div>
<div class="help-image">
<img src="https://picsum.photos/200/200" />
</div>
</section>
2
Answers
I use a program called MadCap Flare for publishing help documentation - it appears that this is the culprit and is breaking the CSS rule for some reason:
Since the original question changed quite a bit, my answer is no
longer valid as I started on some wrong assumptions.
This answer is out of scope, but I’ll leave it in case some people
are trying to target a parent element. It can be interesting to
learn about the
:has()
and the:is()
new selectors.I quickly supposed that you wanted to set a pink background to all
the outer
<section class="help-section">
tag if one of the directchild
<div>
contained either an<img>
,<button>
or<figure>
tag. If that was the case, then you could have used the
:is()
CSS selector:
One could also use twice the
:has()
selector: