HTML & CSS Foundations · 16 min · 120 XP
Write and target CSS rules
Connect CSS to a page, write a rule, and target classes, descendants and interaction states without overspecifying.
A CSS rule has a selector choosing elements and a declaration block saying what changes: .notice { color: #17324d; }. Each declaration is a property-value pair. Put practice CSS in a <style> element; in a real project, link an external file from the head with <link rel="stylesheet" href="styles.css"> so pages can share it.
.notice { padding: 1rem; } /* any element with class="notice" */
.notice strong { color: #8a2b06; } /* strong anywhere inside a notice */
.notice > h2 { margin-top: 0; } /* h2 that is a direct child */
.notice a:hover { text-decoration-thickness: 0.2em; }
.notice a:focus-visible { outline: 0.2rem solid currentColor; }Element selectors (p) affect every matching element; class selectors (.summary) express a reusable role; id selectors (#checkout) are so strong that they make later overrides difficult. Use classes for components and keep selectors short. A space means inside, while > means direct child.
Pseudo-classes describe state. :hover means a pointing device is over an element; :focus-visible means keyboard focus needs a visible indicator; :checked, :disabled and :required describe form state. Never put essential information behind hover alone. Touchscreens and keyboards do not have the same hover interaction.
Loading your workspace…