The Wayback Machine - https://web.archive.org/web/20241001032352/https://www.geeksforgeeks.org/css-notlast-childafter-selector/
Open In App

CSS :not(:last-child):after Selector

Last Updated : 09 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.



Similar Reads

Last-child and last-of-type selector in SASS
SASS is also called Syntactically Awesome Stylesheet. It is a programming language that is interpreted into CSS. The Last Child selector is a selector that allows the user to target the last element inside the containing element. This selector is also known as a Structural Pseudo class which means it is used for styling content on the basis of pare
2 min read
CSS :last-child Selector
The CSS :last-child selector targets the last child element within its parent. It applies styles exclusively to the last child element based on its position in the parent container, regardless of the element type or class. Syntax: :last-child { //property}Example: In this example we applies limegreen background, white color, italic font-style, and
2 min read
CSS :nth-last-child() Selector
The nth-last-child() selector in CSS is used for selecting elements based on their position among sibling elements, but starting the count from the end rather than the beginning. This selector is particularly useful for styling elements dynamically based on their order in reverse, allowing for flexible and responsive design solutions. Understanding
3 min read
How to use a not:first-child selector in CSS?
This selector is used to select every element which is not the first-child of its parent element. It is represented as an argument in the form of :not(first-child) element. Syntax: :not( element ) { // CSS property } Example: <!DOCTYPE html> <html> <head> <title>not first child selector</title> <style> h1 { color
1 min read
jQuery :nth-last-child() Selector
The jQuery :nth-last-child() selector is used to select all elements that are the nth last child of their parent. The counting of elements starts from the last element. Syntax: :nth-last-child( n | even | odd | formula )Parameters: The :nth-last-child() selector contain parameters which are listed below: n: It holds the child index number of the se
2 min read
jQuery :last-child Selector
It is a jQuery Selector used to select every element that is the last child of its parent. Syntax: $(":last-child")Example 1: Here is the basic example of a last-child selector. C/C++ Code <!DOCTYPE html> <html> <head> <title>jQuery :last-child Selector</title> <script src= "https://ajax.googleapis.com/ajax/lib
1 min read
Difference between :first-child and :first-of-type selector in CSS
The :first-child and :first-of-type selectors in CSS are used to target specific child elements within a parent. The :first-child selector targets an element if it is the first child of its parent, regardless of type. On the other hand, the :first-of-type selector targets the first occurrence of a specified element type within its parent. Understan
2 min read
CSS :nth-child() Selector
The :nth-child() CSS pseudo-class selector is useful for targeting elements based on their position among siblings. This selector can match elements by specific numeric positions, odd/even positions, or complex functional notations like An+B. Understanding and utilizing :nth-child() can enhance web design by allowing for precise and dynamic styling
3 min read
CSS :only-child Selector
The:only-child selector in CSS is used to match every element that is the only child of its parent. It represents an element without any siblings.  Syntax: :only-child { // CSS property} Example 1:  [GFGTABS] html <!DOCTYPE html> <html> <head> <title>:only-child selector</title> <style> h1 { color: green; text-al
2 min read
CSS First Child Selector
CSS :first-child selector is used to select the elements that are the first child of the parent element. Syntax: :first-child { /* CSS property */ }Example 1: This example shows the use of the CSS first child selector. [GFGTABS] html <!DOCTYPE html> <html> <head> <title>first child selector</title> <style> h1 { c
2 min read
How to add space (" ") after an element using :after selector in CSS ?
The :after selector in CSS is used to add the same content multiple times after the content of other elements. It inserts something after the content of each selected element. Syntax: :after { // CSS Property}Example 1: This example uses :after selector to add space after an element. C/C++ Code <!-- HTML code to add space after the selected elem
1 min read
jQuery :only-child Selector
It is a jQuery Selector used to select every element that is the only child of it's parent i.e only a single child is selected. Syntax: $(":only-child")Example 1: In this example, we are using only-child selector. C/C++ Code <!DOCTYPE html> <html> <head> <title>jQuery :only-child Selector</title> <script src= "
1 min read
jQuery parent > child Selector
It is a jQuery Selector used to select all elements that are the direct child of its parent element. Syntax: ("parent > child")Parameter Values: parent: Using this, the parent element will be selected.child: Using this, the direct child element of the specified parent element will be selected.Example 1: C/C++ Code <!DOCTYPE html> <html
1 min read
jQuery :nth-child() Selector
The jQuery :nth-child() Selector Selects all elements that are the nth-child of their parent elements. Syntax: $("Selector:nth-child( index | even |odd |equation )")Index: Index provided. Index starts fromeven: even number of child elements get selected.odd: odd number of child elements will get selected.equation: select child elements by solving a
1 min read
How to create a combined child selector in SASS ?
Introduction: Lets first talk about the various types of Combinators available in CSS. Combinator: A combinator is something that explains the relationship between the selectors. A CSS selector can contain more than one simple selector. Between the simple selectors, we can include a combinator. There are four different combinators in CSS:Descendant
2 min read
jQuery :first-child Selector
The :first-child selector allows you to select the first child of its parent element which makes manipulating DOM elements efficient. In this article, we will explore the jQuery :first-child selector, how to use it, and provide practical examples. What is the jQuery :first-child Selector?The :first-child selector in jQuery is used to target and man
2 min read
How to select all children of an element except the last child using CSS?
When designing and developing web applications, sometimes we need to select all the child elements of an element except the last element. To select all the children of an element except the last child, use :not and :last-child pseudo classes. Syntax: element:not(:last-child) { // CSS Style } Example 1: This example creates a navigation menu separat
2 min read
How to apply CSS in last child of parent using jQuery ?
In this article, we will see how to set styles in the last child of the parent element using jQuery. To set the style on the last child of parent, we use jQuery :last-child selector. The :last-child selector is used to select every element that is the last child of its parent element. Syntax: $("Selector:last-child") Approach: First, we create an H
1 min read
How to select "last child" with a specific class using CSS ?
In this article, we will learn to select the "last-child" with a specific class name in CSS. The last-child selector allows us to select the last element inside a set of siblings' elements within its defining element. the last-child selector is a pseudo-class that chooses the final member of the siblings in the block that contains it. Approach: The
3 min read
CSS :last-of-type Selector
The :last-of-type Selector is used to target the last child element of the same type of it's parent for styling. This selector is same as ":nth-last-of-type". Syntax: :last-of-type { //property}Example: [GFGTABS] HTML <!DOCTYPE html> <html> <head> <style> p:last-of-type { background: limegreen; color: white; font-style: ital
2 min read
CSS :nth-last-of-type() Selector
The:nth-last-of-type() Selector in CSS is used to style only those elements which are the nth number of child of the parent element. The selector counts from the last-child element. A n could be a number, a keyword, or a formula. Syntax: :nth-last-of-type(number) { //css Property;} Where number is the argument that represents the pattern for matchi
3 min read
How content property is used with :after selector in CSS ?
In this article, we will learn how content property is used with the :after selector in CSS. The :after selector is used to insert something after any selected element. And the content property is used to specify the content that will be inserted. So we use the content property in ::after selector to put something after the selected element. Approa
1 min read
CSS ::after Selector
::after selector is used to add the same content multiple times after the content of other elements. This selector is the same as ::before selector. Syntax: ::after{ content:}Below HTMl/CSS code shows the functionality of ::after selector : [GFGTABS] HTML <!DOCTYPE html> <html> <head> <style> p::after { content: " - Rem
2 min read
How to use a "not equal" CSS attribute selector ?
Cascading Style Sheets(CSS) fondly referred to as CSS, is a simply designed language intended to simplify the process of making web pages presentable. CSS allows you to apply styles to web pages. More importantly, CSS enables you to do this independent of the HTML that makes up each web page. CSS is easy to learn and understand, but it provides pow
2 min read
CSS :not Selector
The :not(selector) selector is used to style every element that is not specified by the given selector. Known as the negation pseudo-class, it allows developers to exclude specific items from being selected. Syntax: :not(selector) { //CSS Property}Usage ExamplesExample 1This example demonstrates the usage of the :not selector to style all elements
2 min read
jQuery :last Selector
The jQuery :last Selector is used to select the last element of the specified type. Syntax: $(":last")Example 1: In this example, we will select the last <p> element by using jQuery :last Selector. C/C++ Code <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
1 min read
jQuery :last-of-type Selector
The jQuery :last-of-type Selector is used to select all elements which are the last child of their parent. Syntax: $(":last-of-type") Below are examples that illustrate the :last-of-type selector in jQuery: Example 1: This example changes the background-color to green and text-color to white, of the last element of their parents (div tags). C/C++ C
2 min read
jQuery :nth-last-of-type() Selector
The jQuery :nth-last-of-type() Selector is used to select all child of a parent with the same element name, counting from the last to first. It uses 1-indexed counting. Syntax: element:nth-last-of-type(n|even|odd|formula)Parameters: n: It selects nth element from last.even: It selects every even element from last.odd: It selects every odd element f
2 min read
jQuery :not() Selector
The jQuery :not() selector is used to select all the elements which are not selected. Syntax: $(":not(selector)")Parameter: selector: Used to specify that the element is not to be selected. The selector parameter accepts any kind of selector.Example 1: Change the background color of the p element. C/C++ Code <!DOCTYPE html> <html> <h
1 min read
Not class selector in jQuery
Given a list of elements and the task is to not select a particular class using JQuery. jQuery :not() Selector: This selector selects all elements except the specified element. Syntax: $(":not(selector)") Parameters: It contains single parameter selector which is required. It specifies the element which do not select. This parameter accepts all kin
3 min read