The CSS Formatter is a free browser-based tool that formats, validates, and minifies CSS, SCSS, and Less stylesheets without sending data to any server. It is powered by Prettier's PostCSS parser, supports 2-space and 4-space indentation, detects syntax errors with exact line and column positions, and handles native CSS nesting, CSS custom properties, and @layer rules.
CSS Formatter & Beautifier
How it works
Last updated: November 2026CSS (Cascading Style Sheets) controls the visual presentation of web pages — defining layout, colors, fonts, spacing, and animations for every HTML element. This formatter uses Prettier (with the PostCSS parser) to format your stylesheets entirely in your browser — nothing is ever sent to a server.
Paste any CSS, SCSS, or Less into the input panel and click Beautify to re-indent and normalize the formatting. The Syntax selector tells the formatter which dialect to use: CSS uses the standard PostCSS parser, SCSS supports $variables and nesting, and Less supports @variables and mixins. Beautify rewrites the input box with the formatted output.
Minify strips all whitespace and comments to produce the most compact stylesheet possible and writes the result to the output panel only — the input is never changed. Use the minified output for production deployments to reduce file size.
When your input contains a syntax error, the status bar shows the exact line and column number of the error and highlights that line in the input gutter so you can jump straight to the problem.
SCSS and Less stylesheets must be compiled to plain CSS before browsers can use them. This tool formats the source syntax — it does not compile SCSS to CSS. Use sass or a bundler like Vite or webpack for compilation.
CSS Quick Reference
Common properties
| Property | Example value | Purpose |
|---|---|---|
| display | flex / grid / block | Layout model for the element |
| position | relative / absolute / fixed | Positioning scheme |
| flex | 1 / 0 0 auto | Flex grow, shrink, and basis shorthand |
| align-items | center / stretch / flex-start | Cross-axis alignment in flex/grid |
| justify-content | space-between / center | Main-axis distribution in flex/grid |
| grid-template-columns | repeat(3, 1fr) | Define column tracks in a grid |
| gap | 1rem / 0.5rem 1rem | Space between flex/grid items |
| margin / padding | 1rem / 0.5rem 1rem | Outer / inner spacing |
| color | #3b82f6 / rgb(59 130 246) | Text color |
| background | white / linear-gradient(…) | Background color or image |
| border | 1px solid #e2e8f0 | Border shorthand (width style color) |
| border-radius | 0.375rem / 50% | Rounded corners |
| font-size | 1rem / clamp(1rem, 2.5vw, 1.5rem) | Text size |
| @media | (max-width: 768px) | Conditional styles for screen sizes |
Box model
Every element is a rectangular box. From inside out: content → padding → border → margin.
Set box-sizing: border-box so width and height include padding and border — the modern default used by most CSS resets.
Selector specificity (low → high)
| Selector type | Example | Specificity |
|---|---|---|
| Element / pseudo-element | p, ::before | 0-0-1 |
| Class / attribute / pseudo-class | .btn, [type], :hover | 0-1-0 |
| ID selector | #header | 1-0-0 |
| Inline style | style="…" | 1-0-0-0 |
| !important | color: red !important | Overrides all |
CSS vs SCSS vs Less
SCSS and Less add programming features on top of CSS — but both must be compiled before browsers can use them.
| Feature | CSS | SCSS | Less |
|---|---|---|---|
| Variables | --color: blue | $color: blue | @color: blue |
| Nesting | CSS Nesting (new) | .parent { .child { } } | .parent { .child { } } |
| Mixins | Not supported | @mixin / @include | .mixin() |
| Functions | calc() / var() | darken() / lighten() | darken() / lighten() |
| Imports / partials | @import (network) | @use / @forward | @import |
| Compile step | None — native | Required (sass) | Required (lessc) |
| Variables live in browser | Yes (custom props) | Compiled away | Compiled away |
CSS custom properties
/* CSS Custom Properties (variables) */
:root {
--color-primary: #3b82f6;
--color-secondary: #64748b;
--spacing-sm: 0.5rem;
--spacing-md: 1rem;
--spacing-lg: 2rem;
--font-size-base: 1rem;
--border-radius: 0.375rem;
}
.button {
background-color: var(--color-primary);
padding: var(--spacing-sm) var(--spacing-md);
border-radius: var(--border-radius);
font-size: var(--font-size-base);
} SCSS variables + nesting
// SCSS variables
$color-primary: #3b82f6;
$color-secondary: #64748b;
$spacing-sm: 0.5rem;
$spacing-md: 1rem;
// SCSS nesting
.nav {
display: flex;
gap: $spacing-md;
&__item {
color: $color-secondary;
&:hover {
color: $color-primary;
}
&--active {
font-weight: bold;
color: $color-primary;
}
}
} Tips & Tricks
SCSS nesting compiles away — format to see the structure
Formatted SCSS shows deeply nested selectors clearly, making it easy to audit the compiled specificity before your build step flattens it. Paste SCSS and click Beautify to see the canonical nesting structure Prettier recommends.
Minification strips comments — keep source maps in production
Minified CSS has no comments or whitespace, making browser DevTools hard to read. Always generate source maps (--source-map flag in sass, or your bundler config) so DevTools can map minified output back to your source files.
CSS custom properties are preserved by the formatter
Unlike SCSS $variables which are compiled away, CSS custom properties (--color-primary: red) survive in the output and can be changed at runtime with JavaScript or overridden inside @media blocks for dark mode.
Use box-sizing: border-box universally
Adding *, *::before, *::after { box-sizing: border-box; } to every project means width: 300px is always exactly 300px regardless of padding or border. Without it, padding expands the element beyond the stated width, causing layout surprises.
Prefer logical properties for international layouts
Use margin-inline-start instead of margin-left and padding-block instead of padding-top/bottom. Logical properties respect the document's writing direction, automatically adapting to right-to-left languages like Arabic or Hebrew without extra overrides.
Switch syntax before Beautify — not after
Set the Syntax selector (CSS / SCSS / Less) before clicking Beautify. If you paste SCSS but format with the CSS parser, Prettier will report a syntax error on SCSS-only features like $variables or @mixin.
Using CSS Formatters in Code
How to format and process CSS programmatically in the most common environments.
Node.js — Prettier
Install: npm install prettier
import * as prettier from 'prettier';
// Format CSS
const formattedCSS = await prettier.format(cssString, {
parser: 'css',
tabWidth: 2,
});
// Format SCSS
const formattedSCSS = await prettier.format(scssString, {
parser: 'scss',
tabWidth: 2,
});
// Format Less
const formattedLess = await prettier.format(lessString, {
parser: 'less',
tabWidth: 2,
}); Sass CLI — compile SCSS to CSS
Install: npm install -g sass or brew install sass/sass/sass
# Compile SCSS to CSS (expanded output) sass input.scss output.css --style=expanded # Compile SCSS to CSS (compressed/minified) sass input.scss output.css --style=compressed # Watch for changes and recompile sass --watch src/styles:dist/css # Compile all .scss files in a directory sass src/styles/ dist/css/
PostCSS CLI — transform and optimize
PostCSS is a plugin-based CSS processor used by Tailwind CSS, Autoprefixer, and cssnano.
# Install PostCSS CLI npm install -D postcss postcss-cli # Basic usage npx postcss input.css --output output.css # With autoprefixer (adds vendor prefixes) npm install -D autoprefixer npx postcss input.css --use autoprefixer --output output.css # With cssnano (minification) npm install -D cssnano npx postcss input.css --use cssnano --output output.min.css
Frequently Asked Questions
- What is CSS?
- CSS (Cascading Style Sheets) is a stylesheet language that controls the visual presentation of web pages. CSS rules describe how HTML elements should be displayed — including layout, colors, fonts, spacing, and animations. It is one of the three core web technologies alongside HTML and JavaScript, and is supported natively by every browser without compilation. CSS was first proposed by Håkon Wium Lie in 1994 and standardized as CSS1 in 1996 by the W3C. The language is defined in modular specifications — CSS Color Level 4, CSS Flexbox, CSS Grid, and others — each progressing independently. Browsers implement these modules incrementally, which is why some features like @layer (Chrome 99+) have different support timelines. CSS is the only way to style HTML on the web; JavaScript-in-CSS approaches like CSS-in-JS ultimately generate and inject standard CSS rules at runtime.
- What is the difference between CSS, SCSS, and Less?
- CSS is the native stylesheet language browsers understand directly — no compilation step required. SCSS (Sassy CSS) is a superset of CSS that adds
$variables, nesting,@mixinand@include, and@extend. Less is a similar preprocessor that uses@variablesyntax and JavaScript-based compilation. Both must be compiled to plain CSS by a build tool (the sass CLI, webpack sass-loader, or Vite's built-in SCSS support) before a browser can use them. SCSS is more widely adopted today — it is the default in Angular projects and is used by Bootstrap. With the introduction of native CSS custom properties, CSS nesting (2023), and@layer, many SCSS features now have direct CSS equivalents, reducing the need for a preprocessor in new projects. - Why should I format my CSS?
- Formatted CSS is easier to read, review in pull requests, and maintain. Consistent indentation and property ordering makes it faster to spot missing semicolons, mismatched braces, or duplicate property declarations. Prettier (which powers this formatter) applies an opinionated, deterministic style that eliminates formatting debates in teams and produces consistent results in every editor and CI pipeline. For example, Prettier always puts each property on its own line, normalizes whitespace around colons and semicolons, and uses consistent quote style in attribute selectors. Teams can add prettier --check to their CI pipeline to enforce consistent formatting on every pull request. For more comprehensive CSS linting (catching deprecated properties, invalid values, or naming conventions), Stylelint provides rules beyond formatting that complement Prettier's purely structural approach.
- What is CSS minification?
- CSS minification removes all whitespace, newlines, and comments from a stylesheet without changing its behavior. Minified CSS reduces the file size browsers must download, improving page load time. Always keep the readable source file and minify only the version deployed to production. Advanced minifiers like cssnano (used by PostCSS and most build tools) go beyond whitespace removal — they also merge duplicate selectors, remove redundant shorthand expansions, and convert color values to shorter forms (e.g. #ffffff to #fff). Combining CSS minification with gzip or Brotli compression on the server provides the best total reduction — CSS compresses extremely well due to repetitive property names. Minification strips all comments, so source maps (generated by your bundler) are essential for debugging minified production CSS in browser DevTools. The sass CLI produces minified output with --style=compressed.
- What are CSS custom properties?
- CSS custom properties (also called CSS variables) let you define reusable values with a double-dash prefix:
--color-primary: #3b82f6;declared in:root, and referenced withvar(--color-primary). Unlike SCSS or Less variables which are compiled away before the browser sees them, custom properties are live in the browser — they participate in the CSS cascade, can be overridden in a narrower scope (inside a media query or on a specific element), and can be changed at runtime with JavaScript using element.style.setProperty('--color-primary', '#new-value'). This enables theming, dark mode toggling, and animation of values that otherwise cannot be animated. Custom properties are supported in all modern browsers since Chrome 49 (2016) and are now the preferred approach for design tokens in CSS design systems. The var() function also accepts a fallback:var(--color-primary, blue). - What is a media query?
- A CSS media query applies styles conditionally based on device or viewport characteristics. The most common use is responsive design:
@media (max-width: 768px) { ... }applies styles only when the viewport is 768 px wide or narrower. Modern CSS supports a rich set of media features:@media (prefers-color-scheme: dark)for automatic dark mode support,@media (prefers-reduced-motion: reduce)for users who have requested reduced animation (important for accessibility),@media (orientation: landscape)for device orientation, and@media printfor print-specific styles. The CSS Media Queries Level 4 specification (supported in all modern browsers) added range syntax:@media (600px <= width <= 1200px)as a more readable alternative to min-width and max-width. Container queries (@container) are a newer feature that queries the parent container size instead of the viewport. - What is CSS specificity?
- CSS specificity is the algorithm browsers use to decide which rule wins when multiple conflicting rules target the same element. Specificity is calculated as a three-column score: (ID selectors, class/attribute/pseudo-class selectors, element/pseudo-element selectors). An ID selector (
#header) beats any number of class selectors (.nav) because (1,0,0) always beats (0,99,0). Inline styles have an even higher specificity of (1,0,0,0) and beat all external stylesheet rules. The!importantdeclaration overrides specificity entirely and creates a parallel "important layer" — it should be used sparingly because it makes styles extremely hard to override and debug. The:where()pseudo-class is a modern CSS feature that always has zero specificity, useful for writing low-specificity base styles. The new CSS@layerfeature provides a better alternative to !important for managing specificity in large codebases. - What is the CSS box model?
- The CSS box model describes how every HTML element is rendered as a rectangular box with four layers from inside out: content (text or image), padding (space between content and border, inheriting the element's background), border (the element's visible border), and margin (transparent space outside the border separating it from neighboring elements). By default,
box-sizing: content-boxmeanswidthandheightapply only to the content area — adding padding makes the element visually larger than the stated width, causing layout surprises. Settingbox-sizing: border-boxincludes padding and border in the stated dimensions, which is the modern standard: a 300px wide element stays 300px wide regardless of padding. Most modern CSS resets (including Tailwind's preflight) applybox-sizing: border-boxglobally via the universal selector. Margins also collapse vertically between adjacent block elements, which can be counterintuitive. - What is the difference between padding and margin?
- Padding is the space between an element's content and its border — it is inside the element, inherits the element's background color, and expands the clickable or hoverable interactive area of a button or link. Margin is the space outside the border that separates the element from its neighbors — it is always transparent and does not expand the interactive area. A critical difference: vertical margins between adjacent block elements collapse to the larger of the two values (called margin collapse), but padding never collapses. For example, if two paragraphs each have margin-bottom: 20px and margin-top: 15px, the gap between them is 20px, not 35px. This behavior does not apply to flex or grid children, only to block-level flow layout. Horizontal margins never collapse. When in doubt about spacing, use padding for internal spacing and gap (in flex/grid) for spacing between items.
- What is flexbox?
- Flexbox (Flexible Box Layout) is a CSS layout model that arranges items along a single axis — either row or column. Setting
display: flexon a container makes its direct children flex items that can grow, shrink, and align automatically. Key container properties:flex-direction(row or column),justify-content(main-axis alignment: flex-start, center, space-between),align-items(cross-axis alignment),flex-wrap(whether items wrap to new lines), andgap(spacing between items). Key item properties:flex-grow,flex-shrink, andflex-basis(combined as the shorthandflex: 1). Flexbox is ideal for navigation bars, button groups, card rows, and centering content both horizontally and vertically. For two-dimensional grid layouts with explicit rows and columns, use CSS Grid instead. - Does the formatter validate my CSS?
- Yes. The formatter uses Prettier's PostCSS parser, which validates CSS syntax as it formats. If your CSS contains a syntax error — missing closing brace, invalid at-rule, malformed selector — the status bar shows the exact line and column number and highlights that line in the input gutter. Note that Prettier validates syntax only; it does not check whether property names or values are recognized by any specific browser version. A syntactically valid CSS property like
color-scheme: darkpasses formatting without issue even if it is unsupported in older browsers. For browser compatibility checks, caniuse.com is the authoritative reference. For stricter CSS linting including unknown properties, deprecated values, and naming convention enforcement, use Stylelint (npm install -D stylelint), which complements Prettier's purely structural formatting. - Is my CSS sent to a server?
- No. All formatting, minification, and validation happen entirely in your browser using JavaScript. Prettier runs client-side via its standalone build — the same Prettier that you install with npm is compiled to a single browser-compatible bundle that runs entirely in the JavaScript engine. Your stylesheet never leaves your machine. This matters when working with CSS that may contain internal design tokens, proprietary component names, or branding information your team keeps confidential. No network requests are made when you click Beautify or Minify — you can verify this by opening your browser's Network DevTools tab while formatting. The only network request the tool makes is the initial page load; your actual CSS input is never transmitted.
- What is CSS Grid?
- CSS Grid is a two-dimensional layout system that arranges items into rows and columns simultaneously. Setting
display: gridon a container lets you define column tracks withgrid-template-columns(e.g.repeat(3, 1fr)for three equal columns), row tracks withgrid-template-rows, and gaps withgap. Child elements can span multiple cells usinggrid-column: 1 / 3andgrid-row: 1 / 2, or be named withgrid-template-areasfor a visual layout definition. Theauto-fillandauto-fitkeywords inrepeat()create responsive grids that reflow without media queries. CSS Grid is best for page-level layouts and structured component grids (like a photo gallery or dashboard). Flexbox is better for single-axis arrangements. The two are frequently used together — a Grid outer layout with Flexbox components inside each cell. - What is the difference between CSS Grid and Flexbox?
- Flexbox is one-dimensional: it arranges items along a single axis (row or column) and lets items determine their own size and wrap naturally. CSS Grid is two-dimensional: you explicitly define row and column tracks, and items are placed into those cells — either automatically or explicitly. A practical guide: use Flexbox when you have a list of items that should flow in one direction and size themselves based on their content (navigation bars, button groups, centering a single item). Use Grid when you need items to align to both a horizontal and vertical structure simultaneously (page layouts, card grids, form layouts, dashboard panels). Both are supported in every modern browser. The most common pattern is an outer Grid for page structure and Flexbox for internal component layout. Neither Flexbox nor Grid replaces the other — they are complementary tools designed for different layout problems.
- What is the CSS @layer rule?
@layer(Cascade Layers, supported in Chrome 99+, Firefox 97+, Safari 15.4+) lets you explicitly control the specificity order of your CSS without relying on selector weight or source order. Styles in a later-declared layer override styles in an earlier layer regardless of selector specificity:@layer base, components, utilities;means anything inutilitieswins overcomponentseven ifcomponentsuses an ID selector. Unlayered styles are treated as the highest-priority implicit layer. This feature solves the specificity arms race common in large projects, third-party style conflicts, and design systems — you can safely import a third-party stylesheet into a low-priority layer without worrying that its high-specificity selectors will override your styles. Tailwind CSS v4 uses@layerinternally to separate base, component, and utility styles. Prettier formats@layerblocks correctly when you select the CSS dialect.- Does the CSS formatter support CSS nesting?
- Yes. CSS nesting — the native W3C feature that lets you write
.parent { .child { } }and&:hover { }directly in CSS without a preprocessor — was finalized in the CSS Nesting specification and is supported in all major browsers as of Chrome 112, Firefox 117, and Safari 16.5 (2023-2024). Native CSS nesting requires the nested selector to either start with a symbol (&, ., #, :, @, >, ~, +, *) or use the&combinator for element selectors. This is a slight difference from SCSS, where bare element selectors likep { span { } }work directly. Prettier's PostCSS parser handles native CSS nesting correctly when you select the CSS dialect. For SCSS-specific nesting features like the&parent selector suffixed with BEM modifiers (&__element,&--modifier), select the SCSS dialect instead.
Related Tools
Other free developer tools available on DevFormatter.
JSON Formatter
Format, validate, minify, and sort JSON. Detects duplicate keys and converts to YAML.
YAML Formatter
Format, validate, and convert YAML. Fix YAML 1.1 coercion risks for Docker and GitHub Actions.
XML Formatter
Format, validate, and convert XML to JSON. All processing runs in your browser.
SQL Formatter
Format and beautify SQL queries. Supports MySQL, PostgreSQL, SQLite, T-SQL and more.
Markdown Preview
Preview Markdown as rendered HTML. GitHub Flavored Markdown, tables, code blocks.