CSS Nesting ↔ Flat Converter
Convert CSS in both directions: expand nested CSS (the native & parent-selector syntax) into flat, legacy-compatible rules — or group flat CSS rules that share a common parent selector into a nested block. Useful when adopting CSS nesting gradually, auditing what native nesting outputs, or migrating away from Sass.
How to use the CSS Nesting ↔ Flat Converter
1. Choose the direction: Nested → Flat expands native CSS nesting blocks (using &) into separate flat rules; Flat → Nested groups rules that share a common selector prefix into nested blocks. 2. Paste your CSS into the input textarea. 3. Click Convert (or use the Example button to load sample input). The converted CSS appears in the output. The converter handles & as parent selector, compound selectors, pseudo-classes, pseudo-elements, descendant combinators, and nested @media queries. Complex edge cases like :is() with arbitrary selectors may need manual review.
Native CSS nesting — what it is and how & works
CSS nesting (CSS Nesting Module Level 1, Baseline 2024) lets you write rules inside other rules, with & representing the parent selector. This mirrors Sass/SCSS nesting but is native to the browser — no preprocessor required. A nested rule like .card { &:hover { color: blue; } } is exactly equivalent to .card:hover { color: blue; } as a flat rule.
The & token is required in some positions (e.g., pseudo-elements: &::before) and optional in others (a bare selector inside a rule is implicitly treated as a descendant). Nested @media queries are also supported: .card { @media (max-width: 600px) { font-size: .9rem; } } — they expand to the equivalent flat query wrapping the parent selector.
Reasons to convert: you may want to audit what your nesting expands to, ship a CSS bundle to browsers that do not support nesting (older Safari, older Chrome for WebViews), or migrate from a Sass project that has established flat-CSS conventions. The flat-to-nested direction helps when refactoring a large flat stylesheet into a more maintainable nested structure.
Common use cases
- Legacy-browser bundles — expand native nesting to flat CSS for a compatibility bundle served to older browsers or WebViews that do not support nesting.
- Sass migration — convert existing Sass nesting habits to native CSS nesting syntax (without the Sass-specific ampersand-only requirement in all positions).
- Code review and diffing — flatten a nested stylesheet to see the equivalent flat selectors clearly before reviewing specificity or debugging cascade issues.
- Refactoring flat stylesheets — group repetitive flat rules that share a parent into nested blocks to reduce repetition and improve readability.
- Learning and exploration — understand exactly what native nesting produces so you can reason about specificity correctly.