Code beautifier with Prettier: HTML, CSS, JS, TS and YAML

Paste messy or minified code and get it indented and tidy with Prettier, the formatter most teams use.

loading Code beautifier with Prettier: HTML, CSS, JS, TS and YAML
About this tool

What is a code beautifier?

A code beautifier (or formatter) rewrites code with consistent indentation, line breaks and spacing without changing what it does. You paste minified JavaScript from a production bundle, a one-line JSON response, a messy CSS file or an HTML snippet copied from a page, and you get it back readable. Developers use it to read code they did not write, to clean a snippet before sharing it, and to make diffs smaller by agreeing on one style.

This online code formatter uses Prettier, the formatter most JavaScript teams already run in their editors and CI. It supports twelve languages: JavaScript, TypeScript, JSX, JSON, HTML, Vue, CSS, SCSS, Less, Markdown, YAML and GraphQL. You choose the indentation (2, 4 or 8 spaces, or tabs), the line width (80 by default), whether statements end with semicolons and whether strings use single quotes. It runs in your browser, so the code never leaves your machine, which matters for private source code or configuration with secrets. If the code has a syntax error, the tool points to the line and column.

How to beautify code

  1. Paste your code in "Your code". The tool guesses the language from the first characters (a leading { is JSON, <!doctype html is HTML, query is GraphQL). Use "Detect the language" to repeat the guess.
  2. Check "Language" and change it if the guess is wrong. Choosing the wrong language is the main cause of errors.
  3. Set "Indentation", "Line width", "Semicolons" and "Single quotes". Your choices are remembered.
  4. Read the result in "Formatted code", with its line count. Everything is updated as you type.
  5. Copy it, or use "Download" to save it with the right extension (.js, .css, .json...).
  6. If you see "There is a problem at line N, column M", fix that point (usually a missing bracket, quote or comma) or change the language.

Examples

Minified JavaScript

Input: const a={b:1,c:[1,2,3]};function x(){return a.b+1}. With the defaults the output is:

const a = { b: 1, c: [1, 2, 3] };
function x() {
  return a.b + 1;
}

CSS in one line

Input: .a{color:red;margin:0 auto}.b>p{font-weight:700} becomes one declaration per line, with .b > p spaced, inside each rule.

JSON that fits in a line

{"a":1,"b":[1,2,{"c":null}]} becomes { "a": 1, "b": [1, 2, { "c": null }] }. Prettier keeps JSON on one line when it fits in the line width and was written that way. To only validate or pretty-print JSON, use the JSON formatter.

Single quotes and no semicolons

const s = "x" with "Single quotes" on and "Semicolons" off gives const s = 'x'.

A syntax error

const a = { reports line 1, column 12: the object is never closed. {"a":1,} as JSON is also rejected, because JSON does not allow a trailing comma.

Common mistakes

  • Wrong language selected. YAML pasted as JSON or Vue as HTML fails. Use "Detect the language" or pick it.
  • Expecting a minifier or a fixer. The tool formats valid code; it does not repair broken syntax. To compress code, use the minifier.
  • Comparing formatters. Prettier has its own opinions, and the result can differ from your editor if the editor uses another tool or another configuration file.
  • Forgetting Prettier does not rename or reorder. It changes only whitespace, line breaks, quotes and trailing commas (and parentheses), never the logic.
  • Pasting secrets. The code stays in your browser, but avoid pasting credentials into any page you do not control.

FAQ

Is it safe to paste private code?

The formatting runs in your browser and nothing is sent to a server.

Which Prettier options does it use?

Indentation (2, 4, 8 or tabs), print width, semicolons and single quotes. The rest are Prettier defaults.

Can it format a whole project?

No, it works on one pasted file at a time. For a project, install Prettier and run it in the folder (see the code below).

Why did the line not break at 80 characters?

Prettier breaks lines where it can; a long string or a comment cannot be split, so a line can exceed the width.

Can I format YAML or HTML?

Yes. Choose YAML or HTML in the language list. For YAML-specific checks you can also use the YAML formatter.

Format code from the command line and in code

Bash

npx prettier --write "src/**/*.{js,ts,css,json}"
echo '{"a":1}' | npx prettier --parser json

JavaScript

import { format } from 'prettier';
const out = await format('const a={b:1}', { parser: 'babel', singleQuote: true });
console.log(out); // const a = { b: 1 };
Report a problem