Selectors
*Universal selector — matches every elementdivType selector — matches all elements
.classnameClass selector#idID selector — should be unique per pagediv, pGroup selector — matches either elementdiv pDescendant combinator — p anywhere inside divdiv > pChild combinator — direct p children of divdiv + pAdjacent sibling — p immediately after divdiv ~ 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 blockBox Model
box-sizing: border-boxWidth/height includes padding and border (recommended default)width / heightContent dimensions (or total if border-box)min-width / max-widthWidth constraintsmin-height / max-heightHeight constraintspadding: top right bottom leftInner spacing (shorthand: 4 values clockwise)padding: 10px 20px10px top/bottom, 20px left/rightmargin: top right bottom leftOuter spacingmargin: 0 autoCentre a block element horizontallyborder: 1px solid #cccShorthand: width style colorborder-radius: 8pxRounded cornersborder-radius: 50%Circle (on square element)outline: 2px solid blueOutside border — does not affect layoutoutline-offset: 4pxGap between element edge and outlineoverflow: hiddenClip overflowing contentoverflow: autoScroll only when content overflowsoverflow-x / overflow-yControl overflow per axisdisplay: blockFull-width block (div default)display: inlineInline with text — ignores width/heightdisplay: inline-blockInline but respects width/heightdisplay: noneRemoves element from layout entirelyvisibility: hiddenHides element but preserves layout spaceopacity: 0.5Transparency (0=invisible, 1=opaque) — keeps layout spaceaspect-ratio: 16 / 9Maintain aspect ratioFlexbox
display: flexEnable flexbox on containerdisplay: inline-flexInline flex containerflex-direction: rowMain axis direction: row | row-reverse | column | column-reverseflex-wrap: wrapAllow items to wrap to next line: nowrap | wrap | wrap-reverseflex-flow: row wrapShorthand for flex-direction + flex-wrapjustify-content: flex-startAlign items along main axis: flex-start | flex-end | center | space-between | space-around | space-evenlyalign-items: stretchAlign items along cross axis: stretch | flex-start | flex-end | center | baselinealign-content: flex-startAlign multiple lines (when wrapping)gap: 16pxSpace between flex itemsgap: 8px 16pxRow gap, column gapflex: 1Grow and shrink equally (shorthand for flex: 1 1 0)flex: 0 0 200pxFixed 200px — no grow, no shrinkflex-grow: 1Grow factor relative to siblingsflex-shrink: 0Prevent shrinking below flex-basisflex-basis: 200pxInitial main-axis size before grow/shrinkorder: 2Change visual order (default: 0)align-self: centerOverride align-items for this itemmargin-left: autoPush item to the far right (or bottom in column)Grid
display: gridEnable grid layoutgrid-template-columns: 1fr 2fr 1frDefine column sizes (fr = fraction of free space)grid-template-columns: repeat(3, 1fr)Three equal columnsgrid-template-columns: repeat(auto-fill, minmax(200px, 1fr))Responsive columns — fill with min 200pxgrid-template-rows: auto 1fr autoDefine row sizesgap: 24pxSpace between grid cellsgrid-column: 1 / 3Item spans from column line 1 to 3grid-column: span 2Item spans 2 columnsgrid-row: 1 / -1Span all rows (-1 = last line)grid-area: headerAssign item to named areagrid-template-areas: "hd hd" "sd main" "ft ft"Define named grid areasjustify-items: startAlign items horizontally in cell: start|end|center|stretchalign-items: startAlign items vertically in cellplace-items: centerShorthand for align-items + justify-itemsjustify-self: endOverride justify-items for one itemalign-self: startOverride align-items for one itemjustify-content: space-betweenDistribute columns in containeralign-content: startDistribute rows in containerTypography
font-family: "Inter", sans-serifFont stack — first available font is usedfont-size: 1remFont size (rem = relative to root, em = relative to parent)font-weight: 400100–900; 400=normal, 700=boldfont-style: italicnormal | italic | obliquefont-variant: small-capsEnable font variantline-height: 1.5Line spacing (unitless preferred)letter-spacing: 0.05emCharacter spacing (tracking)word-spacing: 0.1emWord spacingtext-align: centerleft | right | center | justify | start | endtext-decoration: underlinenone | underline | line-through | overlinetext-decoration-color / style / thicknessCustomise underline appearancetext-transform: uppercasenone | uppercase | lowercase | capitalizetext-indent: 2emIndent first line of blocktext-overflow: ellipsisShow … when text overflows (needs overflow:hidden + white-space:nowrap)white-space: nowrapPrevent line wrappingwhite-space: pre-wrapPreserve whitespace and allow wrappingword-break: break-wordBreak long words to prevent overflowhyphens: autoAutomatic hyphenation at line breaks-webkit-line-clamp: 3Clamp text to N lines (use with display:-webkit-box)@font-face { ... }Define custom font from fileColors & Backgrounds
color: #3b82f6Text color — hex shorthand (#f00 = #ff0000)color: rgb(59, 130, 246)RGB colorcolor: rgba(0, 0, 0, 0.5)RGB with alpha transparencycolor: hsl(220, 90%, 60%)Hue (0-360) Saturation% Lightness%color: oklch(0.65 0.2 250)Modern perceptually-uniform color spacebackground-color: transparentBackground colorbackground-image: url("img.jpg")Background imagebackground-size: covercover = fill, contain = fit, or explicit sizebackground-position: centerPosition: top | center | bottom | left | right | x% y%background-repeat: no-repeatrepeat | no-repeat | repeat-x | repeat-ybackground-attachment: fixedfixed = parallax effect; scroll = defaultbackground: linear-gradient(to right, #f00, #00f)Linear gradientbackground: radial-gradient(circle, #f00, #00f)Radial gradientbackground: conic-gradient(red, yellow, green)Conic gradientbox-shadow: 0 4px 12px rgba(0,0,0,0.15)Box shadow: offset-x offset-y blur spread colorbox-shadow: inset 0 0 8px #000Inner shadowtext-shadow: 1px 1px 2px rgba(0,0,0,0.3)Text shadowfilter: blur(4px) brightness(0.8)CSS filter effectsbackdrop-filter: blur(8px)Blur content behind element (frosted glass)mix-blend-mode: multiplyBlend element with content behind itPositioning
position: staticDefault — normal document flowposition: relativeOffset from its normal position; creates stacking contextposition: absoluteRemoved from flow; positioned to nearest non-static ancestorposition: fixedStays in viewport even when scrollingposition: stickyActs as relative until scroll threshold, then fixedtop / right / bottom / leftOffset from reference edge (used with non-static)inset: 0Shorthand for top/right/bottom/left all zeroz-index: 10Stack order — higher = in front (needs non-static position)float: leftFloat element; text wraps around itclear: bothPrevent float wrapping on this elementtransform: translate(-50%, -50%)Classic centering trick with position:absolute + top:50% + left:50%transform: translateX(20px)Move on X axis onlytransform: rotate(45deg)Rotate elementtransform: scale(1.5)Scale elementtransform: skew(10deg)Skew elementtransform-origin: top leftSet transform reference pointTransitions & Animations
transition: all 0.3s easeShorthand: property duration timing-function delaytransition: color 0.2s, opacity 0.3sMultiple transitions comma-separatedtransition-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 keyframeanimation-nameReferences a @keyframes ruleanimation-duration: 0.5sHow long one cycle takesanimation-iteration-count: infiniteNumber of cycles (or infinite)animation-direction: alternatenormal | reverse | alternate | alternate-reverseanimation-fill-mode: forwardsRetain end state after animation completesanimation-play-state: pausedrunning | paused@media (prefers-reduced-motion: reduce) { ... }Disable animations for users who prefer itwill-change: transformHint browser to promote element to its own layerMedia 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 propertycolor: 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