If I don't know the parent container elements, how can I create a CSS selector that targets only the top-level element(s) without referencing any elements above it? For example, consider the following:
<...>
<div class="foo">
foo1
<div class="foo">foo2</div>
<span class="foo">foo3</div>
<div class="bar">
bar
<div class="foo">foo4</div>
</div>
<div>
</...>
How could one write a CSS selector to target only the outermost "foo" element and not any of the nested ones within it?
I initially attempted .foo:root
, but :root does not function in that manner. Neither would .foo:first-child
or .foo:first-of-type
work, as I am unable to select exclusively from children of a specific element. Are there any other viable solutions?