1

For the below list, i want to style the parent list only like ( first parent ) i tried to use :

  ul < li {
    background-color: yellow;
  }

put this doesn't work , how can i do that ?

    <ul>
  <li>first parent <!-- style this -->
    <ul>
      <li>second</li>
      <li>second</li>
      <li>second</li>
      <li>second</li>
      <li>second</li>
    </ul>
  </li>
  <li>first parent</li> <!-- style this -->
  <li>first parent</li> <!-- style this -->
  <li>first parent</li> <!-- style this -->
  <li>first parent</li> <!-- style this -->
</ul>
osos
  • 2,103
  • 5
  • 28
  • 42

4 Answers4

2

You can't target a parent in CSS. You should just add a class to it and use it that way.

<li class="highlight">

-

.highlight {
    background-color: yellow;
}

Or, designate that specific li by it's position in its parent.

li:nth-child(5) { /* assuming it's the 5th <li> in that list */
    background-color: yellow;
}
casraf
  • 21,085
  • 9
  • 56
  • 91
2

Let's be clear here, just in case someone is finding this from a search engine: there are no parent selectors in CSS, not even in CSS3.

check this tutorial.

Here is the discuss about it: Is there a CSS parent selector?

Community
  • 1
  • 1
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
0

Hmm, you could use a class.

Or style both like this:

ul li { background-color: yellow; }
ul li ul li { background-color: none; }

Id probably use a class

cowls
  • 24,013
  • 8
  • 48
  • 78
0

you need to apply a class to identify your "first level" like this :

<ul class="firstLevel">
  <li>first parent <!-- style this -->
    <ul>
      <li>second</li>
      <li>second</li>
      <li>second</li>
      <li>second</li>
      <li>second</li>
    </ul>
  </li>
  <li>first parent</li> <!-- style this -->
  <li>first parent</li> <!-- style this -->
  <li>first parent</li> <!-- style this -->
  <li>first parent</li> <!-- style this -->
</ul>

Then apply css :

/* ">" means only first level childs "li" will be concerned */
ul.firstLevel > li { 
  background-color: yellow;
}
Pouki
  • 1,654
  • 12
  • 18