CSS | Pseudo Elements

Pseudo Elements: Pseudo-element in CSS is used to add style in specified parts of an element. Example: Using style before or after an element.

Syntax:

filter_none

edit
close

play_arrow

link
brightness_4
code

selector::pseudo-element {
    property:value;
}

chevron_right


Use of Pseudo-Element: Below are some examples to describe the use of pseudo-element.



  • ::before Pseudo-element: It is used to add some CSS property before an element when that element is called.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
    <head>
        <title>before</title>
        <style>
        h1::before {
            content: "Before element - ";
        }
        </style>
    </head>
    <body>
        <h1>GFG</h1>
        <p>It is a paragraph.</p>
        <p>This is another paragraph.</p>
    </body>
    </html>

    chevron_right

    
    

    Output:

  • ::after Pseudo-element: It is used to add some CSS property after an element when that element is called.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
    <head>
        <title>after</title>
        <style>
        h1::after {
            content: " - after element";
        }
        </style>
    </head>
    <body>
        <h1>GFG</h1>
        <p>It is a paragraph.</p>
        <p>This is another paragraph.</p>
    </body>
    </html>

    chevron_right

    
    

    Output:

  • ::first-letter Pseudo-element: It is used to make changes to the first letter of an element.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
    <head>
        <title>after</title>
        <style>
        h1::first-letter {
            color: #ff0000;
        }
        </style>
    </head>
    <body>
        <h1>GFG</h1>
        <p>It is a paragraph.</p>
        <p>This is another paragraph.</p>
    </body>
    </html>

    chevron_right

    
    

    Output:

  • ::first-line Pseudo-element: It is used to make changes to the first line of an element.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
    <head>
        <title>after</title>
        <style>
        h1::first-line {
            color: #ff0000;
        }
        </style>
    </head>
    <body>
        <h1>GFG</h1>
        <p>It is a paragraph.</p>
        <p>This is another paragraph.</p>
    </body>
    </html>

    chevron_right

    
    

    Output:



My Personal Notes arrow_drop_up

I like to do coding in C++C and java programming languages HTML and CSS always intersts me Sharing knowleged is the best way according to me to increase ones knwoledge

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.