Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style each list item in unordered list without inline styling?

Tags:

html

css

I have this unordered list:

<div id="topmenudiv">
  <ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
  </ul>
</div>

How can I access each li tag for styling each one separately with CSS without using inline styling?

like image 906
user2175784 Avatar asked Feb 09 '23 12:02

user2175784


1 Answers

If you want to style each list item differently, you can use the nth-child selector:

/* First item */
li:nth-child(1) {
  color: red;
}

/* Second item */
li:nth-child(2) {
  color: blue;
}


/* Add additional items */
<div id="topmenudiv">
  <ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
</ul>
</div>
like image 173
Steve Sanders Avatar answered Feb 12 '23 03:02

Steve Sanders