In this article, you will be introduced to pseudo-elements in the cascading style sheets and practical examples of how they are used along with the compatibility.
A CSS pseudo-element is a keyword added to a CSS selector that lets you style a specific part of the selected HTML element. In CSS3, they are usually denoted by two colons — for example, ::first-line — to differentiate them from pseudo-classes. In contrast, CSS2 syntax uses one colon (e.g., :first-line). I’ve indicated which pseudo-elements you can use with CSS2.
The general syntax for CSS pseudo-elements looks like this:
selector::pseudo-element {
property: value;
}
It looks just like normal styling statements but with the colons to indicate the specific part of the elements contained in the selector that you want to style.
There are currently seven pseudo-elements in CSS. They are:
There are others, but they are still considered experimental technology. So, in this post, the focus will be on the main seven pseudo-elements.
In this post, you will be introduced to each of these pseudo-elements in a simple demo folder. To follow along you will need:
A code editor — I recommend Visual Studio Code
A live server to serve your HTML code. You can download one in VS Code. First, go to the extensions tab. You will see a search bar alongside a list of favorite extensions. Search for “live server” (it has almost 5 million downloads)
Now, create a folder, call it pseudo, and open it with VS Code. Create two files:
index.html
: this is where our HTML code for content goesmain.css
: this is where the styling will take placeCopy this starter code below into your index.html
file:
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8">
<meta name=”viewport” content=”width=device-width, initial-scale=1.0">
<meta http-equiv=”X-UA-Compatible” content=”ie=edge”>
<title>Document</title>
<link rel=”stylesheet” type=”text/css” href=”main.css”>
</head>
<body>
<p>hello world!</p>
</body>
</html>
The code in bold shows that we have linked the content to a stylesheet — in your case, main.css
. Next, copy this test style into the main.css
file to test it out:
p{
color: aqua;
}
If you click the Go Live button at the footer of the VS Code application, you will be redirected to your default browser, where a live remote server hosting the webpage has been fired up for you.
The ::after
creates a pseudo-element that represents the last child of a selected HTML element. It is used to add styling to this particular element in collaboration with the CSS content property. The syntax looks something like this:
selector::after {
content: "value";
}
Copy the code below into your index.html
file:
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8">
<meta name=”viewport” content=”width=device-width, initial-scale=1.0">
<meta http-equiv=”X-UA-Compatible” content=”ie=edge”>
<title>Document</title>
<link rel=”stylesheet” type=”text/css” href=”main.css”>
</head>
<body>
<p class=”one”>This is my very first paragraph.
This is my very first paragraph.
This is my very first paragraph.
This is my very first paragraph.</p>
<p class=”three”>This is my second paragraph.</p>
<p class=”two”>This is my last but not the least paragraph</p>
</body>
</html>
Now, in your main.css
file, copy the code below:
.one::after {
content: “ — 1”;
color: blue;
}
.two::after {
content: “ — 2”;
color: red;
}
This simply adds the strings before the selected elements — in this case, the class pointing to various paragraphs — and updates their colors, too.
::before (:before)
The ::before
creates a pseudo-element that represents the first child of a selected HTML element. It is inline by default, and it is used to add styling to this particular element in collaboration with the CSS content property. The syntax looks something like this:
selector::before {
content: "value";
}
Your index.html
file will stay intact, but in your main.css
file, copy the code below:
.one::before {
content: “1--”;
color: blue;
}
.two::before {
content: “2--”;
color: red;
}
This simply adds the strings before the selected elements — in this case, the class pointing to various paragraphs — and updates their colors, too.
::first-letter (:first-letter)
The ::first-letter
creates a pseudo-element that represents the first letter of the first line of a block element as long it is not coming after an image or a table.
selector::first-letter {
property: value;
}
It is, however, important to realize that the first letter of an element can be tricky to spot. Things like punctuations can be logically counted as first letters. Some languages have digraphs that are always capitalized together, like the IJ
in Dutch. In these cases, both letters of the digraph should be matched by the ::first-letter
pseudo-element.
The ::before
pseudo-element in conjunction with the content property can create the first element, too, so keep these in mind while debugging your CSS.
Copy the styling code below in your main.css
file to see the ::first-letter
in action:
p::first-letter {
color: red;
font-size: 130%;
}
::first-line (:first-line)
The ::first-line
creates a pseudo-element that represents the first line of a block element. Just like the first letter, the first line is also dependent on a few factors, like the width of the element, the width of the document, and the font size of the element. The syntax of the ::first-line
pseudo-element looks like this:
selector::first-line {
property: value;
}
Copy the styles below into your main.css file:
p {
font-size: 130%;
}
p::first-line {
color: red;
font-size: 130%;
}
The first paragraph style will enlarge the font of the paragraphs in order to get a line out of the given string.
::marker
The ::marker
pseudo-element selects the marker box of a list item, which typically contains a bullet or number. It is used on list items and the summary element. The syntax looks like this:
selector ::marker {
property: value;
}
Add this list items code below to your index.html
file:
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
Copy the ::marker
styles below to the main.css
file to see it in action:
ul li::marker {
color: blue;
font-size: 130%;
}
::placeholder
The ::placeholder
pseudo-element points to the placeholder of input elements in your presentation, mostly found in forms. Most browsers, by default,
apply a light gray color to placeholders. The syntax looks something like this:
selector ::placeholder {
property: value;
}
Copy this input line into your index.html file:
<input placeholder="Type something here!">
Copy the styling below to the main.css file to see the changes:
input::placeholder {
color: red;
font-size: 1.2em;
font-style: italic;
}
::selection
The ::selection
pseudo-element applies to highlighted elements on the DOM. This is one of my favorite pseudo-elements. The syntax looks like this:
selector ::selection {
property: value;
}
There are a few properties that can be used on the ::selection
pseudo-element: color
, background-colo
r
, cursor
, caret-color
, outline
, text-decoration
, text-emphasis-color
, and text-shadow
.
Copy the styling below into your main.css
file:
p::selection {
background-color: red;
}
If you check it out in your browser, it will look like this:
Here is some useful information you might want to know about the pseudo-elements in regards to browser support:
Every popular browser except Safari and Opera fully support the ::after pseudo-element
Every popular browser except Safari, Internet Explorer 9, and Opera fully support the ::before pseudo-element
The ::marker pseudo-element
is supported only by Firefox
The ::first-letter pseudo-element
is supported by all browsers except Safari on iOS devices
The ::first-line pseudo-element
is fully supported by every browser on every screen size
The ::placeholder pseudo-element
is supported in all browsers except Internet Explorer
The ::selection pseudo-element
is supported by all web browsers
CSS is still an integral part of the modern web development process. It is vital that web developers take some time to learn the basics of CSS. Pseudo-elements in CSS will help you extend your knowledge and give you more exciting options for your styling. Happy hacking!
#css #html