CSS Cheatsheet

Basics

Use this CSS reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Anatomy of a Rule

selector {
  property: value;           /* declaration */
  property: value1 value2;   /* multiple values */
}

A ruleset = selector + declaration block. A declaration = property + colon + value + semicolon. Whitespace is insignificant except inside strings.

Use this CSS cheatsheet when you move from algorithm practice into real frontend project work: it gives quick syntax for styling forms, dashboards, portfolio apps, and interview take-home screens without leaving the browser reference flow.

/* Comments: block only, no // */
/* Can span
   multiple lines */

Cascade, Specificity, and Inheritance

Cascade order (later wins when specificity is equal): 1. Browser default styles 2. Author stylesheets (in source order) 3. !important declarations

Specificity — calculated as (inline, id, class/attr/pseudo-class, element/pseudo-element):

SelectorSpecificity
*0-0-0-0
div0-0-0-1
.cls, [attr], :hover0-0-1-0
#id0-1-0-0
style="" (inline)1-0-0-0
!importantoverrides all
p { color: red; }              /* 0-0-0-1 */
.text { color: blue; }         /* 0-0-1-0 — wins */
#main p { color: green; }      /* 0-1-0-1 */
p { color: purple !important; }/* overrides everything */

Inherited properties (pass from parent by default): color, font-*, line-height, letter-spacing, text-*, visibility, cursor, list-style-*, quotes.

Non-inherited (reset to initial): margin, padding, border, background, width, height, display, position, float.

.child { color: inherit; }       /* force inherit */
.other { color: initial; }       /* reset to spec initial */
.more  { color: unset; }         /* inherit if inheritable, else initial */
.reset { color: revert; }        /* revert to user-agent stylesheet */
.rl    { color: revert-layer; }  /* revert to previous cascade layer */

Linking and Loading Stylesheets

<!-- external (preferred) -->
<link rel="stylesheet" href="styles.css">

<!-- scoped to media -->
<link rel="stylesheet" href="print.css" media="print">

<!-- inline in <head> -->
<style>
  body { margin: 0; }
</style>

<!-- inline on element (highest specificity) -->
<p style="color: red;">text</p>
/* @import inside CSS — blocks parallel loading, prefer <link> */
@import url("reset.css");
@import url("theme.css") screen;
@import "mobile.css" screen and (max-width: 600px);
/* must appear before any other rules except @charset and @layer */

@charset

/* must be the very first statement if used */
@charset "UTF-8";

Units

Absolute Units

UnitMeaning
pxCSS pixel (1/96 in)
ptpoint (1/72 in)
pcpica (12pt)
cm, mm, inphysical measurements
Qquarter-millimeter

Relative Units

UnitRelative to
emcurrent element's font-size
remroot (<html>) font-size
exx-height of current font
chwidth of "0" glyph in current font
lhline height of current element
rlhline height of root element
%parent (meaning is property-specific)
vw1% of viewport width
vh1% of viewport height
vminsmaller of vw/vh
vmaxlarger of vw/vh
svh, lvh, dvhsmall/large/dynamic viewport height
svw, lvw, dvwsmall/large/dynamic viewport width
cqw, cqh1% of container query width/height
cqi, cqb1% of container inline/block size
html  { font-size: 16px; }
h1    { font-size: 2rem; }      /* 32px */
p     { padding: 1em; }         /* relative to own font-size */
.hero { height: 100dvh; }       /* full dynamic viewport */
.col  { max-width: 65ch; }      /* ~65 characters */

Math Functions

.box {
  width: calc(100% - 2rem);              /* mix units */
  font-size: clamp(1rem, 2.5vw, 2rem);  /* min, preferred, max */
  padding: min(5%, 2rem);                /* smaller of two */
  margin: max(1rem, 2vw);               /* larger of two */
  width: round(nearest, 33.33%, 1px);   /* CSS Values 4 */
  transform: translateX(sin(45deg) * 100px);
}

Shorthand Properties

