I have a "self-similar" (or recursive) HTML structure where I need to build a CSS Selector that catches only the top matches in this structure. For instance, given the following HTML:
<div class="option-block">
<div class="option">
<p>main content</p>
</div>
<div class="detail">
<div class="option-block">
<div class="option">
<p>child content</p>
</div>
<div class="detail">
<p>child detail</p>
</div>
</div>
</div>
</div>
I need a CSS Selector that gives me the first div
within the top option-block
, IOW, the div
that contains the text main content
.
I've tried with .option-block > div:first-child
(and several variants like it), but that gives me also the div
with the text 'child content'.
One thing to note is that this particular structure could be anywhere within the HTML document, so I don't have a prior reference to use as anchor (like they do here or here).
Another thing to note is that this structure could be self-replicating to more levels: the .detail
component of an .option-block
container could always have another .option-block
container inside, so I cannot rely on counting or anything like that.
UPDATE: thanks for the answers so far. Something that I should have mentioned is that the selector is to be used programmatically to locate the items, not to apply styles. Also, the selector is to be applied from the context of an existing item, which may be already inside a this hierarchy.