liveBook · Manning

Pseudo-element

As explained in Tiny CSS Projects

A CSS pseudo-element is a selector feature that targets a specific conceptual part of an element.

A CSS pseudo-element selects a conceptual part of an element rather than the whole element. Write two colons after the element selector: `p::first-letter` targets a paragraph’s first letter. This differs from a pseudo-class such as `:hover`, which targets an element in a particular state. section 1.6.3

Accent a first letter

To give an opening paragraph a small visual accent, use `header + p::first-letter { color: crimson; font-style: italic; }`. The selector limits the effect to the first paragraph immediately after the header; the pseudo-element limits it to that paragraph’s first letter. section 1.6.3

Add punctuation to a checkout summary

In a description list, `dt::after { content: ": "; }` places a colon after each description term. The `::after` pseudo-element supplies the position, and `content` supplies the character. section 8.2.5

Pseudo-elements also reach parts of browser-rendered controls. A progress indicator can be styled with `::-moz-progress-bar` in Firefox or `::-webkit-progress-value` in Chrome. These experimental selectors are kept in separate, otherwise identical rules: combining them in one rule breaks the Chrome styling in this example. Check browser behavior when using prefixed pseudo-elements. section 3.6.3

A pseudo-element counts as a type selector when CSS calculates specificity, with a value of 1. Not every pseudo-element works on every HTML element, so choose one that fits the part you need to style. section 1.5 section 1.6.3

How Tiny CSS Projects covers it

How deep it goes
Explains selector syntax through CSS examples and a browser-specific workaround, while noting that pseudo-elements do not work on every HTML element.
Examples
Shows a crimson italic first letter, a colon after each description term, and matching progress-indicator styles for Firefox and Chrome.
What it assumes
Introduces pseudo-elements alongside pseudo-classes using HTML paragraphs and links, then applies them to a description list.
What you can do after
Practice/perform targeted CSS styling by accenting a paragraph’s first letter or adding punctuation after description terms.
How it connects
Connects pseudo-elements to pseudo-classes, CSS specificity, the content property, and browser-specific progress styling.

Also explained in

Compare how 2 books explain pseudo-element →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny CSS Projects. The section links point to the parts of the book it draws on.