I have a situation similar to this:
On first page of a website
<div class="div1">
<div class="child"> </div>
</div>
<div class = "div2">
</div>
On some other pages:
<div class="div1">
<div class="child"> </div>
<div class="child"> </div>
<div class="child"> </div>
</div>
<div class = "div2">
</div>
Basically, I want to hide div2 only for a div that has only one child.
I can use only CSS.
Is it possible to target only that specific one child div and hide "div2" using only CSS?
Thanks
What I recall is that I tried doing something similar in the past for div that had 6 children.
(changing width)
/* six items */
div.answer:first-child:nth-last-child(6),
div.answer:first-child:nth-last-child(6) ~ div {
width: calc(50% - 14px) !important;
}
The trick was that we influence the first child and all of its siblings when the first child was also the sixth child from the end.
2
Answers
you can use :has() Pseudo Class for this effect.
For more study about :has pseudo class, you can use this link.
As of today there is
Firefox still doesn’t natively supports :has() by default. (not recommended)
For this to actually work, you will have to modify your HTML a little.
All you will have to do is to make the .div2 a child of .div1 and then this css should do the trick:
It selects a .div2 class which is a sibling of .child class but not is a sibling of .child class which is a sibling of another .child class.
This simplifies to selecting a .div2 class with only one .child class.
Code snippet: