#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS Pseudo-element

A pseudo-element lets you style a specific part of an element, or insert decorative content, without adding extra tags to your HTML. You write it with a double colon after a selector, such as p::first-line or .card::before. It is the tool to reach for when you want a decorative icon, a custom bullet, a styled first letter, or a coloured text selection.

Pseudo-elements versus pseudo-classes

The two are easy to mix up. A pseudo-class (single colon, like :hover) selects an element when it is in a certain state. A pseudo-element (double colon) selects a part of an element, or creates a virtual child that does not exist in the HTML. Browsers still accept the old single-colon spelling for the four oldest ones (:before, :after, :first-line, :first-letter), but the double colon is the modern standard and makes the difference obvious.

The ones you will use most

Pseudo-elementWhat it targets
::before / ::afterA generated child box inserted as the first or last child
::first-letterThe first letter of a block, often used for drop caps
::first-lineThe first rendered line of a block, which changes as the window resizes
::placeholderPlaceholder text inside an input or textarea
::selectionText the user has highlighted with the mouse
::markerThe bullet or number of a list item

Generating content with ::before and ::after

These two are the workhorses. They create an inline box inside the element, before or after its real content. The key rule: they only appear if you set the content property, even if it is an empty string.

/* A required-field star after labels */
label.required::after {
  content: " *";
  color: #c0392b;
}

/* A decorative underline bar under headings */
.section-title {
  position: relative;
  padding-bottom: 10px;
}
.section-title::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 48px;
  height: 3px;
  background: #4057d6;
}

/* Drop cap */
.article p:first-of-type::first-letter {
  float: left;
  font-size: 3em;
  line-height: 1;
  margin-right: 6px;
}

/* Custom highlight colour */
::selection {
  background: #ffe28a;
}

In the heading example, the parent gets position: relative so the absolutely positioned bar is placed relative to the heading, not the whole page.

Pitfalls with generated content

The most frequent reason a ::before does not show is a missing content line. The second is that the generated box is inline by default, so width and height are ignored until you make it display: block, inline-block or absolutely position it.

Also remember that ::before and ::after do not work on replaced or void elements such as img, input and br, because those have no content box to insert into. Wrap the image or input in a span or div and put the pseudo-element on the wrapper.

Finally, keep meaningful text out of content. Screen readers handle generated text inconsistently, it cannot be selected and copied, and search engines may ignore it. Use pseudo-elements for decoration and keep real information in the HTML.

Styling list markers

The ::marker pseudo-element lets you change the colour and size of bullets and numbers directly, for example li::marker { color: #4057d6; }. Only a limited set of properties, such as colour, font and content, apply to it, but for simple recolouring it is far easier than hiding bullets and drawing new ones.

Takeaway: use the double colon, always set content on ::before and ::after, change their display when you need a size, and keep them for decoration rather than real content.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course