Have you ever tried to put a gradient on a border and found it just wouldn’t work with normal CSS? That’s exactly where the magic of CSS pseudo-elements ::before and ::after steps in. These pseudo-elements allow you to add creative styling effects that standard CSS properties simply can’t achieve on their own.
My name’s Rob, and here at PreCodeCamp, I’m excited to show you how to harness the power of these pseudo-elements to create visually stunning effects like gradients on borders, animated underlines, and interactive hover effects — all in just a few minutes!
Pseudo-elements like ::before
and ::after
let you insert content before or after an element’s actual content without modifying the HTML itself. They act as child elements that you can style with CSS, enabling you to add decorative features such as backgrounds, shapes, or animations.
For example, if you want to add a gradient border or a custom underline that changes on hover, these pseudo-elements give you the flexibility to do so without cluttering your HTML markup.
To start, you’ll typically have a simple HTML structure, like an <h1>
tag with some text inside. Your CSS file will be linked to the HTML so you can style the elements effectively.
For instance:
<h1>PreCodeCamp</h1>
Then in your CSS, you target this element and apply the pseudo-elements using the double colon syntax:
h1::before {
content: "Hello";
background-color: red;
}
This adds the word “Hello” before the original h1
content, with a red background for visibility.
While you can add text content with these pseudo-elements, their real power lies in styling. If you remove the text content but want to keep the styling — like a colored block or underline — you need to set up positioning properly.
Set the original element’s position to relative
so the pseudo-element can be positioned absolutely in relation to it.
Give the pseudo-element a height and width to make it visible.
Set the pseudo-element’s position to absolute
so you can place it precisely where you want.
For example, creating a small red block under the heading might look like this:
h1 {
position: relative;
width: max-content;
}
h1::after {
content: "";
position: absolute;
bottom: -5px;
left: 0;
width: 100%;
height: 4px;
background-color: red;
}
You can also combine pseudo-elements with CSS pseudo-classes like :hover
to add interactivity. For instance, you can make the underline disappear when hovering over the heading.
Here’s how you might do that:
h1::after {
transition: width 100ms ease-in-out;
}
h1:hover::after {
width: 0;
}
Hovering over the heading causes the underline to shrink smoothly, adding a subtle but stylish effect.
One of the coolest tricks is using these pseudo-elements to add gradients where normal CSS borders can’t. Since borders don’t support gradients directly, you can create a gradient bar with ::before
or ::after
and position it like a border.
This opens up a whole new world of design possibilities, allowing you to make your website stand out with unique visual effects that engage users.
Creativity: Add decorative elements without extra HTML.
Flexibility: Easily position and style elements for effects like underlines, borders, or overlays.
Interactivity: Combine with hover and other pseudo-classes for dynamic user experiences.
Performance: Keeps your HTML clean and semantic while enhancing visual appeal.
Always set the parent element’s position
to relative
when using absolute positioning on pseudo-elements.
Use content: "";
for styling-only pseudo-elements without adding text.
Experiment with sizes, colors, and positioning to get the exact effect you want.
Combine with transitions for smooth animations.
Pseudo-elements ::before
and ::after
are powerful tools in CSS that let you accomplish styling feats that are otherwise impossible with regular properties. From creating gradient borders and custom underlines to adding interactive hover effects, mastering these pseudo-elements can elevate your web design skills and make your projects truly pop.
Ready to dive deeper into CSS and web development? Check out more tutorials and lessons on our PreCodeCamp blog to continue your learning journey.
::before
inserts content immediately before the element’s content, while ::after
inserts content immediately after. Both are used for styling and can be positioned independently.
Yes, by setting the content
property to a string, you can add text. However, they are mostly used for decorative purposes rather than adding meaningful content.
Because pseudo-elements with position: absolute;
are positioned relative to the nearest positioned ancestor. Setting the parent to relative
ensures your pseudo-element positions correctly in relation to it.
Yes, ::before
and ::after
are widely supported across modern browsers, making them safe to use for most web projects.
Absolutely! You can animate properties like width, height, opacity, and colors on pseudo-elements to create engaging effects like hover animations or loading indicators.
Want to expand your CSS knowledge? Visit our CSS tutorial on creating search input fields with button effects or learn about the fundamentals of HTML, CSS, and JavaScript — the essential trio for web development.
Keep coding and stay creative!