CSS Color Systems — Modern Variable Approach
Hardcoded hex values are a maintenance nightmare. CSS custom properties let you build a color system that scales.
Level 1 — primitive palette
Raw colors with no assigned meaning:
:root {
--blue-50: #EFF6FF;
--blue-100: #DBEAFE;
--blue-500: #3B82F6;
--blue-700: #1D4ED8;
--blue-900: #1E3A8A;
}
Level 2 — semantic tokens
Names describe intent:
:root {
--color-primary: var(--blue-500);
--color-primary-hover: var(--blue-700);
--color-link: var(--blue-600);
--color-bg-page: var(--white);
--color-text: var(--gray-900);
}
Level 3 — component tokens
Only when a component needs an override:
.btn-primary {
--btn-bg: var(--color-primary);
--btn-text: var(--white);
--btn-bg-hover: var(--color-primary-hover);
}
Dark mode support
@media (prefers-color-scheme: dark) {
:root {
--color-bg-page: var(--gray-900);
--color-text: var(--gray-50);
}
}
Only the semantic tokens change — components don't need to know about dark mode.
Modern color features
color-mix()for tints/shades in CSSoklch()for perceptually uniform colorlight-dark()for automatic mode switching