Subscribe

CSS Pseudo-elements

✍️

Lets check out all the CSS pseudo-elements

25 Apr, 2020 · 4 min read

Yesterday we briefly touched on pseudo-elements when creating our custom checkbox

But let’s dive deeper into these awesome features of CSS. Pseudo-elements allow you to create/manipulate an original element. Without affecting that one.

They can be used to style a specific part of an element, like the first letter or add content before or after!

Pseudo-elements always start with ::!

First-line pseudo-element

With the first-line pseudo-element, we can manipulate the view of the first line of a specific element.

See the following example:

<p>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
  incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
  exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
  dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
  Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
  mollit anim id est laborum.
</p>
p::first-line {
  color: #ff0000;
  font-variant: small-caps;
}

This will result into the following:

See the Pen First-line pseudo-element by Chris Bongers (@rebelchris) on CodePen.

First-letter pseudo-element

Much like the above one this one is used to style a part of an element, but in this case only the first letter.

The html we can re-use

<p>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
  incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
  exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
  dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
  Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
  mollit anim id est laborum.
</p>
p::first-letter {
  color: teal;
  font-family: Verdana;
  font-size: 36px;
  display: inline-block;
  float: left;
  padding-right: 5px;
}

You will get this result:

See the Pen First-letter pseudo-element by Chris Bongers (@rebelchris) on CodePen.

Before pseudo-element

The before pseudo-element we saw in action yesterday when we created custom checkboxes. It can we used in many ways though!

<a href="https://daily-dev-tips.com">Daily Dev Tips</a>
<br />
<a href="https://twitter.com/DailyDevTips1">Twitter</a>
a::before {
  content: '⚓️';
}

So let’s say we want to add an anchor icon to every one of our links, it’s just as easy as this!

Check out this Codepen example:

See the Pen Before pseudo-element by Chris Bongers (@rebelchris) on CodePen.

Note: The before and after pseudo-elements always need content to render!

After pseudo-element

The after is basically used in the same way as the before but will place the content after the element.

<a href="https://daily-dev-tips.com">Daily Dev Tips</a>
<br />
<a href="https://twitter.com/DailyDevTips1">Twitter</a>
a::after {
  content: ' (Read more)';
}

This will generate the following:

See the Pen After pseudo-element by Chris Bongers (@rebelchris) on CodePen.

Selection pseudo-element

Another cool one is the selection pseudo-element. It will trigger when someone selects text.

<p>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
  incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
  exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
  dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
  Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
  mollit anim id est laborum.
</p>
p::selection {
  background: teal;
  color: #fff;
}

Now try and select a piece of this text; it should show a teal background with white text!

See the Pen Selection pseudo-element by Chris Bongers (@rebelchris) on CodePen.

CSS Pseudo-elements summary

To summarise, Pseudo-elements are used to modify a part of an element, always start with :: and we can use the following pseudo-elements:

  • ::first-line
  • ::first-letter
  • ::before
  • ::after
  • ::selection

Thank you for reading, and let’s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next 📖

Modifying an SVG path with CSS

10 Dec, 2022 · 2 min read

Modifying an SVG path with CSS

Animate an SVG path with CSS

9 Dec, 2022 · 2 min read

Animate an SVG path with CSS

Join 2097 devs and subscribe to my newsletter