Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip adding CSS rule to first li element using not(first)

Tags:

css

I am working on the code below. What am I not able to not add the CSS rule to the first item using the not option?

li:not(first) {
  color: red;
}
<ul>
  <li>Item 1</li>
  <li>Item 2
    <ul>
      <li>Item 2-1</li>
      <li>Item 2-2</li>
      <li>Item 2-3</li>
      <li>Item 2-4</li>
    </ul>
  </li>
  <li>Item 3</li>
  <li>Item 4</li>
</ul>
like image 490
Behseini Avatar asked Dec 15 '25 15:12

Behseini


2 Answers

You probably meant :not(:first-child) (there is no CSS first). There is :first but that's for printing:

The :first @page CSS pseudo-class describes the styling of the first page when printing a document.

li:not(:first-child){  color:red;  }
<ul>
        <li>Item 1</li>
        <li>Item 2
                <ul>
                    <li>Item 2-1</li>
                    <li>Item 2-2</li>
                    <li>Item 2-3</li>
                    <li>Item 2-4</li>
                </ul>
         </li>
        <li>Item 3</li>
        <li>Item 4</li>
</ul>

You could also do li:not(:first-of-type):

li:not(:first-of-type){  color:red;  }
<ul>
        <li>Item 1</li>
        <li>Item 2
                <ul>
                    <li>Item 2-1</li>
                    <li>Item 2-2</li>
                    <li>Item 2-3</li>
                    <li>Item 2-4</li>
                </ul>
         </li>
        <li>Item 3</li>
        <li>Item 4</li>
</ul>

Or li:not(:nth-child(1)):

li:not(:nth-child(1)){  color:red;  }
<ul>
        <li>Item 1</li>
        <li>Item 2
                <ul>
                    <li>Item 2-1</li>
                    <li>Item 2-2</li>
                    <li>Item 2-3</li>
                    <li>Item 2-4</li>
                </ul>
         </li>
        <li>Item 3</li>
        <li>Item 4</li>
</ul>

Or li:not(:nth-of-type(1)):

li:not(:nth-of-type(1)){  color:red;  }
<ul>
        <li>Item 1</li>
        <li>Item 2
                <ul>
                    <li>Item 2-1</li>
                    <li>Item 2-2</li>
                    <li>Item 2-3</li>
                    <li>Item 2-4</li>
                </ul>
         </li>
        <li>Item 3</li>
        <li>Item 4</li>
</ul>

By the way, if you're wondering why the inner list doesnt have its first element's color changed it because color is an inherited property.

like image 75
j08691 Avatar answered Dec 17 '25 06:12

j08691


You got the syntax wrong

 li:not(:first-child)
like image 32
Joshua Duxbury Avatar answered Dec 17 '25 07:12

Joshua Duxbury



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!