CSS :not(:last-child):after Selector
Last Updated :
09 Sep, 2024
The CSS :not(:last-child): after selector is used to style elements, specifically adding content after each child element except the last one.
This selector is useful when we want to target elements that cannot be directly selected.
Syntax:
element :not(:last-child):after
Example 1: This example shows the uses of: not(:last-child): after selector with the help of an HTML document.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
outline: 1px solid;
margin: 10px;
box-shadow: 0 0 5px black;
background: green;
font-family: 'Segoe UI', sans-serif;
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
}
.inner-div {
width: 90%;
height: 45%;
background: white;
margin: 0 auto;
padding-left: 2px;
}
.div .inner-div:not(:last-child):after {
content: 'not in the bottom div';
}
</style>
</head>
<body>
<div class="div">
<div class="inner-div"></div>
<div class="inner-div"></div>
</div>
<div class="div">
<div class="inner-div"></div>
<div class="inner-div"></div>
</div>
<div class="div">
<div class="inner-div"></div>
<div class="inner-div"></div>
</div>
</body>
</html>
Output:

Example 2: This example creates a simple div element. It does not use: not(:last-child): after selector.
html
<!DOCTYPE html>
<html>
<head>
<title>CSS :not(:last-child):after Selector</title>
<style>
div {
width: 100px;
height: 100px;
outline: 1px solid;
margin: 10px;
box-shadow: 0 0 5px black;
background: green;
font-family: 'Segoe UI', sans-serif;
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
}
.inner-div {
width: 90%;
height: 45%;
background: white;
margin: 0 auto;
padding-left: 2px;
}
</style>
</head>
<body>
<div class="div">
<div class="inner-div"></div>
<div class="inner-div"></div>
</div>
<div class="div">
<div class="inner-div"></div>
<div class="inner-div"></div>
</div>
<div class="div">
<div class="inner-div"></div>
<div class="inner-div"></div>
</div>
</body>
</html>
Output:

Example 3: This example shows the use of inserting || beside the list of courses by using CSS :not(:last-child):after Selector.
html
<!DOCTYPE html>
<html>
<head>
<title>CSS :not(:last-child):after Selector</title>
<style>
body {
text-align: center;
}
h1 {
color: green;
}
li:not(:last-child):after {
content: ' || ';
font-weight: 700;
}
li {
display: inline;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h3>CSS :not(:last-child):after Selector</h3>
<div>GeeksforGeeks Subjects</div>
<ul>
<li>Data Structure</li>
<li>Algorithm</li>
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</ul>
</body>
Output:

Supported Browsers:
- Google Chrome 4
- Edge 12
- Firefox 3.5
- Opera 9
- Safari 3.1
CSS is the foundation of webpages, is used for webpage development by styling websites and web apps.You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.