Quick Reference CSS Cheatsheet
Complete CSS3 quick reference — selectors, cascade, box model, flex, grid, typography,
color, motion, responsive design, custom properties, and colorized snippets for fast revision.
Colorized examples * { }
h1, .title, #hero { color: teal; }
nav a { }
ul > li { }
h1 + p { }
h1 ~ p { }
a[target="_blank"] { }
input[type^="text"] { }
:is(h1, h2, h3) { }
:where(.card) p { }
:not(.hidden) { }
:has(img) { } 🪜Cascade, Specificity & Inheritance
#nav .link:hover { }
a:hover { color: red !important; }
@layer base, components, utilities;
@layer components {
.btn { padding: .5rem 1rem; }
} *, *::before, *::after {
box-sizing: border-box;
}
.card {
width: 320px;
max-width: 100%;
margin: 1rem auto;
padding: 1rem 1.25rem;
border: 2px solid #334155;
} 🧱Display, Position & Flow .badge { display: inline-block; }
.hidden { display: none; }
.sr-only {
position: absolute;
width: 1px; height: 1px;
overflow: hidden; clip-path: inset(50%);
}
.sticky-bar {
position: sticky;
top: 0;
z-index: 10;
} .row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
gap: 1rem;
}
.row > * {
flex: 1 1 200px;
} Container Items .layout {
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-template-rows: auto 1fr auto;
gap: 1rem;
}
.sidebar { grid-column: 1 / 4; }
.main { grid-column: 4 / -1; }
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
} body {
font-family: "Segoe UI", system-ui, sans-serif;
font-size: 1rem;
line-height: 1.6;
font-weight: 400;
letter-spacing: 0.01em;
text-wrap: pretty;
}
h1 {
font-size: clamp(1.75rem, 4vw + 1rem, 3rem);
text-wrap: balance;
} 🎨Color, Background & Images :root {
color-scheme: light dark;
accent-color: #2563eb;
}
.hero {
color: oklch(0.35 0.08 250);
background:
linear-gradient(135deg, #0ea5e9, #6366f1),
url("/hero.jpg") center / cover no-repeat;
opacity: 1;
} ▢Border, Outline & Shadow .panel {
border: 1px solid #cbd5e1;
border-radius: 0.75rem;
outline: 2px solid transparent;
outline-offset: 2px;
box-shadow: 0 8px 24px rgb(15 23 42 / 0.12);
}
.panel:focus-visible {
outline-color: #2563eb;
} ✨Transitions & Animations .btn {
transition: transform .2s ease, box-shadow .2s ease;
}
.btn:hover { transform: translateY(-2px); }
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
.enter {
animation: fade-in .4s ease both;
}
@media (prefers-reduced-motion: reduce) {
* { animation: none; transition: none; }
} 📱Responsive & Media Queries .container {
width: min(100% - 2rem, 72rem);
margin-inline: auto;
}
@media (width >= 768px) {
.nav { display: flex; }
}
@container card (min-width: 400px) {
.card-body { display: grid; grid-template-columns: 1fr 1fr; }
} 👻Pseudo-classes & Pseudo-elements a:hover, a:focus-visible { text-decoration: underline; }
li:nth-child(odd) { background: #f1f5f9; }
input:invalid { border-color: crimson; }
.card::before {
content: "";
position: absolute;
inset: 0;
}
::selection { background: #93c5fd; } Common pseudo-classes Pseudo-elements 🧩Custom Properties & Functions :root {
--brand: #2563eb;
--space: 1rem;
--radius: 0.5rem;
}
.btn {
background: var(--brand, #1d4ed8);
padding: calc(var(--space) * 0.75) var(--space);
border-radius: var(--radius);
} ⚠️Gotchas & Interview Tips - Prefer
border-box globally — width includes padding and border. - Margin collapse: vertical margins between siblings/parent-child can merge; flex/grid/padding/border stop it.
absolute positions relative to nearest positioned ancestor (not always the parent). z-index only works on positioned / flex / grid items; new stacking contexts from opacity, transform, filter. - Use
rem for typography and spacing; em for component-relative sizing. - Never remove focus outlines without a visible
:focus-visible replacement. :has(), container queries, and oklch() are modern — check support if you must support old browsers. - Shorthands reset omitted longhands (e.g.
background: red clears image/layers).
Next: CSS tutorial ·
Cheatsheet hub ·
HTML cheatsheet |