tips
66 tips Css
One command, setting or habit at a time about Css: what it does, the line to copy and a caveat or two.
Layout and type 24
-
Section numbers update by themselves when you add or move a heading.
article { counter-reset: h2; } -
Spacing goes only between items, with no margin on the first or last one.
.toolbar { -
A fixed bottom bar gets extra padding only on devices with a notch or home indicator.
content="width=device-width, initial-scale=1, viewport-fit=cover"> -
Excerpts get the same height in a card grid, with an ellipsis at the end.
.excerpt { -
Long single lines end in three dots instead of overflowing, even inside flexbox.
overflow: hidden; -
Three common reasons a sticky header or sidebar scrolls away anyway, and how to fix them.
position: sticky; -
Named grid areas make a layout readable and easy to rearrange per screen size.
.page { -
A new stacking context stops decorative layers from slipping behind the whole page.
isolation: isolate; -
A dropdown or tooltip sticks to the element that opens it, and flips when space runs out.
.menu-button { -
A centered column that stays narrow on wide screens and keeps side space on phones.
width: min(100% - 2rem, 70rem); -
Makes an image fill a fixed-size box, cropping the edges instead of distorting it.
object-fit: cover; -
Sets top, right, bottom and left of a positioned element with one shorthand property.
.card { position: relative; } -
Centers items horizontally and vertically with one declaration on a grid container.
place-items: center; -
Sizes an element to the visible screen height on mobile even while the browser toolbar moves.
.hero { min-height: 100vh; /* fallback */ min-height: 100dvh; } -
Keeps scrolling inside a panel when it reaches its end instead of moving the page behind it.
.modal-body { overflow: auto; overscroll-behavior: contain; } -
Reserves space for the scrollbar so the layout does not shift when scrolling becomes possible.
.sidebar-list { overflow-y: auto; scrollbar-gutter: stable both-edges; } -
Asks the browser to break paragraph lines so the final line does not hold a single orphan word.
article p { text-wrap: pretty; } -
Leaves space above a link target so a fixed header does not cover it after scrolling.
h2[id], h3[id] { scroll-margin-top: 5rem; } -
Skips layout and painting of off-screen elements to make long pages render faster.
.post-card { content-visibility: auto; contain-intrinsic-size: auto 400px; } -
Makes a horizontally scrolling row stop neatly on each item as you swipe.
.row { display: flex; gap: 1rem; overflow-x: auto; scroll-snap-type: x mandatory; } -
Lets nested grid items share the tracks of the parent grid so content lines up across cards.
.card { display: grid; grid-row: span 3; grid-template-rows: subgrid; } -
Replaces left, right, top and bottom with properties that follow the text direction.
.container { max-inline-size: 70ch; margin-inline: auto; padding-block: 2rem; } -
Sets a value that grows with the window but stays between a lower and an upper limit.
h1 { font-size: clamp(1.75rem, 1.2rem + 2.5vw, 3rem); } -
Lets a component change its layout based on the width of its parent instead of the browser window.
.card-wrap { container-type: inline-size; }
Color and motion 19
-
A list of points creates easing curves that cubic-bezier() cannot express.
.toast { -
A hover scale no longer wipes out the translate that positions the element.
transition: scale 0.2s; -
Elements that switch to display: none can fade out instead of vanishing at once.
[popover] { -
Selected text gets your own colors instead of the system blue.
::selection { -
Focus styles made with box-shadow still show up for people who use a high contrast theme.
button:focus-visible { -
A scroll-driven animation replaces the IntersectionObserver code for simple reveal effects.
animation-timeline: view(); -
Soft grey text and faint borders get stronger when the system setting asks for more contrast.
@media (prefers-contrast: more) { -
Changing only the lightness number gives a predictable set of shades for one hue.
:root { -
Relative color syntax derives transparent or darker variants from one CSS variable.
background: hsl(from var(--accent) h s calc(l - 10)); -
Panels can slide open to their natural height without measuring them in JavaScript.
interpolate-size: allow-keywords; -
A gradient mask shows there is more to scroll, on any background.
.tag-row { -
Registers a typed CSS variable so the browser can animate and validate it.
@property --fill { -
Animates an element from a starting value when it first appears on the page.
@starting-style { -
Applies effects such as blur to the area behind an element, for frosted glass headers and overlays.
.header { -
Chooses between a light and a dark color value without a separate media query block.
:root { color-scheme: light dark; } -
Colors the active part of native checkboxes, radio buttons and sliders with one declaration.
:root { accent-color: #0b6bcb; } -
Detects the reduced-motion setting of the operating system so animations can be switched off.
@media (prefers-reduced-motion: reduce) { -
Animates navigation between two same-origin pages with a cross-fade, using only a stylesheet rule.
@view-transition { navigation: auto; } -
Blends two colors so shades and tints can be derived from a single base color.
:root { --accent: #0b6bcb; }
Selectors and cascade 15
-
Matches elements with no children, so empty wrappers can be hidden or styled differently.
.notice:empty { -
Striped rows stay alternating even when a filter hides some rows.
tr:nth-child(even of :not([hidden])) { -
After clicking a footnote or heading link, the reader sees which part the link meant.
.footnotes li:target, -
One selector covers open details, dialogs and dropdown pickers.
details:open > summary { -
Links to PDFs or other sites get a marker without adding classes by hand.
a[href$=".pdf" i]::after { -
One declaration removes every browser style from an element, including some you want to keep.
all: unset; -
The label moves up out of the way once a field has a value or focus.
.field label { -
Wraps CSS in a feature check, so newer features get a fallback in browsers that lack them.
@supports (display: grid) { -
Marks invalid form fields red only after interaction, not on an empty form that just loaded.
input:user-invalid { -
Selects an element depending on what it contains, so a parent can be styled by its children.
label:has(input:checked) { background: #e6f4ea; } -
Shows the focus ring when the keyboard is used and skips it for ordinary mouse clicks.
button:focus-visible, a:focus-visible { -
Writes child rules inside the parent rule in plain stylesheets, without Sass or a build step.
.card { -
Groups rules into ordered layers so the cascade order beats selector specificity.
@layer reset { a { color: inherit; } } -
Groups several selectors into one short expression, with the specificity of the strongest entry.
.post :is(h2, h3, h4) { margin-block-start: 2rem; } -
Wraps a selector so it adds no specificity, which keeps default styles easy to override.
:where(.prose) a { color: #0645ad; text-decoration: underline; }
Typography 5
-
A scaled local fallback font reduces the layout jump when the web font arrives.
@font-face { -
Buttons and headings get optically even padding because the font's built-in spacing is cut off.
text-box: trim-both cap alphabetic; -
Text without a real bold or italic font file stays in the designed style instead of a smeared imitation.
font-synthesis: none; -
Visitors can read the page in a fallback font instead of staring at invisible text.
font-display: swap; -
Controls the distance between text and its underline, so links stop cutting through descenders.
a {
Web development 3
-
Captures a report of all colors, fonts, unused declarations and media queries on a page.
-
One click on a code snippet selects all of it, ready to copy.
user-select: all; -
Tells the browser your page supports light and dark, so default controls match the visitor's setting.
color-scheme: light dark;
Nothing in this filter yet.