Home  >  Article  >  Web Front-end  >  How to Style All Children Except the Last Child in CSS?

How to Style All Children Except the Last Child in CSS?

Susan Sarandon
Susan SarandonOriginal
2024-11-10 17:22:02916browse

How to Style All Children Except the Last Child in CSS?

Using CSS to Select Children Except the Last Child

In web development, specifically using CSS, there may be instances where you want to apply styles to all children of an element except for the last child. This can be achieved using CSS Selectors Level 3's negation pseudo-class.

Syntax:

:not(:last-child) { /* styles */ }

How it Works:

The :not() pseudo-class allows you to exclude elements that match a specified selector. In this case, we use it to negate the :last-child pseudo-class, which selects the last child element.

Example:

Consider the following HTML structure:

<div>
  <div>First child</div>
  <div>Second child</div>
  <div>Third child</div>
</div>

To style all children except the last child, you can use the following CSS:

:not(:last-child) {
  background-color: blue;
}

This will apply a blue background color to the first two children, but not the third child.

Compatibility:

It's worth noting that the :not() pseudo-class is not supported in Internet Explorer 8 or earlier browsers.

The above is the detailed content of How to Style All Children Except the Last Child in CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn