CSS ::before Selector - A Beginner's Guide

CSS ::before Selector - A Beginner's Guide

  • CSS
  • 3 mins read

Introduction

The CSS ::before selector is used to add content before an HTML element. It is a pseudo-element that allows you to insert content before the content of an element. This can be useful for adding decorative elements, such as icons or labels, to your webpage without having to add extra HTML code. In this guide, you will learn how to use the ::before selector to add visual enhancements to your webpage, including examples and explanations of how to use it effectively.

Using the CSS ::before Selector

To use the ::before selector, you need to first select the element you want to add content before. You can do this by using a CSS class or ID. For example, if you want to add a label before an element with the class "example", you would use the following code:

.example::before {
  content: "Label: ";
  color: blue;
}

In this example, the content "Label: " will be added before the element with the class "example" and the text color will be set to blue.

You can also use the ::before selector to add an image before an element. To do this, you need to set the content property to the URL of the image and set the display property to "inline-block". Here's an example:

.example::before {
  content: url("example.png");
  display: inline-block;
}

In this example, the image "example.png" will be added before the element with the class "example".

CSS ::before Selector Examples:

Here are some creative examples of using the ::before selector:

1. Adding a bullet point before a list item:

li::before {
  content: "\2605"; /* the star character */
  color: black;
  margin-right: 10px;
}

Output:

See the Pen css before ex-1 by Vinish Kapoor (@foxinfotech) on CodePen.

2. Adding a quotation mark before a blockquote:

blockquote::before {
  content: "\"";
  font-size: 50px;
  color: blue;
}

Output:

See the Pen css before ex-2 by Vinish Kapoor (@foxinfotech) on CodePen.

3. Adding a unicode character before a heading element:

h1::before {
    content: "\1F4D6"; /* this is the unicode for a book */
    font-size: 20px; /* adjust the size of the icon */
    color: gray; /* adjust the color of the icon */
    margin-right: 10px; /* add some space between the icon and the heading text */
}

Output:

CSS before selector example for H1 tag.

Conclusion

The CSS ::before selector is a powerful tool for adding content before an HTML element. It allows you to add decorative elements, such as labels or icons, without having to add extra HTML code. With the examples and explanations provided in this tutorial, you should be able to start using the ::before selector to enhance the design of your web pages.

FAQ

Can I use the ::before selector on any HTML element?

The ::before selector can be used on any HTML element, but it is commonly used on block-level elements such as <p>, <div>, <blockquote>, etc.

Can I use more than one ::before selector on the same HTML element?

Yes, you can use multiple ::before selectors on the same HTML element, but it is not recommended as it can make the code difficult to read and maintain.

Can I use the ::before selector to add dynamic content?

Yes, you can use JavaScript or any other programming language to add dynamic content to the ::before selector.