Full answer
A pseudo-class selects an element that is in a certain state or position. It is written with one colon: :hover, :focus, :checked, :first-child, :nth-child(2n), :not(.hidden). The element exists in the HTML already. The pseudo-class just adds a condition.
A pseudo-element selects a part of an element, or creates a virtual box. It is written with two colons: ::before, ::after, ::first-line, ::placeholder, ::marker, ::selection. The ::before and ::after boxes are not in the HTML, so they must have a content property to appear.
The label gets a red star after its text, the first and third list items get a grey stripe, and the last item is bold. A rule of thumb: pseudo-class means "when the element is in this state", pseudo-element means "this part of the element".
Content added with ::before or ::after is not part of the DOM. You cannot select it with JavaScript, and its text may or may not be read by a screen reader depending on the reader, so keep decorative text only.
<style>
label.required::after { content: " *"; color: crimson; }
li:nth-child(odd) { background: #f2f2f2; }
li:last-child { font-weight: bold; }
input::placeholder { color: #888; }
input:focus-visible { outline: 3px solid #0a6cff; }
input:invalid:not(:placeholder-shown) { border-color: crimson; }
</style>
<label class="required" for="name" id="lab">Name</label>
<input id="name" required placeholder="Your name">
<ul>
<li id="l1">Maths</li><li id="l2">Physics</li><li id="l3">Chemistry</li>
</ul>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Pseudo-classes use one colon and describe state or position.
- Pseudo-elements use two colons and style a part of an element.
- ::before and ::after need content to render.
- Generated content is not selectable by scripts and should not carry important text.
- :focus-visible shows focus rings mainly for keyboard users.
Common mistakes
- forgetting content on ::before or ::after. Without the property, even content: "", the box is not created.
- putting important text in ::after. It cannot be selected, copied reliably, or translated by the page content tools.
Follow-ups
- why do old stylesheets show ':before' with one colon?
- What does :not() do to specificity?
- How would you add a required star to a label using CSS only?
Tips
- Use :focus-visible instead of :focus for custom outlines, so mouse users do not see a ring on every click while keyboard users still do.
- Give two quick examples of each in the same breath. Examples settle the definition faster than words.
- Mention :focus-visible when talking about states; it signals accessibility awareness.