Sub topic 16
Practical Patterns and Common Pitfalls
This chapter assumes you know CSS rule syntax, type, class, ID and attribute selectors, and the descendant and child combinators. It also assumes you have met the pseudo-classes and pseudo-elements from earlier chapters. Here you combine them into patterns you can reuse, and learn the mistakes that cause most bugs.
Pattern: a form field that shows errors at the right time
A required field is :invalid the moment the page loads, because it is empty. Styling :invalid alone shows red borders before the user has typed anything. Add :not(:placeholder-shown) so the error appears only after typing. This needs a placeholder attribute, even a single space.
input:required:invalid:not(:placeholder-shown) {
border-color: #c00;
}
input:focus-visible {
outline: 3px solid #1a73e8;
outline-offset: 2px;
}
.form-group:focus-within {
background: #f0f6ff;
}The :focus-within rule highlights the whole group while any control inside it has focus.
Pattern: striped tables and tidy lists
Position selectors replace extra classes in the HTML.
tr:nth-child(even) {
background: #f5f5f5;
}
li {
border-bottom: 1px solid #ddd;
}
li:last-child {
border-bottom: 0;
}Pattern: cards that adapt to their content
:has() selects a parent based on what it contains. This card switches to a two-column layout only when it holds an image.
.card:has(img) {
display: grid;
grid-template-columns: 8rem 1fr;
}
.card::before {
content: "";
display: block;
height: 4px;
background: #1a73e8;
}The ::before bar is purely visual, so it carries no text.
Common pitfalls
| Pitfall | Why it happens | Fix |
|---|---|---|
| `:hover` rule never applies to a visited link | `a:visited` comes later with equal specificity | Order link rules as `:link`, `:visited`, `:hover`, `:active` |
| `:nth-child(2)` selects the wrong element | It counts all siblings, not only those of one type | Use `:nth-of-type()` when types are mixed |
| A `:is()` rule beats your override | `:is()` takes the specificity of its most specific argument | Use `:where()` for zero specificity |
| Errors show on empty fields | `:invalid` matches before any input | Add `:not(:placeholder-shown)` |
| Pseudo-element does not appear | `content` is missing | Set `content: ""` at minimum |
| Keyboard users see no focus | `outline: none` with no replacement | Style `:focus-visible` instead |
Accessibility reminders
- Never remove the focus outline without providing a clear replacement.
- Do not put essential information in
content. Support for screen readers varies, and users cannot select the text. - Do not signal state with color alone. Pair a red border with an icon or text message.
Exercise
A form has <input required placeholder=" ">, and you want a red border only after the user has typed an invalid value. Write the selector and explain why the placeholder attribute is needed.
Show answer
Use input:required:invalid:not(:placeholder-shown). The :placeholder-shown pseudo-class matches only while the placeholder is visible, which is when the field is empty. Without a placeholder attribute it never matches, so :not(:placeholder-shown) would always be true and the red border would show on the empty field.
Exercise
A list has three <li> items and one <p> between the first and second item. Why does li:nth-child(2) select nothing, and which selector selects the second <li>?
Show answer
:nth-child(2) counts every sibling, so the second child is the <p>, which is not an li. Use li:nth-of-type(2), which counts only li siblings.