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…


This content originally appeared on DEV Community and was authored by Tapajyoti Bose

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

Reach out to me on:


This content originally appeared on DEV Community and was authored by Tapajyoti Bose


Print Share Comment Cite Upload Translate Updates
APA

Tapajyoti Bose | Sciencx (2021-02-28T04:25:12+00:00) CSS decoded ::before and ::after. Retrieved from https://www.scien.cx/2021/02/28/css-decoded-before-and-after/

MLA
" » CSS decoded ::before and ::after." Tapajyoti Bose | Sciencx - Sunday February 28, 2021, https://www.scien.cx/2021/02/28/css-decoded-before-and-after/
HARVARD
Tapajyoti Bose | Sciencx Sunday February 28, 2021 » CSS decoded ::before and ::after., viewed ,<https://www.scien.cx/2021/02/28/css-decoded-before-and-after/>
VANCOUVER
Tapajyoti Bose | Sciencx - » CSS decoded ::before and ::after. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/28/css-decoded-before-and-after/
CHICAGO
" » CSS decoded ::before and ::after." Tapajyoti Bose | Sciencx - Accessed . https://www.scien.cx/2021/02/28/css-decoded-before-and-after/
IEEE
" » CSS decoded ::before and ::after." Tapajyoti Bose | Sciencx [Online]. Available: https://www.scien.cx/2021/02/28/css-decoded-before-and-after/. [Accessed: ]
rf:citation
» CSS decoded ::before and ::after | Tapajyoti Bose | Sciencx | https://www.scien.cx/2021/02/28/css-decoded-before-and-after/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.