How to select the last element with a specific class, not the last child inside of parent with CSS?

Spread the love

Sometimes, we want to select the last element with a specific class, not the last child inside of parent with CSS.

In this article, we’ll look at how to select the last element with a specific class, not the last child inside of parent with CSS.

How to select the last element with a specific class, not the last child inside of parent with CSS?

To select the last element with a specific class, not the last child inside of parent with CSS, we’ve to select the last element with the last element with the specific class.

For instance, we write

<div class="page one">
  <div class="page-container">
    <p class="comment">Something</p>
    <p class="comment">Something</p>
  </div>

  <div class="page-container">
    <p class="comment">Something</p>
    <p class="comment">Something</p>
  </div>
</div>

to add nested elements.

Then we write

.page.one .page-container .comment:last-child {
  color: red;
}

to selkect the last child of the div with class page-container.

And then we select the last element with class comment inside the div with .comment:last-child.

Conclusion

To select the last element with a specific class, not the last child inside of parent with CSS, we’ve to select the last element with the last element with the specific class.

Leave a Reply

Your email address will not be published.