The CSS ::before
pseudo-element creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content
property. This allows for adding decorative elements, custom markers, or labels without altering the HTML markup, leading to cleaner and more maintainable code.
Example 1: Adding a "New!" Badge
.new-item::before {
content: "New!"; /* Text to be inserted */
background-color: #ff4500; /* Orange-red background */
color: white; /* White text color */
padding: 2px 6px;
border-radius: 3px;
font-size: 0.8em;
margin-right: 8px; /* Space between badge and item text */
}
Explanation This code snippet adds a small "New!" badge before any element with the class new-item
. It's a common technique for highlighting recent additions on a website, styled to be eye-catching.
Example 2: Custom Bullet Points
ul.custom-list li::before {
content: "\27A4"; /* Unicode for a right-pointing arrow */
color: #1e90ff; /* Dodger blue color */
font-weight: bold;
display: inline-block;
width: 1em;
margin-left: -1em; /* Positions the pseudo-element correctly */
}
Explanation This example replaces standard HTML list bullets with a custom arrow symbol. The content
property uses a Unicode character, and negative margin is applied to align it properly.
Example 3: Numbering with CSS Counters
body {
counter-reset: heading-counter; /* Initializes the counter */
}
h2::before {
counter-increment: heading-counter; /* Increments the counter for each h2 */
content: "Section " counter(heading-counter) ": "; /* Displays the counter value */
color: #555;
}
Explanation This demonstrates using CSS counters to automatically number headings. The ::before
pseudo-element displays "Section" followed by the incremented counter number, which is useful for articles or documentation.
Example 4: Adding an Icon to a Link
a.external-link::before {
content: "\1F517"; /* Unicode for a link icon */
font-family: "Segoe UI Symbol", sans-serif; /* Ensures icon displays */
margin-right: 5px;
text-decoration: none; /* Prevents underline on icon */
}
Explanation This code adds a link icon before any anchor tag with the class external-link
. It provides a visual cue to users that the link will take them to an external site.
Example 5: Decorative Separator
.separator::before {
content: "---";
color: #ccc;
letter-spacing: 0.5em; /* Spreads out the dashes */
display: block;
text-align: center;
margin-bottom: 15px;
}
Explanation Here, ::before
is used to create a simple, decorative separator. This is a lightweight alternative to using an image or a border for stylistic division between content blocks.
Example 6: Adding Quotes to a Blockquote
.custom-quote::before {
content: "\201C"; /* Unicode for a left double quotation mark */
font-size: 3em;
font-weight: bold;
color: #ddd;
float: left;
margin-top: -0.2em;
margin-right: 0.1em;
}
Explanation This CSS adds a large, decorative opening quotation mark to a blockquote element. It enhances the visual presentation of quoted text, making it stand out from the regular paragraph content.
Example 7: Breadcrumb Separators
.breadcrumb a::before {
content: "/";
padding: 0 10px;
color: #6c757d;
}
.breadcrumb a:first-child::before {
content: ""; /* Removes separator before the first item */
padding: 0;
}
Explanation This code inserts a slash separator before each link in a breadcrumb trail, except for the very first one. This is a classic, clean method for building navigation paths without extra HTML elements.