CSS to LESS Converter
Convert CSS to LESS online with automatic selector nesting, color variables, vendor prefix mixins, and customizable formatting options.
About
Raw CSS files grow unmanageable past a few hundred lines. Duplicate color values scatter across selectors, vendor-prefixed properties triple your line count, and flat selector lists obscure the DOM hierarchy they target. This converter parses your CSS into an abstract syntax tree, then reconstructs it as valid LESS with nested selectors, extracted @color variables, and vendor-prefix mixins. The nesting algorithm computes the longest common selector prefix chain so .nav, .nav ul, and .nav ul li collapse into a single nested block. Color extraction uses deduplication: if #3498db appears 12 times, it becomes one @color-1 declaration. The tool approximates optimal nesting assuming well-structured selectors. Deeply combinator-mixed selectors (e.g., div > .a + .b ~ .c) may produce verbose nesting where manual grouping would be cleaner.
Formulas
The conversion pipeline applies three transformations in sequence. First, selector nesting groups flat CSS rules into a tree hierarchy.
where each selector Si is split by whitespace and combinator tokens into segments. The trie merges common prefixes so that .a .b and .a .c share the .a node. Pseudo-selectors (:hover, ::after) are detected via leading : and rendered with the & parent reference operator.
Second, color variable extraction scans all property values with the pattern:
Each unique color is assigned @color-N where N ∈ N. All occurrences are replaced with the variable reference.
Third, vendor mixin generation detects property groups where the base property name (stripped of prefix) matches across 2+ vendor prefixes from the configured list. These collapse into a parametric mixin:
Reference Data
| Option | Type | Default | Description |
|---|---|---|---|
| indentSize | Number | 1 | Number of indent symbols per nesting level |
| indentSymbol | String | \t (tab) | Character used for indentation: tab or spaces |
| selectorSeparator | String | ,\n | Separator between grouped selectors |
| blockFromNewLine | Boolean | FALSE | Place opening { on a new line (Allman style) |
| blockSeparator | String | \n | Whitespace between rule blocks |
| updateColors | Boolean | FALSE | Extract color literals into @color-N variables |
| vendorMixins | Boolean | TRUE | Collapse vendor-prefixed properties into LESS mixins |
| vendorPrefixesList | Array | -moz, -o, -ms, -webkit | Vendor prefixes to detect and collapse |
| LESS Language Features Used | |||
| Nesting | Child selectors become nested blocks: .parent { .child { } } | ||
| Variables | Repeated values extracted to @var: value; at file top | ||
| Mixins | Vendor groups become .mixin(@val) { -webkit-prop: @val; prop: @val; } | ||
| & Operator | Pseudo-classes/elements use &:hover, &::before | ||
| Comments | Original CSS comments preserved in output | ||
| Common CSS Constructs Handled | |||
| @media | Media queries preserved as top-level blocks with nested content | ||
| @keyframes | Keyframe blocks passed through without nesting modification | ||
| @font-face | Font-face declarations preserved as-is | ||
| @import | Import statements hoisted to file top | ||
| @charset | Charset declarations hoisted to first line | ||
| @supports | Feature queries preserved as top-level blocks | ||