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

Susan Sarandon
Release: 2024-11-10 17:22:02
Original
902 people have browsed it

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 */ }
Copy after login

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>
Copy after login

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

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template