CSS is an essential tool for every web developer, but pseudo-elements can often be confusing to understand and implement. In this post we will explain what are these selectors, particularly ::before
and ::after
, how they work and how to use them. We’ll also present some useful practical examples and best practices to help you finally master CSS pseudo-elements.
A CSS pseudo-element is a keyword added to a selector that lets you style a specific part of the selected elements.
For example, it can be used to:
It’s important to note that pseudo-elements are not the same thing as pseudo-classes. This type of selector is out of the scope of this post, but pseudo-classes are used to target elements based on their specific states (:hover
, :active
, :focus
, :visited
, :not
are some examples of pseudo-classes).
The syntax is fairly simple if you are already familiar with CSS’s syntax. First, you use the selector, and in front of it, you write the pseudo-element to use; then, between brackets, like any other CSS, you write the styles to apply.
Since in this post, we will focus on ::before
and ::after
pseudo-elements, let’s see their syntax.
::before
and ::after
These two types of pseudo-elements behave in similar ways; the only difference is that ::before
adds content before the selector and ::after
adds content after the selector. Exactly as their names suggest.
In order to add content, we need to specify the content
property. If this property is not specified, has an invalid value, or has none
as value, then the pseudo-element will not be rendered; it behaves as if the property display: none;
is set.
Let’s create a simple example to demonstrate how to use content
property along with other CSS properties. The following snippet inserts the text "Before "
before and the text " After"
after a paragraph.
It renders Before - Mastering CSS Pseudo-Elements - After
, with the text Before in red and the text After in blue.
In this section, we will explore some examples of what you can build with the help of ::before
and ::after
CSS pseudo-elements. So, let’s dive in and see these tools in action.
When creating a list, the ::before
pseudo-element can be very useful for customizing the bullet point item to anything you want.
In this example, we are hiding the default bullet point and replacing it with the 👉 emoji using a ::before
pseudo-element selector.
If you intend to include quotes in your content, CSS pseudo-elements can help you add stylish quotation marks to enhance the visual appeal of your blockquotes.
Resorting to the ::before
CSS pseudo-element, in this example, we added and styled a quotation mark (before the <blockquote>
content) and a dash (before the <footer>
content).
Tooltips are an interactive way to present additional information when you move your cursor over an element. In this third example, we’ll improve the visuals of a tooltip-like element.
Here, the pseudo-element ::after
is used to create a square with just a top border showing (it creates the effect of a triangle pointing down) to improve the appearance of a tooltip that only appears when we move the cursor over.
The pseudo-elements generated by ::before
and ::after
are generated as if they were immediate children of the element on which they are applied; therefore, they cannot be applied to replacement elements, like <img>
or <iframe>
, whose content is outside the scope of the CSS formatting model.
Also, remember to specify a content
property; otherwise, the pseudo-element will behave as having a property display
set to none
.
Modern browsers, including Chrome, Firefox, Safari, and Edge, fully support CSS pseudo-elements like ::before
and ::after
with consistent rendering; on the other side, older browsers, or even some mobile browsers, may present some rendering issues or not render at all.
Ensure to test thoroughly if you are using pseudo-elements and need to support old browsers, like Internet Explorer.
When you use pseudo-elements to enhance the visual design of your websites, like we have seen above, it’s a good practice to make sure that your core content and functionality are accessible without them. It’s also recommended to provide fallback styling or even alternative designs when you need to support browsers that do not fully support CSS pseudo-elements.
To finish, if you have accessibility concerns, you should avoid using ::before
or ::after
pseudo-elements, as they may not be totally accessible to screen readers.
CSS pseudo-elements, like ::before
and ::after
, offer web developers powerful tools to enhance your site’s design without cluttering the HTML. Throughout this guide, we’ve explored their versatility in creating custom bullets for lists, styling blockquotes, adding visual effects like tooltips, and managing browser compatibility and limitations.
By understanding these two pseudo-elements, you can:
There are more pseudo-elements available, but ::before
and ::after
are the most versatile; with them, you can achieve pretty much any design you want.