WEB-CSS-001
Style rules
A style rule connects a selector to declarations. A declaration needs a property, a colon and a valid value.
Build with DEX · 49 reference entries
Shape readable layouts with selectors, spacing, flexbox, grid, responsive type and accessible interaction states.
Start with the content and the available space. These examples cover practical CSS rules without importing legacy browser hacks or resetting useful accessibility defaults.
Choose an entry or use Find on page to look for a term. Each number identifies an educational reference; production checks need their own recorded result.
WEB-CSS-001
A style rule connects a selector to declarations. A declaration needs a property, a colon and a valid value.
WEB-CSS-002
Use classes for reusable styling. An id identifies one element and has higher selector specificity than a class.
.notice { padding: 1rem; }
#main-content { scroll-margin-top: 5rem; }Documentation: CSS documentation
WEB-CSS-003
Use descendant, child or sibling combinators to express the actual element relationship instead of relying on long brittle selector chains.
.card > h2 { margin-block-start: 0; }
label + input { display: block; }Documentation: CSS documentation
WEB-CSS-004
Target a meaningful attribute or value when it describes the state you want to style.
input[type="email"] { max-inline-size: 30rem; }
[aria-invalid="true"] { border-color: #a50020; }Documentation: CSS documentation
WEB-CSS-005
A pseudo-class selects a state or relationship. A pseudo-element styles a particular part; generated decoration should not carry essential instructions.
a:hover { text-decoration-thickness: 2px; }
p::first-letter { font-weight: 700; }Documentation: CSS documentation
WEB-CSS-006
Keep a visible focus indicator on links and controls. Focus-visible lets the browser decide when a focus ring is particularly useful.
button:focus-visible, a:focus-visible { outline: 3px solid currentColor; outline-offset: 4px; }Documentation: CSS documentation
WEB-CSS-007
The cascade resolves competing declarations; inheritance passes selected properties from a parent. Inspect computed styles when a result is unexpected.
body { color: #222; font-family: system-ui, sans-serif; }
button { font: inherit; }Documentation: CSS documentation
WEB-CSS-008
Keep selectors modest so later changes remain predictable. Where is useful when a grouping should add no specificity of its own.
:where(article, aside) a { text-underline-offset: .2em; }Documentation: CSS documentation
WEB-CSS-009
Use named layers to define an intentional order between groups of rules. Unlayered normal declarations still take precedence over normal layered rules.
@layer reset, base, components;
@layer components { .card { padding: 1rem; } }Documentation: CSS documentation
WEB-CSS-010
Store reusable design values in custom properties. A fallback handles a missing value at the point of use.
:root { --space: 1rem; --accent: #9b1234; }
.card { padding: var(--space, 1rem); }Documentation: CSS documentation
WEB-CSS-011
A shorthand can reset related longhand values that you did not write. Check the computed result before replacing a carefully configured property group.
.panel { background-color: #fff; background-image: none; }Documentation: CSS documentation
WEB-CSS-012
Border-box includes padding and borders inside the declared width, which helps prevent unexpected overflow.
*, *::before, *::after { box-sizing: border-box; }Documentation: CSS documentation
WEB-CSS-013
Understand normal flow before adding positioning. A block fills its available inline space; inline content flows with text.
.badge { display: inline-block; }
.stack { display: block; }Documentation: CSS documentation
WEB-CSS-014
Use logical spacing when the layout should follow the document's writing direction. Gap belongs to a layout container; margin belongs to an item.
.card { padding-block: 1rem; padding-inline: 1.25rem; }
.card + .card { margin-block-start: 1rem; }Documentation: CSS documentation
WEB-CSS-015
Use a border to separate regions without relying only on a shadow. Match corner radius to the site's existing visual system.
.card { border: 1px solid #767676; border-radius: .75rem; }Documentation: CSS documentation
WEB-CSS-016
Backgrounds are decorative. Put meaningful images in HTML so they can have appropriate text alternatives.
.banner { background: linear-gradient(135deg, #fff4f6, #ffffff); }Documentation: CSS documentation
WEB-CSS-017
Choose readable foreground and background pairs. Opacity affects the entire element and its descendants; an alpha colour affects that colour alone.
.notice { color: #222; background-color: rgb(255 240 244 / .9); }Documentation: CSS documentation
WEB-CSS-018
Check the actual foreground and background combination, including hover and disabled designs. WCAG AA generally needs 4.5:1 for ordinary text.
/* Verify this pair with a contrast checker. */
.copy { color: #222; background: #fff; }Documentation: CSS documentation
WEB-CSS-019
An outline can mark focus without changing layout dimensions. A shadow provides visual depth but should not be the only state indicator.
.control:focus-visible { outline: 3px solid #111; outline-offset: 3px; }
.card { box-shadow: 0 2px 8px rgb(0 0 0 / .08); }Documentation: CSS documentation
WEB-CSS-020
Flexbox arranges items along one main axis. Allow wrapping when a row of controls must fit a smaller screen.
.actions { display: flex; flex-wrap: wrap; gap: .75rem; align-items: center; }Documentation: CSS documentation
WEB-CSS-021
Use flex-basis, grow and shrink deliberately. A flexible text region often needs min-inline-size zero to shrink beside another item.
.summary { flex: 1 1 18rem; min-inline-size: 0; }
.icon { flex: 0 0 auto; }Documentation: CSS documentation
WEB-CSS-022
Grid controls rows and columns. Use minmax with a zero minimum when long content must not force a fractional track wider.
.layout { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); gap: 1.5rem; }Documentation: CSS documentation
WEB-CSS-023
Let available width determine the number of columns while keeping each card within the container.
.cards { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr)); }Documentation: CSS documentation
WEB-CSS-024
Use justify-content along the relevant main axis and align-items on the cross axis. Their visual direction depends on the layout mode.
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 1rem; }Documentation: CSS documentation
WEB-CSS-025
Keep the reading and keyboard order meaningful in HTML. Visual reordering with order or grid placement does not rewrite the document's logical order.
/* Put items in their intended reading order in HTML. */
.steps { display: flex; flex-direction: column; }Documentation: CSS documentation
WEB-CSS-026
Sticky positioning needs an inset and is affected by scrolling ancestors. Test that a sticky region leaves enough space for the page content.
.section-nav { position: sticky; inset-block-start: 0; }Documentation: CSS documentation
WEB-CSS-027
A larger z-index only competes within its stacking context. Inspect ancestors with transforms, opacity or positioning before raising a number.
WEB-CSS-028
Keep long values and code samples inside their component. Allow scrolling where preserving formatting is necessary.
pre { max-inline-size: 100%; overflow-x: auto; }
.long-url { overflow-wrap: anywhere; }Documentation: CSS documentation
WEB-CSS-029
Combine a readable maximum width with flexible outer space. Avoid a fixed page width that exceeds a narrow viewport.
.content { inline-size: min(70rem, calc(100% - 2rem)); margin-inline: auto; }Documentation: CSS documentation
WEB-CSS-030
Reserve a media region with aspect-ratio. Contain preserves the whole image; cover fills the box and can crop it.
.product-image { aspect-ratio: 4 / 3; inline-size: 100%; object-fit: contain; }Documentation: CSS documentation
WEB-CSS-031
Provide a sensible fallback stack. Use font inheritance for controls so forms remain visually consistent with the surrounding text.
body { font-family: system-ui, -apple-system, "Segoe UI", sans-serif; }
input, button { font: inherit; }Documentation: CSS documentation
WEB-CSS-032
Use a unitless line-height so it scales with text. Keep long paragraphs within a comfortable measure.
article { max-inline-size: 70ch; line-height: 1.6; }Documentation: CSS documentation
WEB-CSS-033
Clamp sets a minimum, a preferred fluid value and a maximum. Check browser zoom and text enlargement after choosing the range.
h1 { font-size: clamp(2rem, 1.5rem + 2vw, 3.5rem); }Documentation: CSS documentation
WEB-CSS-034
Allow descriptions and translated labels to wrap. Use truncation only when the full content remains available through another clear route.
.description { white-space: normal; overflow-wrap: anywhere; }Documentation: CSS documentation
WEB-CSS-035
Rem follows the root font size; em follows the relevant font-size context. Viewport units track the viewport, so test mobile browser controls.
.panel { padding: 1rem; }
.label { letter-spacing: .02em; }Documentation: CSS documentation
WEB-CSS-036
Choose breakpoints where the content needs a layout change. A CSS number and its unit belong together without a space.
@media (max-width: 48rem) { .layout { grid-template-columns: 1fr; } }Documentation: CSS documentation
WEB-CSS-037
A container query adapts a component to its available container space. Establish containment on the parent before querying its inline size.
.card-shell { container-type: inline-size; }
@container (min-width: 30rem) { .card-content { display: grid; grid-template-columns: 1fr 1fr; } }Documentation: CSS documentation
WEB-CSS-038
A colour-scheme preference is a starting point for a complete theme. Check borders, controls, images and every text state together.
@media (prefers-color-scheme: dark) { :root { color-scheme: dark; } }Documentation: CSS documentation
WEB-CSS-039
Use semantic borders and system colours where needed so controls remain recognisable when a user overrides the palette.
@media (forced-colors: active) { .card { border: 1px solid CanvasText; } }Documentation: CSS documentation
WEB-CSS-040
Use hover and pointer queries for optional refinements. Keep every essential action usable without hover.
.button { min-block-size: 2.75rem; }
@media (hover: hover) { .button:hover { text-decoration: underline; } }Documentation: CSS documentation
WEB-CSS-041
Respect a request for less motion. Target the moving component and retain a useful final state.
.notice { transition: opacity .2s ease; }
@media (prefers-reduced-motion: reduce) { .notice { transition: none; } }Documentation: CSS documentation
WEB-CSS-042
Name the properties that should animate instead of transitioning every change. Prefer short feedback that does not delay an action.
.button { transition: background-color .15s ease, color .15s ease; }Documentation: CSS documentation
WEB-CSS-043
Use keyframes for a defined sequence. Provide a reduced-motion treatment and avoid flashing or automatically moving essential reading content.
@keyframes reveal { from { opacity: 0; } to { opacity: 1; } }
.notice { animation: reveal .2s ease-out; }Documentation: CSS documentation
WEB-CSS-044
Transforms change visual presentation without moving surrounding flow content. Test overlap and hit targets when scaling or translating controls.
.icon { transform: rotate(90deg); transform-origin: center; }Documentation: CSS documentation
WEB-CSS-045
Remove navigation and decorative backgrounds from a printed lesson while keeping its content, links and code readable.
@media print { nav, .print-hide { display: none; } pre { white-space: pre-wrap; } }Documentation: CSS documentation
WEB-CSS-046
Display none removes a box; visibility hidden keeps its layout space. Opacity zero alone can leave an element interactive.
WEB-CSS-047
Reset only the browser defaults the design needs to change. Retain useful list markers, form affordances and focus indicators.
body { margin: 0; }
img { max-inline-size: 100%; block-size: auto; }
button, input, select, textarea { font: inherit; }Documentation: CSS documentation
WEB-CSS-048
Use supports to enhance a working baseline when a particular CSS feature is available.
@supports (text-wrap: balance) { h1 { text-wrap: balance; } }Documentation: CSS documentation
WEB-CSS-049
Multiple columns can suit short reference material on wide screens. Keep a single-column reading path on small screens.
.terms { column-width: 20rem; column-gap: 2rem; }
.terms > li { break-inside: avoid; }Documentation: CSS documentation
Reviewed . Send a correction through contact and corrections, including the entry number and supporting documentation.
Explore offers, save a shortlist and buy with the retailer.
Confirm current prices and offer terms with the retailer. Device saves start in this browser. Account sync is optional.
Read what recorded checks establish and what remains unconfirmed.
Country labels do not confirm delivery or convert prices.
Prepare a report to review and send yourself. Preparing it does not send it.
Open the tour on the guide page when you choose. You can skip or close it at any time.