DEV Community

Cover image for CSS decoded ::before and ::after
Tapajyoti Bose
Tapajyoti Bose

Posted on • Edited on

59 17

CSS decoded ::before and ::after

What is a Pseudo Element in CSS?

A CSS Pseudo Element is a keyword added to a selector that lets you style a specific part of the selected element(s). For example, ::first-line can be used to style the first line of a paragraph, ::before can be used to prepend an element and style it as well, etc.

The main utility of the Pseudo Element is for cosmetic purpose only.

In CSS3 the Pseudo Elements are represented by ::<pseudo-element>, even though the CSS2 syntax :<pseudo-element> still works on most browsers for the Pseudo Elements that were available in CSS2.

What is the use of ::before and ::after?

In CSS, ::before and ::after creates pseudo-elements that is the prepended and appended to the selected element respectively.

How do I use ::before and ::after in my website?

One really important thing to keep in mind while using ::before and ::after is to set content property in the css

selector::before{
  /* without content the pseudo-elements will not work */
  content: "";
}
Enter fullscreen mode Exit fullscreen mode

After adding the content, you can style them just like any other element

Some simple examples of how ::before and ::after can bring you website to life are given below.

Quotes

Adding opening and closing quotes from CSS using ::before and ::after. The quote class can be used on any element to add quote-like styling.

Underline on Hover

CSS ::after is used to create an animated underline for links on hover (style prevalent in navlinks).

Info on Hover

CSS ::after is used to create animated info displaying on hover (similar to the popups in Hacker Earth and Leet Code).

Reference

MDN Web Docs

  1. Pseudo-elements
  2. ::before
  3. ::after

Thanks for reading

Need a Top Rated Software Development Freelancer to chop away your development woes? Contact me on Upwork

Want to see what I am working on? Check out my Personal Website and GitHub

Want to connect? Reach out to me on LinkedIn

Follow my blogs for bi-weekly new Tidbits on Medium

FAQ

These are a few commonly asked questions I get. So, I hope this FAQ section solves your issues.

  1. I am a beginner, how should I learn Front-End Web Dev?
    Look into the following articles:

    1. Front End Buzz words
    2. Front End Development Roadmap
    3. Front End Project Ideas
    4. Transition from a Beginner to an Intermediate Frontend Developer
  2. Would you mentor me?

    Sorry, I am already under a lot of workload and would not have the time to mentor anyone.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (10)

Collapse
 
afif profile image
Temani Afif

::before and ::after creates pseudo-elements that is the first and last child of the selected element respectively. --> The pseudo-element will not become "child" of selected element in a sense that we cannot select them using Child combinator .parent > * or Descendant combinator .parent *. also :last-child and :first-child don't apply to them.

To be more accurate, we should say "they behave as child" like said in the specification: w3.org/TR/css-pseudo-4/#generated-...

these pseudo-elements generate boxes as if they were immediate children of their originating element, with content as specified by content.

Collapse
 
ruppysuppy profile image
Tapajyoti Bose

Updated the wording of the phrase 👍

Collapse
 
ruppysuppy profile image
Tapajyoti Bose • Edited

Thanks for pointing it out. I intended to convey the same message, but it became a bit ambiguous. Updated the emphasis and example :)

Collapse
 
afif profile image
Temani Afif

You should probably quote your definition: developer.mozilla.org/en-US/docs/W...

Collapse
 
ruppysuppy profile image
Tapajyoti Bose

Thanks a lot! I am new to blogging and looking for every opportunity to improve :)

Collapse
 
sittisukintaruk profile image
Sitthisak Intharak

Good job man!

Collapse
 
ruppysuppy profile image
Tapajyoti Bose

Thanks a lot! :)

Collapse
 
urielbitton profile image
Uriel Bitton

good explanation!

Collapse
 
ruppysuppy profile image
Tapajyoti Bose

Thankyou!

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay