I’m trying to target the first div block that wraps “Target”.
<div class="box-content">
<div>
<div>Target</div>
<div>xxxx</div>
<div>xxxx</div>
<div>xxxx</div>
<div>
<div>Unwanted</div>
<p>xxxx</p>
</div>
</div>
I tried;
div.box-content div div:nth-child(1) {
display:none;
}
and
div.box-content div div:first-child {
display:none;
}
but it removes the div that wraps “Unwanted” too. How can I target only the first one that wraps “Target”
You want to select the direct children, like:
Your original code selects any
div:first-child, who has adivas an ancestor (not a necessarily a direct parent), which also is an ancestor ofdiv.box-content… in other words, there’s no constraint to just yourTargetandUnwantedalso falls into this.The code above uses the
>selector, which saysdiv:first-childshould be styled only when it is a direct child of adiv, which is also a direct child ofdiv.box-content