css
  1. css-before

CSS :before Selector

  • The :before selector in CSS is used to insert content before the content of a specified element.
  • It is a pseudo-element in CSS, and it is used to style a part of an element's content.

Syntax

Here is the syntax for using the :before selector in CSS:

selector::before {
  content: "some content";
  /* other styles */
}

In the content property, you can add any text or other content that you want to add before the specified element.

Example

Here is an example that uses the :before selector:

<!DOCTYPE html>
<html>
<head>
    <style>
        body::before {
            content: "Welcome to additional sheet";
            color: white;
            background-color: blue;
            padding: 10px;
            display: block;
        }
    </style>
</head>
<body>
    <p>This is some text.</p>
</body>
</html>
Try Playground

Explanation

The :before pseudo-element is used to add content to the beginning of an element. It inserts content that is specified in the content property. The content can be text, an image, or any other valid CSS content. The :before selector is often used to add icons or other decorative elements to web pages.

Use

The :before selector can be used for many purposes, such as:

  • Adding decorative elements to a web page
  • Creating custom bullets for lists
  • Adding icons to links or buttons
  • Adding a label or caption to an element

Important Points

  • The content property of the :before selector is required when using the ::before pseudo-element.
  • The ::before pseudo-element creates a new element that is added before the content of a specified element.
  • The content property can contain text, images, or other valid CSS content.
  • The :before selector can be used for many purposes, such as adding decorative elements, creating custom bullets, or adding icons to links or buttons.

Summary

In summary, the :before selector in CSS is a powerful tool for adding content to the beginning of an HTML element. It can be used for many purposes, such as adding decorative elements, creating custom bullets, or adding icons to links or buttons. The content property is required when using the ::before pseudo-element, and it can contain text, images, or other valid CSS content.

Published on: