Home 〉 CSS Tutorials 〉 CSS Pseudo-Elements Explained with Examples | Beginner-Friendly Guide
CSS Pseudo-Elements Explained with Examples | Beginner-Friendly Guide !
Need to style part of an element's content? CSS pseudo-elements like `::before`, `::after`, and `::first-line` let you insert and style content without touching the HTML. This is helpful for adding icons, styling the first letter, or custom bullets.
CSS pseudo-elements let you style specific parts of an element—like the first letter of a paragraph or adding content before or after an element—without extra HTML. Let's go through each pseudo-element with simple examples.
In this Tutorial, we are going to cover the following CSS 'Pseudo-Elemenets':
A pseudo-element starts with '::' and is used to style parts of elements that are not normally targetable with regular selectors.
Syntax ✍
selector::pseudo-element {
property: value;
}
Adds content before the element's actual content.
<!DOCTYPE html>
<html>
<head>
<style>
.para::before {
content: "Note: ";
color: red;
}
</style>
</head>
<body>
<p class="para">This is a paragraph.</p>
</body>
</html>
output 📌
This is a paragraph.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Result: Displays “Note:” in red before the paragraph text.
Adds content after the element's actual content.
<!DOCTYPE html>
<html>
<head>
<style>
.para1::after {
content: " --->";
}
</style>
</head>
<body>
<p class="para1">This is a paragraph.</p>
</body>
</html>
output 📌
This is a paragraph.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
<!DOCTYPE html>
<html>
<head>
<style>
.para2::first-letter {
font-size: 2em;
color: green;
}
</style>
</head>
<body>
<p class="para2">This is a paragraph.</p>
</body>
</html>
output 📌
This is a paragraph.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Stylize the beginning of a paragraph like a storybook.
<!DOCTYPE html>
<html>
<head>
<style>
.para3::first-line {
font-weight: bold;
}
</style>
</head>
<body>
<p class="para3">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Obcaecati, fuga! Dolores labore repellendus, ex impedit expedita quae officiis. Harum, sunt dolore recusandae nam natus sapiente? Error assumenda necessitatibus voluptatem odio!</p>
</body>
</html>
output 📌
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Obcaecati, fuga! Dolores labore repellendus, ex impedit expedita quae officiis. Harum, sunt dolore recusandae nam natus sapiente? Error assumenda necessitatibus voluptatem odio!
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
<!DOCTYPE html>
<html>
<head>
<style>
.para4::selection {
background: yellow;
color: black;
}
</style>
</head>
<body>
<p class="para4">This is a paragraph.</p>
<p class="para5">This is a paragraph.</p>
</body>
</html>
output 📌
This is a paragraph.
This is a paragraph.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Targets the placeholder text in form input fields.
<!DOCTYPE html>
<html>
<head>
<style>
input::placeholder {
color: red;
}
</style>
</head>
<body>
<input type="text" placeholder="Enter Name">
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Targets the bullet or number in a list. Refer our post on HTML List Tags for more details
<!DOCTYPE html>
<html>
<head>
<style>
.list-item::marker {
color: blue;
}
</style>
</head>
<body>
<ul>
<li class="list-item">List Item1</li>
<li class="list-item">List Item2</li>
<li class="list-item">List Item3</li>
</ul>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
CSS pseudo-elements give you fine-grained control over styling without changing the HTML structure. They are perfect for enhancing design while keeping your HTML clean.
Practice using '::before', '::after', and others to create visually appealing effects on your site.
Pseudo-elements use '::' and style parts of elements (like '::before'), while pseudo-classes use ':' and style based on state or position (like ':hover').
Yes! Example:
p.note::before {
content: " ";
}
Most modern browsers support all major pseudo-elements. '::marker' and '::placeholder' may have minor differences in older versions.