Home > Article > Web Front-end > 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!