thoughtasylumTOOLBOX Preview Guides Use offline Automate Feedback

Tools › Data

Code Format

Minify HTML, CSS, JavaScript, SQL or SVG, or lay it out again with 2 or 4 space or tab indents, convert HTML or SVG to React JSX, and turn code into a syntax-highlighted picture.

About this tool What it's for, how to use it and an example

What it's for

Make code smaller, or make squashed code readable again, for HTML, CSS, JavaScript, SQL and SVG, or turn HTML and SVG into JSX for React.

  • When you’re putting a snippet into a page, an email template or a bookmarklet and want it as small as it can be.
  • When you’re moving a snippet of HTML, or an SVG icon, into a React component.
  • When you’re reading minified code from a website or a build, or a query logged on one long line, and want it laid out with one statement per line.

How to use it

Choose the language and an indent (2 or 4 spaces, or tabs), paste the code, then:

  • Prettify lays it out with line breaks and indents, using js-beautify for HTML, CSS and JavaScript, and sql-formatter for SQL. SVG is laid out as XML Format does it, keeping comments.
  • Minify removes comments and unneeded whitespace. JavaScript also goes through Terser, which shortens local names and simplifies the code. SVG goes through SVGO, which also rounds numbers and simplifies shapes. In HTML, <style> and <script> contents are minified too.

HTML to JSX and SVG to JSX (either button) convert the markup for React. Attributes are renamed (class becomes className, for becomes htmlFor, stroke-width becomes strokeWidth, xlink:href becomes xlinkHref), style strings become objects, empty elements are self-closed, comments become {/* */}, and { and } in text are escaped. data-* and aria-* attributes stay as they are. Prettify indents the result; Minify puts it on one line. Give a component name, starting with a capital letter, to wrap it in a function component. Several top-level elements are wrapped in a fragment (<>…</>).

The message line shows the size before and after. The language and indent are remembered in this browser. Each language’s library loads the first time you use it.

Make an image draws the result (or the code, before you format it) as a syntax-highlighted picture in a window frame, dark or light, for slides, documents and posts; Download PNG saves it at twice screen size, so it stays sharp.

Example

Choose JavaScript, paste this and choose Minify:

function add(first, second) {
  // Add two numbers
  return first + second
}
console.log(add(1, 2))

The result is function add(d,n){return d+n}console.log(add(1,2));, and the message says it is 48% smaller. Paste that back in and choose Prettify: the layout returns, but the comment and the names first and second don’t.

Good to know

Prettifying can’t bring back what minifying removed: comments, and local names that Terser shortened. Terser keeps top-level names, and comments starting /*! or containing @license; the CSS minifier removes every comment.

HTML minifying keeps one space where whitespace could show on the page (between <b> and <i>, say) and removes it next to block elements such as <div> and <p>. <pre> and <textarea> are left exactly as they are, as are scripts of other types, such as templates. IE conditional comments are kept.

SQL minifying keeps strings and quoted names as they are, including MySQL’s \' escapes. Prettifying uses standard SQL, so some dialect-specific syntax may be reported as an error.

SVGO can remove or shorten ids, which matters if CSS or a script outside the SVG uses them.

JSX conversion reads the code with the browser’s own parser, so HTML is tidied the way a page would be (a missing </p> is added, for example) and SVG must be well-formed. Event attributes such as onclick are renamed (onClick) but stay strings, which React won’t accept: replace them with functions. Numbers in styles stay as strings.

As a picture

Built with The third-party code and data this tool uses, and their licences

This tool runs on the following open-source work, copied onto this site and served from it:

js-beautify 2.0.3
Copyright
Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors
Licence
MIT License
Source
github.com/beautifier/js-beautify
sql-formatter 15.9.0
Copyright
Copyright (c) 2016-2020 ZeroTurnaround LLC; Copyright (c) 2020-2021 George Leslie-Waksman and other contributors; Copyright (c) 2021-present inferrinizzard and other contributors
Licence
MIT License
Source
github.com/sql-formatter-org/sql-formatter
SVGO 4.1.0
Copyright
Copyright (c) Kir Belevich
Licence
MIT License
Source
github.com/svg/svgo
Terser 5.51.2
Copyright
Copyright 2012-2018 (c) Mihai Bazon
Licence
BSD 2-Clause License
Source
github.com/terser/terser

Every page also uses Montserrat (SIL Open Font License 1.1).
Everything the site uses is on Credits and licences.

Private: this tool runs in your browser. Nothing you type, paste or choose leaves this page.

Saved you a few minutes? Say thanks with a coffee.

Something wrong with this tool, or missing from it? Report a bug or suggest a feature.

↑ ↓ move↵ openesc close