Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CSS selector ignore nested children

I have a nested set of list where I need to select only the first node of children and ignore anything below it.

<ul class="parent">
    <li class="node">
        <a>A link</a>
    </li>
    <li class="node">
        <a>A link</a>
        <ul class="sub-child">
            <li class="node">
                <a>A link</a>
            </li>
        </ul>
    </li>
    <li class="node">
        <a>A link</a>
    </li>
<ul>

In this case I want to style the links <a> but only ones directly under parent>node. What does the selector for this look like in CSS?
I tried .parent .node a but that will also apply to the link under sub-child>node

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

> means directly under the element

.parent>.node>a {
  background: red;
}

.parent>.node>.sub-child>li>a {
  background: blue;
}
<ul class="parent">
  <li class="node">
    <a>A link</a>
  </li>
  <li class="node">
    <a>A link</a>
    <ul class="sub-child">
      <li class="node">
        <a>A link</a>
      </li>
    </ul>
  </li>
  <li class="node">
    <a>A link</a>
  </li>
  <ul>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading