| name | css-mastery |
| description | Expert CSS guidance — invoked when writing or reviewing CSS/SCSS for layout, responsive design, animations, modern CSS features, or performance. |
CSS Mastery
The Box Model
Every element is a rectangular box. Two sizing modes control how width and height are calculated:
content-box (default)
┌──────────────────────────────┐
│ margin │
│ ┌───────────────────────┐ │
│ │ border │ │
│ │ ┌─────────────────┐ │ │
│ │ │ padding │ │ │
│ │ │ ┌───────────┐ │ │ │
│ │ │ │ content │ │ │ │ ← width/height apply here only
│ │ │ └───────────┘ │ │ │
│ │ └─────────────────┘ │ │
│ └───────────────────────┘ │
└──────────────────────────────┘
border-box
┌──────────────────────────────┐
│ margin │
│ ┌───────────────────────┐ │ ← width/height apply here
│ │ border │ │
│ │ ┌─────────────────┐ │ │
│ │ │ padding │ │ │
│ │ │ ┌───────────┐ │ │ │
│ │ │ │ content │ │ │ │
│ │ │ └───────────┘ │ │ │
│ │ └─────────────────┘ │ │
│ └───────────────────────┘ │
└──────────────────────────────┘
Always apply globally:
*, *::before, *::after {
box-sizing: border-box;
}
Margin Collapsing
Vertical margins collapse between adjacent block elements — the larger margin wins.
div { margin-bottom: 24px; }
p { margin-top: 16px; }
/* Gap between them = 24px, not 40px */
Collapsing does NOT happen when:
- Elements are in a flex or grid container
- The parent has
overflow: hidden/auto (creates a Block Formatting Context)
- There is padding or border between parent and child
- Elements are absolutely or floatingly positioned
Layout Systems
Flexbox — One-Dimensional
Main axis (row): ←────────────────────→
[item] [item] [item]
Main axis (col): ↑
[item]
[item]
↓
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 1rem;
}
.item {
flex: 1 1 200px;
}
Common Flexbox use cases:
- Navigation bars:
justify-content: space-between
- Centering:
justify-content: center; align-items: center
- Card rows with equal height
- Form label + input pairs
CSS Grid — Two-Dimensional
grid-template-columns: 1fr 2fr 1fr
┌──────┬────────────┬──────┐
│ 1fr │ 2fr │ 1fr │
│ │ │ │
├──────┼────────────┼──────┤
│ │ │ │
└──────┴────────────┴──────┘
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto;
gap: 1.5rem;
}
.layout {
display: grid;
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
grid-template-columns: 240px 1fr 1fr;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.wide-card {
grid-column: 1 / span 2;
grid-row: 2 / 4;
}
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}
auto-fill vs auto-fit:
auto-fill (3 items, 5 columns fit):
[item][item][item][ ][ ]
auto-fit (3 items, 5 columns fit):
[ item ][ item ][ item ]
Positioning
static — normal flow (default)
relative — offset from normal position; creates stacking context with z-index
absolute — removed from flow; positioned relative to nearest positioned ancestor
fixed — relative to viewport; stays on scroll
sticky — hybrid: relative until threshold, then fixed within scroll container
.parent {
position: relative;
}
.overlay {
position: absolute;
inset: 0;
}
.header {
position: sticky;
top: 0;
z-index: 100;
}
Stacking context is created by: position + z-index (not auto), opacity < 1, transform, filter, will-change, isolation: isolate.
Multi-Column Layout
.article-body {
column-count: 3;
column-width: 20ch;
column-gap: 2rem;
column-rule: 1px solid #e2e8f0;
}
.figure {
break-inside: avoid;
}
Responsive Design
Mobile-First
Start with the smallest viewport. Layer complexity upward:
.card {
display: block;
padding: 1rem;
}
@media (min-width: 768px) {
.card {
display: flex;
padding: 1.5rem;
}
}
@media (min-width: 1200px) {
.card {
padding: 2rem;
}
}
Fluid Sizing with clamp()
h1 {
font-size: clamp(1.75rem, 4vw, 3rem);
}
.container {
width: clamp(320px, 90%, 1200px);
margin-inline: auto;
}
.hero-gap {
padding-block: clamp(3rem, 8vh, 8rem);
}
Container Queries
Component responds to its container size, not the viewport:
.card-wrapper {
container-type: inline-size;
container-name: card;
}
@container card (min-width: 400px) {
.card {
display: flex;
flex-direction: row;
}
}
@container (min-width: 600px) {
.card__image {
width: 200px;
flex-shrink: 0;
}
}
CSS Custom Properties (Variables)
:root {
--color-brand: #0066cc;
--color-brand-hover: #0052a3;
--spacing-base: 0.25rem;
--radius-md: 0.5rem;
--font-sans: 'Inter', system-ui, sans-serif;
}
.button {
background: var(--color-brand);
padding: calc(var(--spacing-base) * 3) calc(var(--spacing-base) * 6);
border-radius: var(--radius-md);
color: var(--color-text-on-brand, white);
}
.button--danger {
--color-brand: #dc2626;
--color-brand-hover: #b91c1c;
}
Modern CSS Features
Mathematical Functions
.sidebar {
width: calc(100% - 2rem);
}
.container {
width: min(100%, 1200px);
}
.text {
font-size: max(1rem, 2.5vw);
}
.heading {
font-size: clamp(1.5rem, 3vw + 1rem, 4rem);
}
Logical Properties (RTL/LTR Support)
margin-left: margin-inline-start
margin-right: margin-inline-end
margin-top: margin-block-start
margin-bottom: margin-block-end
padding-left/right: padding-inline
padding-top/bottom: padding-block
border-left: border-inline-start
width: inline-size
height: block-size
Modern Selectors
:is(h1, h2, h3) + p {
margin-top: 0.5rem;
}
:where(h1, h2, h3, h4) {
line-height: 1.2;
}
.card:has(img) {
padding-top: 0;
}
.form-field:has(input:invalid) label {
color: red;
}
li:not(:last-child) {
border-bottom: 1px solid #e2e8f0;
}
Cascade Layers
@layer reset, base, components, utilities;
@layer reset {
* { margin: 0; padding: 0; }
}
@layer base {
a { color: var(--color-brand); }
}
@layer components {
.button { }
}
@layer utilities {
.sr-only { }
}
Native CSS Nesting
.card {
padding: 1rem;
border-radius: 0.5rem;
&:hover {
box-shadow: 0 4px 12px rgb(0 0 0 / 0.1);
}
& .card__title {
font-size: 1.25rem;
}
@media (min-width: 768px) {
padding: 1.5rem;
}
}
@scope
@scope (.card) to (.card__footer) {
p { color: var(--color-text-secondary); }
}
Specificity and the Cascade
Specificity order (high → low):
1. !important (avoid in your own code)
2. Inline styles (style="")
3. ID selectors (#id)
4. Class, attribute, pseudo-class (.class, [attr], :hover)
5. Element, pseudo-element (div, ::before)
Specificity values (A, B, C):
#nav .item:hover → (0, 1, 1, 1) → wins over
div.item:hover → (0, 0, 1, 1)
Use @layer to manage specificity without specificity wars. Within a layer, normal specificity applies; layers themselves create a priority order.
Animations and Transitions
.button {
background: var(--color-brand);
transition: background 200ms ease, transform 150ms ease;
}
.button:hover {
background: var(--color-brand-hover);
transform: translateY(-1px);
}
@keyframes slide-in {
from {
opacity: 0;
transform: translateY(16px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.toast {
animation: slide-in 300ms ease forwards;
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
}
}
will-change — use sparingly:
.animated-panel {
will-change: transform;
}
.animated-panel.done {
will-change: auto;
}
Performance
.use-these-for-animation {
transform: translateX(100px) scale(1.1);
opacity: 0.5;
}
.avoid-animating {
top: 100px;
left: 100px;
width: 200px;
margin-top: 10px;
}
.widget {
contain: layout paint;
}
.card {
contain: strict;
}
Critical CSS pattern:
<style>
body { margin: 0; font-family: system-ui; }
.hero { }
</style>
<link rel="preload" href="styles.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="styles.css"></noscript>
Anti-Patterns
- Magic numbers without comment:
top: 37px — why 37? Document it.
- Deeply nested selectors:
.page .container .section .card .title span { } — high specificity, fragile
- Overriding third-party with
!important everywhere — use @layer to slot third-party below your styles
- Fixed px for font sizes — use
rem so users' browser font size preferences are respected
- Not using
box-sizing: border-box globally — inconsistent sizing math everywhere
- Using
margin instead of gap in flex/grid — gap is direction-agnostic and doesn't collapse
- No
:focus-visible styles — keyboard users can't see where they are
Quick Reference Checklist