TFT

CSS Variables Generator

Convert your color palette into ready-to-use CSS custom properties. Generate a clean :root variable block for any design system or theme.

Color Variables
Define your CSS color variables
#
#
#
#
Color Preview
color-primary
color-secondary
color-accent
CSS Output
Copy or download your CSS variables
:root {
  --color-primary: #3B82F6;
  --color-secondary: #8B5CF6;
  --color-accent: #F59E0B;
}
:root {
  --color-primary: #3B82F6;
  --color-primary-rgb: 59, 130, 246;
  --color-secondary: #8B5CF6;
  --color-secondary-rgb: 139, 92, 246;
  --color-accent: #F59E0B;
  --color-accent-rgb: 245, 158, 11;
}
:root {
  --color-primary: #3B82F6;
  --color-primary-rgb: 59 130 246;
  --color-secondary: #8B5CF6;
  --color-secondary-rgb: 139 92 246;
  --color-accent: #F59E0B;
  --color-accent-rgb: 245 158 11;
}

Related Color Tools

What This Tool Generates

Paste a color palette and get CSS custom properties (variables) with RGB fallbacks. The output includes modern CSS syntax that works with CSS-in-JS, Tailwind integration, and HSL variants for dynamic theming.

Why Use CSS Variables for Colors

CSS custom properties let you define a color once and use it everywhere. Change the variable value, and every instance updates automatically. This is essential for:

  • Dark mode theming (swap variable values, not individual colors)
  • Brand color updates (change one variable, update the entire site)
  • Dynamic styling with JavaScript (modify variables at runtime)
  • Consistent color usage across large codebases

Understanding the Output Format

The generator produces multiple formats for each color:

:root {
  /* Primary color with RGB fallback */
  --primary: #3B82F6;
  --primary-rgb: 59, 130, 246;
  
  /* HSL for dynamic adjustments */
  --primary-h: 217;
  --primary-s: 91%;
  --primary-l: 60%;
}

Use var(--primary) for the hex value, rgb(var(--primary-rgb)) for RGB contexts, or hsl(var(--primary-h), var(--primary-s), var(--primary-l)) for HSL.

Real Use Cases

Dark Mode Implementation

Define light mode colors in :root, dark mode in [data-theme="dark"]. Toggle the data attribute and all colors update automatically.

Component Library Theming

Build components that use var(--primary) instead of hardcoded colors. Consumers theme your components by overriding the variables.

Brand Color Experiments

Hook up variables to a color picker. Designers can adjust brand colors in real-time and see the impact across the entire site.

Tailwind CSS Integration

Reference CSS variables in tailwind.config.js. Get the flexibility of variables with the utility-first workflow of Tailwind.

Tailwind CSS Integration

The Tailwind export format integrates CSS variables directly into your config:

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      colors: {
        primary: 'hsl(var(--primary-h), var(--primary-s), var(--primary-l))',
      }
    }
  }
}

Now use bg-primary, text-primary, border-primary throughout your project.

Frequently Asked Questions

Do CSS variables work in all browsers?

CSS custom properties are supported in all modern browsers (Chrome 49+, Firefox 31+, Safari 9.1+, Edge 15+). IE11 doesn't support them — use a PostCSS plugin for fallbacks if needed.

Why include RGB values separately?

Some CSS properties need RGB format, like rgba() for transparency. Having --primary-rgb lets you write rgba(var(--primary-rgb), 0.5) without parsing the hex.

Can I nest CSS variables?

Yes, you can reference one variable in another: --dark-primary: hsl(var(--primary-h), var(--primary-s), 30%); This creates a darker variant using the same hue and saturation.

How do I use variables in JavaScript?

Read with getComputedStyle(document.documentElement).getPropertyValue('--primary'). Write with document.documentElement.style.setProperty('--primary', '#newColor').

What's the difference between this and Palette Export Tool?

Palette Export Tool offers more format options (JSON, SCSS, etc.). This tool focuses specifically on CSS variables with RGB fallbacks and HSL breakdowns for advanced theming.

Can I scope variables to specific elements?

Yes, define variables on any selector: .card { --card-bg: #fff; }. They'll only apply within that element and its children.

Tips for CSS Variable Organization

Use consistent naming. Pick a convention (--primary, --brand-primary, --color-primary) and stick with it across your project.

Group by purpose. Separate variables into categories: brand colors, UI colors, semantic colors (success, warning, error).

Document your variables. Add comments explaining what each variable is for. Future you (and your teammates) will thank you.

Start small. Don't create 100 variables on day one. Add variables as you identify repeated colors in your codebase.