Cheatsheets / CSS

CSS Cheatsheet

Complete CSS reference. Hit Ctrl+P to print.

Selectors

*Universal selector — matches every element
divType selector — matches all
elements
.classnameClass selector
#idID selector — should be unique per page
div, pGroup selector — matches either element
div pDescendant combinator — p anywhere inside div
div > pChild combinator — direct p children of div
div + pAdjacent sibling — p immediately after div
div ~ pGeneral sibling — all p siblings after div
[attr]Attribute exists
[attr="val"]Attribute equals value
[attr^="val"]Attribute starts with value
[attr$="val"]Attribute ends with value
[attr*="val"]Attribute contains value
:hoverPointer over element
:focusElement has keyboard focus
:focus-visibleFocus indicator visible (keyboard nav)
:activeElement being activated (click held)
:visitedVisited link
:disabled / :enabledForm field state
:checkedChecked checkbox or radio
:valid / :invalidForm field validation state
:required / :optionalForm field required state
:placeholder-shownInput showing placeholder text
:first-child / :last-childFirst or last child of parent
:nth-child(2n+1)Odd children (also accepts even, 3, 2n, etc.)
:nth-of-type(2)Second element of its type within parent
:only-childElement with no siblings
:emptyElement with no children or text
:not(.class)Negation — elements not matching selector
:is(h1, h2, h3)Matches any selector in the list
:where(h1, h2)Like :is() but zero specificity
:has(> img)Parent selector — element that contains match
::before / ::afterGenerated content pseudo-elements (needs content: "")
::placeholderStyle input placeholder text
::selectionUser-selected text
::first-line / ::first-letterFirst line or first letter of a block

Box Model

box-sizing: border-boxWidth/height includes padding and border (recommended default)
width / heightContent dimensions (or total if border-box)
min-width / max-widthWidth constraints
min-height / max-heightHeight constraints
padding: top right bottom leftInner spacing (shorthand: 4 values clockwise)
padding: 10px 20px10px top/bottom, 20px left/right
margin: top right bottom leftOuter spacing
margin: 0 autoCentre a block element horizontally
border: 1px solid #cccShorthand: width style color
border-radius: 8pxRounded corners
border-radius: 50%Circle (on square element)
outline: 2px solid blueOutside border — does not affect layout
outline-offset: 4pxGap between element edge and outline
overflow: hiddenClip overflowing content
overflow: autoScroll only when content overflows
overflow-x / overflow-yControl overflow per axis
display: blockFull-width block (div default)
display: inlineInline with text — ignores width/height
display: inline-blockInline but respects width/height
display: noneRemoves element from layout entirely
visibility: hiddenHides element but preserves layout space
opacity: 0.5Transparency (0=invisible, 1=opaque) — keeps layout space
aspect-ratio: 16 / 9Maintain aspect ratio

Flexbox

display: flexEnable flexbox on container
display: inline-flexInline flex container
flex-direction: rowMain axis direction: row | row-reverse | column | column-reverse
flex-wrap: wrapAllow items to wrap to next line: nowrap | wrap | wrap-reverse
flex-flow: row wrapShorthand for flex-direction + flex-wrap
justify-content: flex-startAlign items along main axis: flex-start | flex-end | center | space-between | space-around | space-evenly
align-items: stretchAlign items along cross axis: stretch | flex-start | flex-end | center | baseline
align-content: flex-startAlign multiple lines (when wrapping)
gap: 16pxSpace between flex items
gap: 8px 16pxRow gap, column gap
flex: 1Grow and shrink equally (shorthand for flex: 1 1 0)
flex: 0 0 200pxFixed 200px — no grow, no shrink
flex-grow: 1Grow factor relative to siblings
flex-shrink: 0Prevent shrinking below flex-basis
flex-basis: 200pxInitial main-axis size before grow/shrink
order: 2Change visual order (default: 0)
align-self: centerOverride align-items for this item
margin-left: autoPush item to the far right (or bottom in column)

Grid

display: gridEnable grid layout
grid-template-columns: 1fr 2fr 1frDefine column sizes (fr = fraction of free space)
grid-template-columns: repeat(3, 1fr)Three equal columns
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr))Responsive columns — fill with min 200px
grid-template-rows: auto 1fr autoDefine row sizes
gap: 24pxSpace between grid cells
grid-column: 1 / 3Item spans from column line 1 to 3
grid-column: span 2Item spans 2 columns
grid-row: 1 / -1Span all rows (-1 = last line)
grid-area: headerAssign item to named area
grid-template-areas: "hd hd" "sd main" "ft ft"Define named grid areas
justify-items: startAlign items horizontally in cell: start|end|center|stretch
align-items: startAlign items vertically in cell
place-items: centerShorthand for align-items + justify-items
justify-self: endOverride justify-items for one item
align-self: startOverride align-items for one item
justify-content: space-betweenDistribute columns in container
align-content: startDistribute rows in container

Typography

