Pseudo-elements and generated boxes
Definition and purpose
Pseudo-elements let CSS address a part or generated box associated with an element. Double colons distinguish modern pseudo-element syntax from pseudo-classes. ::before and ::after can create generated inline content when their content value is not none or normal.
Decorative marker
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style>.note::before { content: "• "; color: navy; }</style> </head> <body> <p id="a" class="note">Meeting confirmed</p> <p id="b">Other text</p> </body></html>The note receives a decorative bullet; the message stays in HTML.
Syntax and mental model
p::before { content: "• "; color: navy; } generates a decorative marker before paragraph content. ::after appears after the content. ::first-letter and ::first-line address typographic parts of eligible text, with limited applicable properties; the first line can change with width. ::marker styles a list item marker, and ::selection styles selected text within its supported property set. These are not ordinary HTML elements; generated content does not insert a DOM text node.
Typography and list markers
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> p::first-letter { color: navy; font-size: 24px; } li::marker { color: green; } </style> </head> <body> <p id="a">Read this paragraph.</p> <ul> <li id="b">Bring a notebook</li> </ul> </body></html>The first letter and list marker receive distinct styles without rewriting the text.
Common mistakes
Keep essential labels, warnings and instructions in HTML; generated content is not a reliable substitute for semantic text or accessible names. If a generated marker is missing, check the content property and whether the pseudo-element applies.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Interaction and link states. Next lesson: Origins and competing rules.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Pseudo-elements and generated boxes — knowledge check
Five questions on pseudo-elements and generated boxes.
Which syntax targets generated content before a note?
Which property supplies the generated marker?
Does ::before insert an ordinary DOM text node?
Which styles a list item marker?
Where should an essential warning message live?