Skip to main content

Command Palette

Search for a command to run...

:: before & ::after in CSS Explained

Updated
โ€ข3 min read
:: before & ::after in CSS Explained
I

Insha is a backend developer who specializes in building communities that drive shared value between developer-centric organizations and their users, aiming to enhance engagement, satisfaction, and retention.

Proficient in Python and well-versed in web technologies including HTML, CSS, JavaScript, Django and SQL, she brings a dynamic skill set to the table.

Hey Readers๐Ÿ‘‹

Today we are going to learn about ::before and ::after pseudo elements.

Often considered as one of those topics in CSS that scares almost every beginners. The main cause behind this is the lack of good examples. As a beginner I was too scared of these pseudo classes and it was like a nightmare to me. I curated few simple and easy to understand example in this article so that anyone can understand it better. Lets get started.

What actually are ::before and ::after ?

  • In CSS, these are known as pseudo elements.

A pseudo-element is used to style specified parts of an element.

For example, it can be used to:

  • Style the first letter, or line, of an element

  • Insert content before, or after, the content of an element

Both the ::before and ::after pseudo-elements create a child element inside an element only if you define a content property.

 .my-element::before {
    content: "";
}

.my-element::after {
    content: "";
}

The content can be any string โ€”even an empty one but be mindful that anything other than an empty string will likely be announced by a screen reader.

Once a ::before or ::after element has been created, you can style it however you want with no limits.

Lets know more about each of the two by looking at some examples.

::before

  • The ::before pseudo-element can be used to insert some content before the content of an element.
  • It is inline by default.

EXAMPLE : Check the output below ๐Ÿ‘‡

::after

  • The ::after pseudo-element can be used to insert some content after the content of an element.
  • It is inline by default.

EXAMPLE : Check the output below ๐Ÿ‘‡

  • Let's see some more examples to understand the use of ::before and ::after pseudo class better.

Example 1: Adding quotation marks

  • One simple example of using ::before pseudo-elements is to provide quotation marks. Here we use both ::before and ::after to insert quotation characters.

HTML ๐Ÿ‘‡

<p>
<q>Some quotes</q>, he said, <q>are better than none.</q>
</p>

CSS ๐Ÿ‘‡

q::before {
  content: "ยซ";
  color: blue;
}

q::after {
  content: "ยป";
  color: red;
}

Check the Output Below ๐Ÿ‘‡

Example 2 : Gradient Borders

HTML๐Ÿ‘‡

<div class = "gradient-border"</div>

CSS ๐Ÿ‘‡

.gradient-border {
width: 500px; 
height: 300px;
margin: 10px;
background-color: #111;
position: relative;
}

.gradient-border::before{
content: " " ;
position: absolute;
z-index: -1;
top: -20px;
left: -20px;
bottom: -20px;
right:-20px;
background-image: linear-gradient(#ff7a00, #ff1a00);
}

Check the Output below ๐Ÿ‘‡

The End

I hope you found this article valuable. If yes do let me know in the comments ๐Ÿ˜Š

Also if you got any questions feel free to ping me on Twitter

You can now extend your support by buying me a Coffee.๐Ÿ˜Š๐Ÿ‘‡

V

This is a very informative Article Insha, you made the topic easy to understand, I have not written the ::before & after in CSS because I am not used to UI and front-end but I will definitely look forward to using it when I need to as you explained it so well.

B

Nice article for beginners it would be nice if you write on linear gradient too

I

Thank you. I'll surely look upto your suggestion ๐Ÿ™Œ

V

Hi Insha, great article. Short and sweet, also well-structured and well-formatted. The only feedback I would give is the numberings on the "::before" and "::after" header are both 1.

I believe it should be 1. ::before then 2. ::after, so it makes sense (see image below).

image.png

image.png

Also, may be another small detail. You should use italic and bold mindfully, to emphasize certain points in your article. It can get too over-the-top if it's used in almost every sentence ๐Ÿ˜Š Overall, well done!

2
I

Thank you so much Victoria for the feedback. I'll keep these in mind while writing the next article. Surely will try to avoid the silly mistakes. ๐Ÿ˜Š๐Ÿ™Œ

More from this blog

Insha's Blog

17 posts

Sharing my bit of knowledge on Blockchain and Crypto space!