CSS ::before property
The ::before
pseudo-element in CSS is used to insert content before the content of an element. This pseudo-element allows you to add decorative content or stylistic elements before the actual content of the element, without modifying the HTML.
Syntax
selector::before {
content: "text or value";
/* Other styles */
}
selector
: The element to which you want to add content.::before
: The pseudo-element that inserts content before the content of the selected element.content
: Specifies what content to insert. This property is required for::before
to work.
How It Works
Basic Usage:
- The
content
property defines the content to be inserted. It can be text, images, or other values.
.example::before { content: "Note: "; color: red; }
- In this example, the text "Note: " will appear before the content of any element with the class
.example
.
- The
Inserting Images:
- You can use
::before
to insert images by setting thecontent
property tourl()
.
.icon::before { content: url('icon.png'); }
- This example inserts an image before the content of elements with the class
.icon
.
- You can use
Using with Other Properties:
- You can style the inserted content with CSS properties like
color
,font-size
,margin
, andpadding
.
.highlight::before { content: "[Important] "; color: orange; font-weight: bold; }
- This example adds "[Important] " before the content of elements with the class
.highlight
and styles it with orange color and bold text.
- You can style the inserted content with CSS properties like
Adding Decorative Content:
- You can use
::before
to add decorative elements such as icons or symbols.
.checkmark::before { content: "✔"; color: green; margin-right: 5px; }
- This example adds a checkmark symbol before the content of elements with the class
.checkmark
and styles it with green color and margin.
- You can use
Examples
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Before Pseudo-element Example</title>
<style>
.note::before {
content: "Note: ";
color: red;
}
.icon::before {
content: url('icon.png');
}
.highlight::before {
content: "[Important] ";
color: orange;
font-weight: bold;
}
.checkmark::before {
content: "✔";
color: green;
margin-right: 5px;
}
</style>
</head>
<body>
<p class="note">This is a note.</p>
<p class="icon">Icon content.</p>
<p class="highlight">This is highlighted.</p>
<p class="checkmark">Task completed.</p>
</body>
</html>
Explanation:
.note::before
:- Adds "Note: " before the content of
<p>
elements with the class.note
and styles it in red.
- Adds "Note: " before the content of
.icon::before
:- Inserts an image before the content of
<p>
elements with the class.icon
.
- Inserts an image before the content of
.highlight::before
:- Adds "[Important] " before the content of
<p>
elements with the class.highlight
and styles it with orange color and bold font.
- Adds "[Important] " before the content of
.checkmark::before
:- Inserts a green checkmark symbol before the content of
<p>
elements with the class.checkmark
and adds margin for spacing.
- Inserts a green checkmark symbol before the content of
Important Points
Content Property:
- The
content
property is required for::before
to work. Ifcontent
is not specified, the pseudo-element will not be rendered.
- The
Inline-Level Element:
- The
::before
pseudo-element is inline by default. You can change its display property toblock
orinline-block
if needed.
.example::before { content: "• "; display: inline-block; margin-right: 5px; }
- The
No Direct DOM Manipulation:
::before
does not modify the DOM. It only affects how content is rendered visually.
Combined with
::after
:::before
is often used in conjunction with::after
to add content both before and after the element's content.
.example::before { content: "Start - "; } .example::after { content: " - End"; }
- This example adds text both before and after the content of elements with the class
.example
.