CSS COLOR VARIABLES GUIDE
CSS Color Variable Generator – Turn Any Color Palette into CSS Custom Properties Online Free
A CSS color variable generator saves you from the slow, error-prone job of typing the same HEX codes again and again across a stylesheet. Instead of hard-coding #4F46E5 in fifty places, you define it once as a custom property, such as --primary, and reuse it everywhere with var(--primary). Our free CSS color variable generator takes the colors you pick, cleans up the names, converts the values to HEX, RGB or HSL, and writes a complete, well-formatted block of code that you can paste straight into your project. It can also build a full shade scale for every color from light tints to deep shades, create matching dark mode variables, add RGB channel variables for transparent colors, and export everything as CSS custom properties, SCSS variables, Less variables, a Tailwind color config or a JSON file for design tokens. There is nothing to install, no account to create and no limit on how many palettes you generate. Whether you are setting up a design system for a large web app, theming a WordPress or Laravel site, restyling a Bootstrap project or simply tidying the colors of a personal landing page, this tool turns a handful of colors into production-ready code in seconds.
:root
CSS Custom Properties Generator for Your :root
Generate a clean :root block of CSS custom properties from your brand colors. Every variable gets a tidy, lowercase, hyphenated name, an optional prefix and the color format you prefer, so your stylesheet stays consistent and easy to maintain.
50–900
Color Shade Scale and Dark Mode Variables
Create a 5-step or 10-step shade scale for each color, from a soft 50 tint to a deep 900 shade, and add dark mode variables with a single click. Choose an automatic prefers-color-scheme block or a manual data-theme toggle for your own theme switcher.
{ }
Export to SCSS, Less, Tailwind and JSON
Not every project uses plain CSS. Switch the output format to get SCSS variables, Less variables, a Tailwind theme colors object or JSON design tokens, then copy the code or download it as a ready-to-use file.
How to Use the CSS Color Variable Generator (Step by Step)
Creating a set of CSS color variables only takes a minute, and you do not need to write a single line of code yourself. Follow these steps to go from a few colors to a finished, copy-ready file.
-
Add and name your colors: Use the color box to pick each color or type its HEX code, then give it a meaningful name such as primary, secondary, accent or surface. Press Add color to include more.
-
Choose the output format: Select CSS for custom properties, SCSS or Less for preprocessor variables, Tailwind for a theme colors object, or JSON for design tokens.
-
Set a prefix and value format: Add an optional prefix like brand to avoid naming conflicts, and decide whether values should be written as HEX, RGB or HSL.
-
Turn on shades, dark mode or RGB channels: Enable a 5-step or 10-step shade scale, add dark mode variables, or switch on RGB channel variables if you need semi-transparent colors.
-
Copy or download: Check the live preview, then copy the generated code or download it as a file such as colors.css and drop it into your project.
The preview and the code update instantly after every change, so you can try different names, formats and shade settings until the output matches how your project is organised.
What Are CSS Color Variables (Custom Properties)?
CSS color variables, officially called CSS custom properties, are values that you define once and reuse throughout your stylesheet. A custom property always starts with two dashes, for example --primary: #4f46e5;, and is usually declared on the :root selector so that it is available to every element on the page. You then read the value with the var() function, for example color: var(--primary);. Unlike variables in a preprocessor such as Sass, CSS custom properties are live in the browser: they follow the cascade, they can be overridden inside a component or a theme class, and they can be changed with JavaScript while the page is running. That single feature is what makes theming, dark mode and user-selected accent colors so much easier than they used to be.
The var() function also accepts a fallback value, written as var(--primary, #4f46e5), which is used if the variable is missing. This makes component styles safer when they are shared between projects. The table below compares the syntax you will get from this generator in each output format.
Color variable syntax by output format
| Format |
Declaration |
Usage |
Best for |
| CSS |
--primary: #4f46e5; |
color: var(--primary); |
Any website, runtime theming, dark mode |
| SCSS |
$primary: #4f46e5; |
color: $primary; |
Sass projects, Bootstrap customisation |
| Less |
@primary: #4f46e5; |
color: @primary; |
Less based themes and older toolchains |
| Tailwind |
primary: '#4f46e5' |
class="text-primary" |
Tailwind CSS projects and utility classes |
| JSON |
"primary": "#4f46e5" |
Imported by scripts or token tools |
Design tokens, React Native, style dictionaries |
CSS Variables vs SCSS and Less Variables: Which Should You Use?
Many developers still ask whether they should keep using Sass variables now that native CSS custom properties are supported in every modern browser. The honest answer is that they solve different problems, and many projects use both. Preprocessor variables are replaced at build time, so the browser never sees them and they cannot change after the page loads. CSS custom properties exist at runtime, which means they can react to a dark mode switch, a user setting or a media query without recompiling anything. If you need theming or any kind of dynamic color, choose CSS variables. If you only need compile-time values for calculations, SCSS or Less variables are still fine.
CSS custom properties vs preprocessor variables
| Feature |
CSS custom properties |
SCSS / Less variables |
| When they are resolved |
In the browser, at runtime |
At build time, before the browser |
| Change with JavaScript |
Yes, with style.setProperty() |
No, values are already compiled |
| Dark mode and themes |
Easy, override inside a media query or a class |
Needs separate compiled stylesheets |
| Scoped to a component |
Yes, they follow the cascade |
Limited to file or block scope |
| Needs a build step |
No |
Yes |
Because this generator can output both, you can start with CSS custom properties for your live theme and export the same palette as SCSS variables if part of your codebase still depends on Sass.
How to Name CSS Color Variables: Best Practices and Naming Conventions
Good names make a stylesheet easier to read, easier to refactor and far easier to hand over to another developer. The biggest decision is whether to name a variable after what the color looks like or after what the color does. Names based on appearance, such as --blue or --light-grey, break down the moment the brand color changes from blue to green. Names based on purpose, such as --primary, --surface or --text-muted, survive a redesign because the meaning stays the same while the value changes. In larger design systems teams usually keep two layers: primitive variables that describe the raw palette, and semantic variables that assign roles to those colors.
Examples of good and weak CSS color variable names
| Weak name |
Better name |
Why it is better |
--blue |
--primary |
Still correct if the brand color changes |
--color1 |
--accent |
Describes the role, not the position |
--lightgrey2 |
--surface-muted |
Explains where the color is used |
--red |
--danger |
Matches the meaning of an error state |
--Brand_Blue |
--brand-primary |
Lowercase kebab-case is the common convention |
The generator automatically converts the names you type into lowercase kebab-case, removes spaces and symbols, and adds a numeric suffix if two colors end up with the same name, so you always get valid variable names. Use the prefix field to add a short namespace such as brand or ui, which prevents collisions when your styles share a page with a third-party widget or a CSS framework.
- Prefer roles over appearance: primary, secondary, accent, success, warning, danger, surface and text describe intent.
- Keep names short and predictable: A small, consistent vocabulary is easier for a whole team to remember.
- Use a prefix for shared pages: A prefix such as brand keeps your variables separate from other libraries.
- Avoid numbers without meaning: Names like color1 and color2 tell the next developer nothing.
Generate a Color Shade Scale (50 to 900) for Every CSS Variable
Most real interfaces need more than one version of a color. You need a very light tint for backgrounds and badges, a mid tone for buttons, and a dark shade for hover states, borders and text. A shade scale gives every color a ready-made ladder of values numbered from 50 to 900, the same system used by Tailwind CSS and Material Design. When you switch on the 10 shades option, the generator keeps the hue and saturation of your base color and moves only the lightness. The 500 step is always your exact base color, the lower numbers become lighter tints, and the higher numbers become darker shades.
How the shade steps are used in a typical interface
| Step |
Tone |
Common use |
| 50 – 100 |
Very light tint |
Page backgrounds, alerts, badges, table stripes |
| 200 – 300 |
Light |
Borders, dividers, disabled states, soft highlights |
| 400 – 500 |
Base range |
Primary buttons, links, icons, active states |
| 600 – 700 |
Dark |
Hover and pressed states, strong accents |
| 800 – 900 |
Very dark shade |
Headings, body text on light backgrounds, dark surfaces |
If you only need a few variations, choose the 5 shades option, which outputs steps 100, 300, 500, 700 and 900. This keeps the file short while still covering backgrounds, borders, the main color, hover states and text. Because the scale is calculated from your color, you get a palette that always matches your brand instead of a generic grey ramp.
Dark Mode with CSS Variables: prefers-color-scheme and data-theme
Dark mode is the best example of why CSS variables are so useful. Instead of writing a second copy of every rule, you redefine the same variables inside a dark block, and every element that uses var() updates automatically. The generator supports the two most common approaches. The Auto (OS) option wraps your dark values in a prefers-color-scheme: dark media query, so the site follows the visitor's device setting without any JavaScript. The Toggle option writes the dark values inside a [data-theme="dark"] selector, so you can add a theme switch button that sets that attribute on the html element and remembers the visitor's choice.
To create the dark values, the tool inverts the lightness of each color within a safe range and softens the saturation slightly, because fully saturated colors tend to vibrate and look harsh on dark backgrounds. Treat the result as a strong starting point and review it in your real interface, especially for brand colors that must match exact guidelines. In the SCSS, Less, Tailwind and JSON formats, the dark values are exported with a dark suffix or in a separate dark object, so they are easy to wire into your own theme logic.
- Auto (OS) mode: Best for simple sites that should follow the system theme automatically.
- Toggle mode: Best when you offer a theme switcher and store the choice in local storage.
- Test text contrast: Check body text and buttons in both themes so they stay readable.
- Set the color-scheme property: Adding color-scheme: light dark helps browsers style form controls and scrollbars correctly.
RGB Channel Variables for Transparent Colors and Opacity
A common limitation of CSS color variables appears when you need the same color at different opacities, for example a primary-colored shadow at 20 percent or a hover overlay at 8 percent. If your variable stores a complete HEX value, you cannot simply add transparency to it. The usual solution is to store the red, green and blue channels as a separate variable, and then build the final color with the modern syntax, such as background: rgb(var(--primary-rgb) / 0.2);. When you switch on RGB channel variables, the generator adds a companion variable such as --primary-rgb: 79 70 229; for every color, so you can mix any opacity without defining new colors. Many design systems rely on this pattern for shadows, focus rings, glass effects and overlays.
Modern browsers support the space-separated rgb() syntax with a slash for alpha, and it is the same approach used by Tailwind CSS when it applies opacity utilities to custom colors. If you must support very old browsers, you can switch the value format to RGB and write the alpha channel manually with the older rgba() function.
Use Your Generated Variables in Real Projects
Once you have the generated code, using it is straightforward. For a plain website, paste the :root block at the top of your main stylesheet, or save it as colors.css and link it before your other styles so that every rule can read the variables. In a Laravel application you can place the file in your resources or public folder and include it in your layout, or paste the block into the Blade layout inside a style tag. In React, Vue, Next.js or Nuxt projects, import the CSS file once in your entry point and use var() in your component styles, CSS modules or styled components. In WordPress, add the variables through your theme stylesheet or the additional CSS panel. For Bootstrap projects, export the SCSS variables and place them before the Bootstrap import to override its default theme colors. For Tailwind, paste the generated colors object into the extend section of your theme and use classes such as bg-primary and text-primary-700 straight away.
- Buttons and links: Use --primary for the main call to action and a darker shade for the hover state.
- Cards and backgrounds: Use the lightest tints for surfaces and subtle borders.
- Alerts and status colors: Define success, warning and danger variables once and reuse them in every component.
- JavaScript theming: Change a variable with document.documentElement.style.setProperty to update an accent color live.
Accessibility, Contrast and Common CSS Color Variable Mistakes
A well-organised palette is only useful if people can actually read it. The Web Content Accessibility Guidelines ask for a contrast ratio of at least 4.5 to 1 between normal text and its background, and at least 3 to 1 for large text and interface components. When you build your own pairs, test them with a contrast checker, especially for light shades on white backgrounds and mid-tone buttons with white text. Shade scales make this easier because you can move one step darker until the ratio passes, without leaving your brand hue.
- Defining colors in many places: Keep one source of truth in :root so a change updates the whole site.
- Naming by appearance: Names like --blue become wrong after a rebrand, so use roles such as --primary.
- Forgetting fallbacks: Use var(--primary, #4f46e5) in shared components that might load without your stylesheet.
- Overriding too deep: Redefine variables at a theme or section level instead of rewriting individual rules.
- Skipping contrast checks in dark mode: Always test text and buttons in both themes.
- Using too many colors: A small palette of a few roles with shade scales is easier to keep consistent than dozens of unrelated colors.
Browser Support and Performance of CSS Custom Properties
CSS custom properties have been supported by all major browsers for years, including Chrome, Edge, Firefox, Safari and their mobile versions. Only very old browsers such as Internet Explorer 11 lack support. Performance is rarely a concern: reading a variable is extremely fast, and defining your palette once on :root actually keeps stylesheets smaller than repeating raw values. For the best results, keep your color variables in a single, early-loading file and avoid deeply nested overrides that make the source of a value hard to trace.
CSS var() Function: Syntax and Fallback Values
You declare a custom property with two hyphens, such as --color-primary: #4f46e5;, and read it with var(--color-primary). The function accepts a second value as a fallback, as in var(--color-accent, #f59e0b), which is used when the first variable is not defined. Variable names are case sensitive, so --Brand and --brand are two different properties.
A fallback does not repair every mistake. If a variable exists but holds a value that makes no sense for the property, the browser does not jump to the fallback. Check names and values with the developer tools when a color goes missing.
Global and Scoped CSS Color Variables
Variables declared on :root are available across the document. You can also redefine them on a smaller selector to theme one part of a page. For example, a .pricing-card rule can set a new value for --color-primary, and every child of that card that reads the variable picks up the new color.
- Global tokens: Brand, text, background and border colors used everywhere.
- Section themes: A promotion banner or footer that swaps a few values.
- Component variables: A button that reads its own variable, which defaults to a global one.
Design Tokens: Primitive, Semantic and Component Colors
Larger design systems often arrange color variables in layers. The numbered values from a shade scale work well as the first layer, and you can point meaningful names at them.
Three layers of CSS color design tokens
| Layer | Example | What it holds |
| Primitive | --primary-600 | A fixed color value from the scale |
| Semantic | --button-bg | The role, pointing to a primitive |
| Component | --card-button-bg | A local override for one component |
A link between layers looks like --button-bg: var(--primary-600);. Adjust the name to match the prefix and color names you chose in the generator. A small site may only need one or two layers.
How to Change CSS Variables With JavaScript
Custom properties stay live in the browser, so a script can change them without rebuilding your stylesheet. Use document.documentElement.style.setProperty('--color-primary', '#16a34a'); to set a value, and getComputedStyle(document.documentElement).getPropertyValue('--color-primary') to read it. This is useful for theme pickers, white label dashboards and live previews. Validate any color that comes from user input before applying it.
How to Migrate Hard-Coded Colors to CSS Variables
- List the HEX, RGB and HSL values already used in your stylesheet.
- Group near duplicates and decide which value each group should keep.
- Add the chosen colors to the generator and give each a clear name.
- Paste the output at the top of your main stylesheet.
- Replace old values one group at a time and review the affected pages after each step.
Avoid a blind find and replace across third party files. The same HEX code can serve different roles, and each role may deserve its own variable.
Free CSS Color Variables Generator for Any Device
This CSS variables generator works in your web browser on Windows, Mac, Linux, Android and iPhone, so you can build a color system wherever you work. There is no signup, no watermark and no usage limit, and because the code is generated on your device, your palette is never sent to a server. Whether you need a quick set of CSS root variables for a client site, a SCSS color file for a Sass project, a Tailwind custom colors config or JSON design tokens for a design system, you can generate, review and copy the result in a few seconds.
Ready to try it? Scroll back up, add your brand colors, choose your format and copy your new color variables. If you are working on other design and developer tasks, explore the rest of the free tools on our site.