Delve into the world of HTML and CSS with this comprehensive tutorial, covering topics such as pseudo-elements, the content property, and using Chrome’s DevTools, and learn to add content before or after an element with CSS.
This exercise is excerpted from Noble Desktop’s past front-end web development training materials and is compatible with updates through 2022. To learn current skills in web development, check out our coding bootcamps in NYC and live online.
Topics covered in this HTML & CSS tutorial:
Using pseudo-elements, The content property, Seeing pseudo-elements in Chrome’s DevTools
Exercise Preview
Exercise Overview
Usually content is added using HTML, but in this exercise you’ll learn how it can be helpful to use CSS to add content before or after an element.
Getting Started
- We’ll be switching to a new folder of prepared files for this exercise. In your code editor, close all open files to avoid confusion.
- For this exercise we’ll be working with the Tahoe Pseudo-Elements folder located in Desktop > Class Files > Advanced HTML CSS Class. Open that folder in your code editor if it allows you to (like Visual Studio Code does).
- Open index.html from the Tahoe Pseudo-Elements folder.
-
Preview index.html in Chrome (we’ll be using its DevTools).
This page is similar to the previous exercise, but notice the links at the bottom of each column are on their own line. This helps them to stand out, and gives us space to add some cool styling. We want to add a special character to all three of the Read More links, which we can do with CSS!
Leave the page open in Chrome.
Pseudo-Elements & the Content Property
- First let’s figure out how to target these links. Go to index.html in your code editor.
- Find the Read More link at the bottom of the first section.
- Notice it has a read-more class we can use to style it.
- Open main.css from the css folder (in the Tahoe Pseudo-Elements folder).
-
Above the .book-now-btn rule add the following new rule
.read-more::before { }
The ::before creates a pseudo-element, which is a virtual (fake) element that will render something into the anchor tag before everything else that’s already in it. It’s typically used to add cosmetic content to an element, as we’d like to do here.
-
Specify the content to add by adding the code shown below in bold. Pay close attention to spaces, and to type » use Opt–Shift–
\
(Mac) or hold down Alt while typing code 0187 on the numeric keypad (Windows)..read-more::before { content: "» "; }
- Save the file and reload the page in Chrome.
- Check out the Read More links to see they now have the special » character added at the beginning of the link.
- Return to your code editor.
-
Change before to after:
.read-more::after { content: "» "; }
-
Move the space from before the » character to after it:
.read-more::after { content: " »"; }
Save the file and reload the page in Chrome. Now the » character should be at the end of the link.
Seeing Pseudo-Elements in Chrome’s DevTools
- Ctrl–click (Mac) or Right–click (Windows) on a Read More link’s » character and choose Inspect.
- In the DevTools Elements panel you’ll see the HTML code. Expand the
<a href="#" class="read-more">
element to see the ::after inside. - Click on the ::after and you’ll see the style that creates it listed in the Styles panel.
Single vs. Double Colons (Pseudo-Element vs. Pseudo-Class)
Some pseudo-elements (such as :before and :after) can be written with one or two colons (:after or ::after). Why? Initially they were written with one colon, but the W3C wanted to distinguish pseudo-elements from pseudo-classes (such as :hover, :first-of-type, and :last-child) so they changed pseudo-elements to a double colon syntax.
Because the single colon syntax had already been used, it’s acceptable for backward compatibility but is not allowed for new pseudo-elements.
What’s the difference between a pseudo-class and pseudo-element?
Pseudo-Class:
- Styles an element when it’s in a certain state.
- Preceded by one colon.
- Examples are :hover, :first-of-type, :last-child, :not, :checked
Pseudo-Element:
- Styles a specific part of an element.
- Preceded by two colons.
- Examples are ::after, ::before, ::first-letter, ::first-line
We’ve only covered some pseudo-classes and pseudo-elements. You can learn more at tinyurl.com/pseudo-ce