contact@codebetter.in 8823075444
9993928766
  • Tutorials Home
  • Contact

What is the purpose of the CSS pseudo-elements?

CSS pseudo-elements are used to style specific parts of an HTML element, rather than the element itself. They allow you to apply styles to parts of an element's content or create elements that don't exist in the HTML document structure. Pseudo-elements are denoted by a double colon (::) and are often used to enhance the design and presentation of web pages. For Example, Styling the first letter or line of an element, and Inserting content before or after the content of an element. All of these can be done using Pseudo Elements in CSS.
  1. ::before
  2. ::after
  3. ::first-line
  4. ::first-letter
  5. ::selection
  6. ::marker
  1. ::before:
  2. This pseudo-element allows you to insert content before the content of an element. It's often used for decorative elements or to generate additional content.
    Example:
    HTML:
    <html>
    <head>
    <style>
    h3::before {
    content: "Hello ";
    color: red;
    font-size: 30px;
    }
    </style>
    </head>
    <body>
    <h3>World</h3>
    </body>
    </html>
    Output:

    World

  3. ::after
  4. This pseudo-element inserts content after the content of an element. It's commonly used for adding icons, decorative elements, or additional text.
    Example:
    HTML:
    <html>
    <head>
    <style>
    h2::after {
    content: " !";
    color: lightblue;
    font-size: 30px;
    }
    </style>
    </head>
    <body>
    <h2>World</h2>
    </body>
    </html>
    Output:

    World

  5. ::first-line:
  6. This pseudo-element selects and styles the first line of text within an element. It's often used for drop caps, changing the font size or color of the first line, or adding special styling.
    Example:
    <html>
    <head>
    <style>
    p::first-line {
    color: Red;
    font-weight: bold;
    }
    </style>
    </head>
    <body>
    <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quibusdam enim quo quis?Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quibusdam enim quo quis?Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quibusdam enim quo quis?Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quibusdam enim quo quis?</p>
    </body>
    </html>
    Output:

    Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quibusdam enim quo quis?Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quibusdam enim quo quis?Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quibusdam enim quo quis?Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quibusdam enim quo quis?

  7. ::first-letter:
  8. It selects and styles the first letter of a block-level element. It's commonly used for drop caps or ornamental initials.
    Example:
    <html>
    <head>
    <style>
    h1::first-letter {
    color: Red;
    }
    </style>
    </head>
    <body>
    <h1>CodeBetter</h1>
    </body>
    </html>
    Output:

    CodeBetter

  9. ::selection:
  10. applies styles to the part of a document that has been highlighted by the user such as clicking and dragging the mouse across the text.
    Example:
    <html>
    <head>
    <style>
    u::selection {
    background-color: Red;
    }
    </style>
    </head>
    <body>
    <i></i>
    </body>
    </html>
    Output: Lorem ipsum dolor sit amet consectetur adipisicing elit. Perspiciatis eius et ullam quos magni voluptate mollitia provident praesentium adipisci ipsum, quas eligendi ad numquam doloremque quasi sed debitis repudiandae sit, consectetur, ducimus possimus ipsam! nostrum laudantium amet voluptate impedit.
  11. ::marker:
  12. selects the marker box of a list item, which typically contains a bullet or number. It works on any element or pseudo-element set to display: list-item, such as the <li> and <summary> elements.
    Example: <html>
    <head>
    <style>
    ul li::marker {
    color: red;
    font-size: 15px;
    }
    </style>
    </head>
    <body>
    <ul>
    <li>HTML</li>
    <li>CSS</li>
    <li>BOOTSTRAP</li>
    </ul>
    </body>
    </html>
    Output:
    • HTML
    • CSS
    • BOOTSTRAP