font-family: "Inter", sans-serifFont stack — first available font is used
font-size: 1remFont size (rem = relative to root, em = relative to parent)
font-weight: 400100–900; 400=normal, 700=bold
font-style: italicnormal | italic | oblique
font-variant: small-capsEnable font variant
line-height: 1.5Line spacing (unitless preferred)
letter-spacing: 0.05emCharacter spacing (tracking)
word-spacing: 0.1emWord spacing
text-align: centerleft | right | center | justify | start | end
text-decoration: underlinenone | underline | line-through | overline
text-decoration-color / style / thicknessCustomise underline appearance
text-transform: uppercasenone | uppercase | lowercase | capitalize
text-indent: 2emIndent first line of block
text-overflow: ellipsisShow … when text overflows (needs overflow:hidden + white-space:nowrap)
white-space: nowrapPrevent line wrapping
white-space: pre-wrapPreserve whitespace and allow wrapping
word-break: break-wordBreak long words to prevent overflow
hyphens: autoAutomatic hyphenation at line breaks
-webkit-line-clamp: 3Clamp text to N lines (use with display:-webkit-box)
@font-face { ... }Define custom font from file

Colors & Backgrounds

color: #3b82f6Text color — hex shorthand (#f00 = #ff0000)
color: rgb(59, 130, 246)RGB color
color: rgba(0, 0, 0, 0.5)RGB with alpha transparency
color: hsl(220, 90%, 60%)Hue (0-360) Saturation% Lightness%
color: oklch(0.65 0.2 250)Modern perceptually-uniform color space
background-color: transparentBackground color
background-image: url("img.jpg")Background image
background-size: covercover = fill, contain = fit, or explicit size
background-position: centerPosition: top | center | bottom | left | right | x% y%
background-repeat: no-repeatrepeat | no-repeat | repeat-x | repeat-y
background-attachment: fixedfixed = parallax effect; scroll = default
background: linear-gradient(to right, #f00, #00f)Linear gradient
background: radial-gradient(circle, #f00, #00f)Radial gradient
background: conic-gradient(red, yellow, green)Conic gradient
box-shadow: 0 4px 12px rgba(0,0,0,0.15)Box shadow: offset-x offset-y blur spread color
box-shadow: inset 0 0 8px #000Inner shadow
text-shadow: 1px 1px 2px rgba(0,0,0,0.3)Text shadow
filter: blur(4px) brightness(0.8)CSS filter effects
backdrop-filter: blur(8px)Blur content behind element (frosted glass)
mix-blend-mode: multiplyBlend element with content behind it

Positioning

position: staticDefault — normal document flow
position: relativeOffset from its normal position; creates stacking context
position: absoluteRemoved from flow; positioned to nearest non-static ancestor
position: fixedStays in viewport even when scrolling
position: stickyActs as relative until scroll threshold, then fixed
top / right / bottom / leftOffset from reference edge (used with non-static)
inset: 0Shorthand for top/right/bottom/left all zero
z-index: 10Stack order — higher = in front (needs non-static position)
float: leftFloat element; text wraps around it
clear: bothPrevent float wrapping on this element
transform: translate(-50%, -50%)Classic centering trick with position:absolute + top:50% + left:50%
transform: translateX(20px)Move on X axis only
transform: rotate(45deg)Rotate element
transform: scale(1.5)Scale element
transform: skew(10deg)Skew element
transform-origin: top leftSet transform reference point

Transitions & Animations

transition: all 0.3s easeShorthand: property duration timing-function delay
transition: color 0.2s, opacity 0.3sMultiple transitions comma-separated
transition-timing-function: easeease | ease-in | ease-out | ease-in-out | linear | cubic-bezier(...)
animation: spin 1s linear infiniteShorthand: name duration timing iteration
@keyframes spin { to { transform: rotate(360deg); } }Define animation keyframes
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }Fade-in keyframe
animation-nameReferences a @keyframes rule
animation-duration: 0.5sHow long one cycle takes
animation-iteration-count: infiniteNumber of cycles (or infinite)
animation-direction: alternatenormal | reverse | alternate | alternate-reverse
animation-fill-mode: forwardsRetain end state after animation completes
animation-play-state: pausedrunning | paused
@media (prefers-reduced-motion: reduce) { ... }Disable animations for users who prefer it
will-change: transformHint browser to promote element to its own layer

Media Queries

@media (min-width: 768px) { ... }Apply styles at 768px and wider (mobile-first)
@media (max-width: 767px) { ... }Apply styles below 768px (desktop-first)
@media (min-width: 640px) and (max-width: 1024px) { ... }Range query
@media (orientation: landscape) { ... }Landscape orientation
@media (prefers-color-scheme: dark) { ... }User has dark mode enabled
@media (prefers-reduced-motion: reduce) { ... }User prefers less animation
@media (hover: hover) { ... }Device supports hover (non-touch)
@media print { ... }Apply styles when printing
@media (display-mode: standalone) { ... }App is installed as PWA
:root { --clr: #333; }Define CSS custom property (variable)
color: var(--clr)Use CSS custom property
color: var(--clr, #000)With fallback value
@layer base, components, utilities;Declare cascade layers (order = priority)
@container (min-width: 400px) { ... }Container query — respond to parent size not viewport