Skip to main content

Overview

The CSS beautifier is available as a named export from the js-beautify package:
const beautify = require('js-beautify').css;
const beautify = require('js-beautify').css_beautify; // legacy alias
For ESM imports:
import beautify from 'js-beautify';

beautify.css(code, options);

Function signature

beautify.css(sourceCode: string, options?: object): string

Parameters

sourceCode
string
required
The CSS source code string to beautify.
options
object
Configuration options object. All keys use underscores.

Return value

result
string
The beautified CSS source code as a string.

Examples

const beautifyCss = require('js-beautify').css;

const ugly = '.container{display:flex;flex-direction:row;}.item{color:red;margin:0}';

const pretty = beautifyCss(ugly, {
  indent_size: 2,
  selector_separator_newline: true,
});

console.log(pretty);
// .container {
//   display: flex;
//   flex-direction: row;
// }
//
// .item {
//   color: red;
//   margin: 0
// }
The CSS beautifier is simpler in scope than the JavaScript beautifier and has fewer options. For HTML and JavaScript inside CSS (e.g., in <style> blocks), use the HTML beautifier with embedded language support.

Language-specific overrides

When loading from .jsbeautifyrc, you can scope CSS settings under the css key:
{
  "indent_size": 4,
  "css": {
    "indent_size": 2,
    "newline_between_rules": true
  }
}