CSS course menu
CSS Pseudo-elements: ::before, ::after, ::first-letter, ::placeholder and ::selection
Learn CSS pseudo-elements with live examples: adding decorative content with ::before and ::after and the content property, styling the first letter and first line, ::placeholder, ::marker for list bullets, and ::selection.
On this page
A pseudo-element styles a specific part of an element, such as its first letter or the space just before its content, as if it were a separate element. Pseudo-elements use two colons.
::before and ::after#
::before and ::after insert a box at the start or end of an element's content. They only appear if you set the content property:
.price::before {
content: "₹";
color: #888;
}
.new::after {
content: "NEW";
margin-left: 6px;
padding: 1px 6px;
font-size: 11px;
background: crimson;
color: white;
border-radius: 4px;
}<p class="price">499</p>
<p class="new">Dark mode is here</p>content is usually a string. It can also be empty (content: ""), which is common for purely decorative shapes:
.title::after {
content: "";
display: block;
width: 60px;
height: 4px;
margin-top: 6px;
background: #2f5fe0;
border-radius: 2px;
}<h2 class="title">Our Courses</h2>::before and ::after do not work on void elements such as <img> and <input>, because those have no content box to insert into.
::first-letter and ::first-line#
.story::first-letter {
float: left;
font-size: 3em;
line-height: 1;
margin-right: 6px;
color: #8a3b12;
}
.story::first-line {
font-variant: small-caps;
}<p class="story">Once upon a time, in a small village by the river, there lived a potter who made the finest clay lamps in the whole valley. Every evening the lamps glowed in every window.</p>The "first line" changes as the window is resized, and the style follows it.
::placeholder#
input::placeholder {
color: #8a8a8a;
font-style: italic;
}<input placeholder="Search recipes" size="30">Keep enough contrast so placeholders stay readable.
::marker#
::marker styles the bullet or number of a list item:
li::marker {
color: crimson;
font-weight: bold;
}<ol>
<li>Preheat the oven</li>
<li>Mix the batter</li>
<li>Bake for 25 minutes</li>
</ol>Only a few properties work on ::marker, mainly colour, font properties and content.
::selection#
::selection styles text the user has highlighted:
p::selection {
background: #ffd34d;
color: black;
}<p>Select some of this text to see the custom highlight colour.</p>One colon or two?#
Pseudo-classes use one colon (:hover) and pseudo-elements use two (::before). Old CSS used one colon for both, so browsers still accept :before and :after, but write two colons in new code.
Practice#
Practice exercise
Add a check mark "✓ " before every item of a list with the class features, coloured green, and make the bullet of each item disappear by setting list-style: none on the list.
Show solution
.features { list-style: none; padding-left: 0; }
.features li::before {
content: "✓ ";
color: green;
font-weight: bold;
}<ul class="features">
<li>Free lessons</li>
<li>Live examples</li>
<li>Practice exercises</li>
</ul>Key takeaway#
Finished this lesson?