CSS Variables & Functions in CSS

From the CSS cheat sheet ยท Custom Properties & Functions ยท verified Jul 2026

CSS Variables & Functions

Define reusable values with custom properties and use calc, clamp, min, max

css
:root {
  --color-primary: #3b82f6;
  --spacing: 1rem;
  --radius: 8px;
}

.btn {
  background: var(--color-primary);
  padding: var(--spacing);
  border-radius: var(--radius);
}

.container {
  width: min(100% - 2rem, 1200px);
}
๐Ÿ’ก CSS variables cascade and inherit โ€” override them in any scope for easy theming
โšก clamp(min, preferred, max) replaces min-width + max-width + media queries in one line
๐Ÿ“Œ var(--name, fallback) provides a default value if the variable is not defined
๐ŸŸข Use @media (prefers-color-scheme: dark) with variable overrides for system dark mode
variablescustom-propertiescalcclamp

More CSS tasks

Back to the full CSS cheat sheet