DevFormatter
CSS Formatter
 

CSS Formatter & Beautifier

How it works

CSS (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
displayflex / grid / blockLayout model for the element
positionrelative / absolute / fixedPositioning scheme
flex1 / 0 0 autoFlex grow, shrink, and basis shorthand
align-itemscenter / stretch / flex-startCross-axis alignment in flex/grid
justify-contentspace-between / centerMain-axis distribution in flex/grid
grid-template-columnsrepeat(3, 1fr)Define column tracks in a grid
gap1rem / 0.5rem 1remSpace between flex/grid items
margin / padding1rem / 0.5rem 1remOuter / inner spacing
color#3b82f6 / rgb(59 130 246)Text color
backgroundwhite / linear-gradient(…)Background color or image
border1px solid #e2e8f0Border shorthand (width style color)
border-radius0.375rem / 50%Rounded corners
font-size1rem / 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-elementp, ::before0-0-1
Class / attribute / pseudo-class.btn, [type], :hover0-1-0
ID selector#header1-0-0
Inline stylestyle="…"1-0-0-0
!importantcolor: red !importantOverrides 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.
What is the difference between CSS, SCSS, and Less?
CSS is the native stylesheet language browsers understand directly. SCSS (Sassy CSS) and Less are preprocessor languages that add variables, nesting, mixins, and functions — features that make large stylesheets easier to write and maintain. Both must be compiled to plain CSS before a browser can use them. SCSS uses $variable syntax; Less uses @variable syntax. SCSS is more widely adopted in modern 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 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.
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. Minification strips all comments, so source maps are important for debugging in browser DevTools.
What are CSS custom properties?
CSS custom properties (CSS variables) let you define reusable values with a double-dash prefix: --color-primary: #3b82f6;, and reference them with var(--color-primary). Unlike SCSS or Less variables which are compiled away, custom properties are live in the browser — they can be changed at runtime with JavaScript, can be overridden inside @media queries (e.g. for dark mode), and respect the CSS cascade.
What is a media query?
A CSS media query applies styles conditionally based on device 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 also supports @media (prefers-color-scheme: dark) for dark mode and @media (prefers-reduced-motion: reduce) for accessibility.
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 scored as (IDs, classes/attributes/pseudo-classes, elements/pseudo-elements). An ID selector (#header) beats any number of class selectors (.nav). Inline styles beat all stylesheet rules. The !important declaration overrides specificity entirely and should be used sparingly, as it makes styles very hard to override later.
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 (transparent space between content and border), border, and margin (transparent space outside the border). By default, box-sizing: content-box means width applies only to the content area. Setting box-sizing: border-box includes padding and border in the stated dimensions, which is the modern default in most CSS resets.
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 and inherits the element's background color, and it expands the clickable/hoverable area. 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 key difference: vertical margins between adjacent block elements collapse to the larger value (margin collapse), but padding never collapses.
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: flex on a container makes its direct children flex items that can grow, shrink, and align automatically. Key properties: flex-direction, justify-content (main-axis), align-items (cross-axis), flex-wrap, and gap. Flexbox is ideal for navigation bars, card rows, and centering content. For two-dimensional layouts, 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, 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.
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. Your stylesheet never leaves your machine.

Related Tools

Other free developer tools available on DevFormatter.