Selectors
Basic Selectors
*Universal — all elements
divType — all
elements
.classClass — elements with class
#idID — element with id
[attr]Attribute — has attribute
[attr="val"]Attribute equals value
Combinators
A BDescendant (any depth)
A > BDirect child only
A + BAdjacent sibling (next)
A ~ BGeneral sibling (all after)
Pseudo-classes
:hoverMouse over element
:focusElement has focus
:first-childFirst child of parent
:nth-child(n)Nth child (1-based, odd, even, 2n+1)
:not(sel)Negation — exclude matches
:has(sel)Parent selector (contains match)
Pseudo-elements
::beforeInsert content before element
::afterInsert content after element
::placeholderStyle input placeholder text
::selectionStyle highlighted text
Box Model
Box Sizing
/* Include padding/border in width */ *, *::before, *::after { box-sizing: border-box; }
Properties
marginSpace outside border
borderEdge around padding
paddingSpace inside border
width / heightContent dimensions
outlineRing outside margin (no space)
Shorthand
margin: 10px; /* all sides */ margin: 10px 20px; /* vertical | horizontal */ margin: 10px 20px 30px; /* top | horiz | bottom */ margin: 10px 20px 30px 40px; /* T R B L */
Flexbox
Container
.flex { display: flex; justify-content: center; /* main axis */ align-items: center; /* cross axis */ gap: 1rem; }
Container Properties
flex-directionrow | column | row-reverse | column-reverse
flex-wrapnowrap | wrap | wrap-reverse
justify-contentflex-start | center | space-between | space-around | space-evenly
align-itemsstretch | center | flex-start | flex-end | baseline
align-contentMulti-line cross axis alignment
gapSpace between flex items
Item Properties
flex: 1Grow to fill available space
flex: 0 0 200pxFixed width, no grow/shrink
align-selfOverride align-items for one item
orderChange visual order (default 0)
Grid
Container
.grid { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: auto 1fr auto; gap: 1rem; }
Container Properties
grid-template-columnsDefine column tracks: 1fr 2fr, repeat(3, 1fr)
grid-template-rowsDefine row tracks
grid-template-areasNamed areas: "header header" "nav main"
gapRow and column gaps
justify-itemsAlign items inline (horizontal)
align-itemsAlign items block (vertical)
Item Placement
.item { grid-column: 1 / 3; /* span cols 1-2 */ grid-row: 1 / -1; /* span all rows */ grid-area: header; /* named area */ }
Typography
Font Properties
font-familyTypeface stack: 'Inter', sans-serif
font-sizeSize: 1rem, 16px, clamp(1rem, 2vw, 2rem)
font-weightnormal (400) | bold (700) | 100-900
font-stylenormal | italic | oblique
line-heightLine spacing: 1.5 (unitless recommended)
letter-spacingCharacter spacing: 0.05em
Text Properties
text-alignleft | center | right | justify
text-decorationnone | underline | line-through
text-transformuppercase | lowercase | capitalize
text-overflowellipsis (with overflow: hidden)
white-spacenowrap | pre | pre-wrap
word-breakbreak-all | break-word
Colors & Backgrounds
Color Formats
color: #ff6600; /* hex */ color: rgb(255, 102, 0); /* rgb */ color: hsl(24, 100%, 50%); /* hsl */ color: oklch(70% 0.15 50); /* oklch */
Background Properties
background-colorSolid fill: #f0f0f0
background-imageurl(img.jpg) or gradient
background-sizecover | contain | 100px 200px
background-positioncenter | top right | 50% 50%
background-repeatno-repeat | repeat-x | repeat-y
Gradients
background: linear-gradient(to right, #f00, #00f); background: radial-gradient(circle, #fff, #000); background: conic-gradient(red, yellow, green);
Transitions & Animation
Transitions
.btn { transition: background 0.3s ease, transform 0.2s; } .btn:hover { background: #0056b3; transform: scale(1.05); }
Transition Properties
transition-propertyWhich property to animate
transition-durationLength: 0.3s, 300ms
transition-timing-functionease | linear | ease-in-out | cubic-bezier()
transition-delayWait before starting
Keyframe Animation
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } .icon { animation: spin 1s linear infinite; }
Animation Properties
animation-nameReference @keyframes name
animation-durationLength of one cycle
animation-iteration-count1 | 3 | infinite
animation-directionnormal | alternate | reverse
animation-fill-modeforwards | backwards | both
Responsive Design
Media Queries
@media (max-width: 768px) { .sidebar { display: none; } } @media (prefers-color-scheme: dark) { body { background: #1a1a1a; color: #eee; } }
Common Breakpoints
max-width: 480pxMobile phones
max-width: 768pxTablets
max-width: 1024pxSmall laptops
max-width: 1280pxDesktops
Viewport Units
vw / vh% of viewport width / height
dvhDynamic viewport height (mobile-safe)
svh / lvhSmall / large viewport height
cqiContainer query inline size
Container Queries
.card-wrapper { container-type: inline-size; } @container (min-width: 400px) { .card { flex-direction: row; } }
Positioning
Position Values
staticDefault — normal document flow
relativeOffset from normal position; keeps space
absolutePositioned to nearest positioned ancestor
fixedPositioned to viewport; stays on scroll
stickyToggles relative/fixed based on scroll
Centering Patterns
/* Flex center */ display: flex; justify-content: center; align-items: center; /* Grid center */ display: grid; place-items: center;
Stacking
z-indexStack order (higher = on top); needs position
isolation: isolateCreates new stacking context
Custom Properties
Define & Use
:root { --color-primary: #3b82f6; --spacing-md: 1rem; } .btn { background: var(--color-primary); padding: var(--spacing-md); }
Fallback Values
color: var(--accent, #ff6600); /* uses #ff6600 if --accent is not defined */
Dynamic Theming
[data-theme="dark"] { --bg: #1a1a2e; --text: #e0e0e0; } body { background: var(--bg); color: var(--text); }