Open In App

What is before and after in CSS?

Last Updated : 02 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The ::before and ::after pseudo-elements in CSS are used to insert content before or after the HTML element. They allow us to add content or elements without modifying the actual HTML structure. Those pseudo-elements are often used for styling purposes, such as adding icons, symbols, or any text that doesn't need to be part of the HTML.

Approach:

  • First, create a basic HTML layout for your needs.
  • Then you can create a container to show the::before and ::after pseudo-elements.
  • Use CSS for the HTML elements.
  • Then add contents using the selector.

Syntax for "::before":

selector::before {
content: "your content";
/* additional styles */
}

Syntax for"::after":

selector::after{
content: "your content";
/* additional styles */
}

Example: This example shows the example of ::before and ::after in CSS.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>Modern Before and After</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f4f8;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
        }

        .container {
            background-color: #ffffff;
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            width: 30%;
            max-width: 800px;
        }

        h2 {
            font-size: 24px;
            color: rgb(15, 235, 29);
            margin-bottom: 20px;
            text-align: center;
        }

        .example {
            margin-bottom: 40px;
            text-align: center;
        }

        .button {
            display: inline-block;
            padding: 15px 30px;
            background-color: #3498db;
            color: white;
            text-decoration: none;
            font-size: 18px;
            border-radius: 50px;
            transition: background-color 0.3s ease;
            position: relative;
            overflow: hidden;
        }

        .before-example .button::before {
            content: '✔';
            position: absolute;
            font-size: 16px;
            color: rgb(15, 235, 29);
            top: 50%;
            left: 9px;
            transform: translateY(-50%);
        }

        .after-example .button::after {
            content: ' ➜';
            margin-left: 10px;
            font-weight: bold;
        }

        .button:hover {
            background-color: #2980b9;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="example before-example">
            <h2>::Before Example</h2>
            <a href="#" class="button">Confirm</a>
        </div>

        <div class="example after-example">
            <h2>::After Example</h2>
            <a href="#" class="button">Continue</a>
        </div>
    </div>
</body>

</html>

Output:

Screenshot-_596_
Output

Next Article
Article Tags :

Similar Reads