Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Basic

CSS ::before Selector

Posted in CSS Tutorial
Updated on Oct 07, 2024
By Mari Selvan
๐Ÿ‘๏ธ 20 - Views
โณ 4 mins
๐Ÿ’ฌ 1 Comment
CSS ::before Selector

Photo Credit to CodeToFun

๐Ÿ™‹ Introduction

The ::before selector in CSS allows you to insert content before the content of an element using the content property.

It is a pseudo-element that is commonly used to add cosmetic content, such as icons, text, or symbols, without affecting the actual HTML structure.

This can be very helpful for styling purposes, especially when enhancing user interface elements.

๐Ÿ’ก Syntax

The signature of the ::before Selector is as follows:

Syntax
Copied
Copy To Clipboard
selector::before {
    content: "content-to-insert";
    /* Additional CSS properties */
}

The ::before selector must be combined with the content property, which defines what will be inserted before the elementโ€™s content.

๐Ÿ“ Example

Hereโ€™s a basic example demonstrating how to use the ::before pseudo-element to insert content.

โ˜ ๏ธ HTML

HTML
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS ::before Selector Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h1>Welcome to My Website</h1>
    <p>This is an example of the ::before pseudo-element.</p>
</body>
</html>

๐ŸŽจ CSS

CSS
Copied
Copy To Clipboard
/* Adding a decorative symbol before all <h1> elements */
h1::before {
  content: "โ˜… "; /* Unicode star symbol */
  color: gold;
  font-size: 1.5rem;
}

/* Adding text before the paragraph */
p::before {
  content: "Note: ";
  font-weight: bold;
  color: red;
}

In this example:

  • A gold star (โ˜…) is inserted before the content of all <h1> elements.
  • The word "Note:" is inserted before the content of the paragraph, styled in red and bold to draw attention.

๐Ÿ’ฌ Usage Tips

  • Use Unicode Characters: You can insert Unicode symbols or characters before elements for creative design, such as stars, checkmarks, or arrows.
  • Custom Styling: The content added using ::before can be styled with any CSS property, such as color, font-size, padding, or margin, just like normal elements.
  • Limitations: The ::before pseudo-element does not affect the documentโ€™s actual structure or accessibility. Screen readers might not always recognize content inserted with ::before unless appropriately labeled.

โš ๏ธ Common Pitfalls

  • Content is Required: If the content property is missing, the ::before pseudo-element will not render. Always define the content to insert, even if it's empty (content: "";).
  • Only for Block and Inline Elements: The ::before pseudo-element works on most block and inline elements but may not behave as expected on replaced elements like <img>, <input>, and <br>.
  • Browser Compatibility: Always check browser support when using pseudo-elements, though most modern browsers support ::before well.

Example: Adding Decorative Content to Links

You can use the ::before pseudo-element to add icons before links to visually distinguish them.

โ˜ ๏ธ HTML

HTML
Copied
Copy To Clipboard
<a href="https://example.com">Visit Example Website</a>

๐ŸŽจ CSS

CSS
Copied
Copy To Clipboard
a::before {
    content: "๐Ÿ”— "; /* Link icon */
    color: #555;
}

This example inserts a link icon before all anchor (<a>) elements.

๐ŸŽ‰ Conclusion

The ::before selector is a versatile pseudo-element that enables you to insert content before an element in a clean and structured way. It can be used to improve the design and readability of your web pages without affecting the HTML structure.

By mastering this pseudo-element, you can enhance your website's visual appeal and create more dynamic interfaces.

๐Ÿ‘จโ€๐Ÿ’ป Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
๐Ÿ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy