CSS Variable Extractor
Extract all CSS custom property declarations from a CSS file and list them.
Input
Result
CSS Variable Extractor
The CSS Variable Extractor is a stylesheet analysis utility designed to identify and extract CSS custom property (variable) declarations from a CSS block. Design systems, theme files, and modern stylesheets utilize CSS variables to store colors, fonts, margins, and sizing configurations. This tool automates the regex scanning process, preventing manual search errors. Developers and designers paste CSS code, and the parsing engine extracts the variable names and values, compiling a clean list instantly.
CSS Custom Properties Explained
CSS variables, officially called custom properties, represent author-defined properties that store specific values. They are declared using a double-dash prefix (e.g. --primary-color: #3498db;) and can be accessed throughout the stylesheet using the var() function. This allows centralizing design values, making updates simple.
According to frontend engineering standards, there are 4 distinct structural properties that govern CSS variable declarations. First, variables must begin with two hyphens (--). Second, they are declared within a CSS selector block (usually the :root selector to provide global scope). Third, each declaration consists of a key and a value separated by a colon, ending with a semicolon. Fourth, the variable names can contain letters, numbers, and hyphens. Extraction engines apply these rules to locate variables within complex CSS files.
The History of CSS Custom Properties
In the early days of web development, CSS lacked native variable support. To manage colors and design tokens, developers relied on preprocessors like Sass (with $variables) or Less (with @variables), which compiled down to static CSS values. W3C standardized CSS custom properties in 2015, introducing native runtime variables to the browser. This allowed changing variable values dynamically using JavaScript (e.g. for dark mode toggles), establishing a critical requirement for developers to extract and document these variables easily.
How the CSS Variable Extractor Works
To extract variables, paste the CSS stylesheet content and run the extractor. The parsing engine processes the stylesheet through a 3-step sequence.
- Syntax Analysis: The engine scans the input CSS text, skipping comments and whitespace blocks to locate property declarations.
- Regex Matching:
- The engine runs a regular expression match designed to locate variable declarations:
/--([w-]+)s*:s*([^;} ]+)/g. - This extracts the variable name (e.g.
primary-color) and the assigned value (e.g.#3498db).
- The engine runs a regular expression match designed to locate variable declarations:
- Document Generation: The engine compiles the matches into a structured list, sorting variables and formatting them with clean declarations.
For example, pasting a theme block containing primary colors and font styles extracts each declaration into a separate row. The tool displays this result instantly.
CSS Variable Extraction Reference Table
The table below displays sample extractions from standard CSS inputs.
| CSS Input Code Block | Extracted Variable Names | Extracted Values | Standard Variable Output Format | Design Token Category |
|---|---|---|---|---|
--main-bg: #ffffff; |
main-bg | #ffffff | --main-bg: #ffffff |
Color token (Backgrounds) |
--font-size-lg: 1.25rem; |
font-size-lg | 1.25rem | --font-size-lg: 1.25rem |
Typography token (Sizing) |
--spacing-md: 16px; |
spacing-md | 16px | --spacing-md: 16px |
Layout token (Margins/Padding) |
--box-shadow: 0 4px #000; |
box-shadow | 0 4px #000 | --box-shadow: 0 4px #000 |
Visual effect token (Shadows) |
Frequently Asked Questions
Why are my Sass ($) variables not extracted?
This tool extracts native CSS custom properties starting with a double dash (--). Sass variables are processed during compilation and do not exist in the final CSS file.
Can this tool extract variables declared inside media queries?
Yes, the regex scanner searches the entire CSS content regardless of media queries or nested selectors. It extracts all variables found in the style block.
Why do CSS variables require a :root selector?
Declaring variables inside the :root selector makes them globally accessible. They can be inherited by all child elements on the page.
Audit Your Design Tokens Instantly
Manual searching through thousands of lines of CSS files is slow and prone to overlooking declarations. The CSS Variable Extractor delivers reliable, instant lists. Use this tool to audit design systems, generate theme documentation, and verify style sheets easily.