Tailwind has first-class support for dark mode and a powerful theme customization system. This lesson covers the dark: variant, extending the default theme, and creating a custom design system.
export default {
darkMode: 'class', // 'media' uses OS preference, 'class' uses a CSS class
// ...
}
Heading
Body text
Card content
const html = document.documentElement;
const toggle = document.getElementById('toggle');
toggle.addEventListener('click', () => {
html.classList.toggle('dark');
localStorage.setItem('theme', html.classList.contains('dark') ? 'dark' : 'light');
});
// On page load, restore preference
if (localStorage.theme === 'dark') html.classList.add('dark');export default {
theme: {
// 'extend' adds to defaults; removing 'extend' REPLACES defaults
extend: {
colors: {
brand: {
50: '#fef9f0',
100: '#fdf0d9',
500: '#c4873e', // your primary color
600: '#b07635',
900: '#4a2e12',
}
},
fontFamily: {
sans: ['DM Sans', 'sans-serif'],
serif: ['DM Serif Display', 'serif'],
mono: ['JetBrains Mono', 'monospace'],
},
borderRadius: {
'4xl': '2rem',
},
boxShadow: {
'glow': '0 0 40px rgba(196, 135, 62, 0.15)',
}
}
}
}theme.extend.colors generate the full set of utilities automatically: bg-brand-500, text-brand-600, border-brand-100, etc.darkMode: 'class' to your config and define custom brand colors.dark from <html> and saves to localStorage.darkMode: 'class' toggles dark mode by adding the dark class to <html>.dark:bg-gray-900 dark:text-white — prefix any utility with dark: for dark styles.theme.extend adds to defaults. Without extend, you replace Tailwind's defaults entirely.theme.extend.colors auto-generate all color utilities (50–950 scale).