/* margin / padding — top right bottom left (clockwise) */
margin: 10px;                      /* all sides */
margin: 10px 20px;                 /* top/bottom  left/right */
margin: 10px 20px 30px;            /* top  left/right  bottom */
margin: 10px 20px 30px 40px;       /* top right bottom left */

/* border */
border: 1px solid #ccc;            /* width style color */

/* font */
font: italic 600 1.2rem/1.5 "Inter", sans-serif;
/* style weight size/line-height family */

/* background */
background: #fff url("img.png") no-repeat center / cover;

/* transition */
transition: color 0.2s ease, opacity 0.3s;

/* animation */
animation: slide 0.4s ease-out forwards;

Shorthands reset unspecified sub-properties to their initial values. Use longhands when overriding only one side.

Value Keywords

KeywordMeaning
inheritUse parent's computed value
initialSpec-defined initial value
unsetinherit if inheritable, else initial
revertReset to user-agent stylesheet
revert-layerReset to previous cascade layer
autoBrowser-computed (property-specific)
noneDisable / no value
normalProperty's "normal" state
currentColorCurrent color value
transparentFully transparent (rgba(0,0,0,0))

At-Rules Overview

At-rulePurpose
@charsetDeclare file encoding
@importImport another stylesheet
@layerDeclare cascade layer
@mediaConditional by media feature
@supportsConditional by CSS feature support
@keyframesDefine animation keyframes
@font-faceDefine custom font
@containerContainer queries
@propertyRegister typed custom property
@counter-styleCustom list counter
@pagePrint page styles
@namespaceXML namespace

Cascade Layers

/* Declare order first — later layers win */
@layer reset, base, components, utilities;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
}
@layer base {
  body { margin: 0; font-family: system-ui, sans-serif; }
}
@layer components {
  .btn { padding: 0.5rem 1rem; }
}
/* Unlayered styles always beat all @layer rules */

@supports

@supports (display: grid) {
  .layout { display: grid; }
}
@supports not (display: grid) {
  .layout { display: flex; }
}
@supports (display: grid) and (gap: 1rem) {
  /* both features supported */
}
@supports (display: grid) or (display: -ms-grid) {
  /* either supported */
}
@supports selector(:has(a)) {
  /* :has() selector supported */
}

display Values

display: block;        /* stacks vertically, full width */
display: inline;       /* flows in text, ignores width/height */
display: inline-block; /* inline flow + block sizing */
display: flex;
display: inline-flex;
display: grid;
display: inline-grid;
display: none;         /* removed from layout entirely */
display: contents;     /* element invisible; children promoted */
display: flow-root;    /* creates new Block Formatting Context */
display: table;        /* like <table> */
display: list-item;    /* generates list marker */

visibility and opacity

visibility: hidden;  /* invisible, still occupies space */
display: none;       /* invisible, removed from layout */
opacity: 0;          /* invisible, occupies space, receives pointer events */

overflow

overflow: visible;  /* default — overflows container */
overflow: hidden;   /* clips overflow */
overflow: scroll;   /* always shows scrollbars */
overflow: auto;     /* scrollbars only when content overflows */
overflow: clip;     /* clips; no scroll context created */

overflow-x: auto;
overflow-y: hidden;
overflow: hidden auto; /* x then y */

all Property

.reset {
  all: initial;       /* reset all properties to initial values */
  all: inherit;       /* inherit all from parent */
  all: unset;         /* unset all properties */
  all: revert;        /* revert to user-agent defaults */
  all: revert-layer;  /* revert to previous cascade layer */
}

Custom Properties (Quick Reference)

:root {
  --color-brand: #3b82f6;
  --spacing-md: 1rem;
}
.btn {
  background: var(--color-brand);
  padding: var(--spacing-md, 0.75rem); /* second arg is fallback */
}

Full coverage in the Variables cheatsheet.

Common Global Reset

*, *::before, *::after {
  box-sizing: border-box;
}
body {
  margin: 0;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
img, video, svg {
  max-width: 100%;
  display: block;
}
input, button, textarea, select {
  font: inherit;
}