Sachin Jain
Sachin Jain

Reputation: 21842

Select all elements after specific element

I want to apply some specific styles to all elements which come after a certain element. For example, I want to select all elements which come after the divider and change their background color to green.

<ul>
  <li> Cricket </li>
  <li> Hockey </li>
  <li class='divider'> </li> //Divider here
  <li> Football </li>
  <li> Table Tennis </li>
</ul>

CSS code

li {
  background-color: red;
}

//Selector to select elements after divider {
  background-color: green;
}

Upvotes: 74

Views: 47682

Answers (1)

Zero Fiber
Zero Fiber

Reputation: 4465

You can use

.divider ~ li {
    background-color:green;
}

Upvotes: 126

Related Questions