Skip to main content

Synopsis

html-beautify [options] <file ...>
The html-beautify command is installed alongside js-beautify when you install the package globally:
npm install -g js-beautify
Beautified output is written to stdout by default. You can also invoke the HTML beautifier via js-beautify --type html.
Pass - as a filename to read from stdin.

Options

I/O options

FlagLong formDescription
-f--fileInput file(s). Pass - for stdin.
-r--replaceWrite output in-place, overwriting the input file.
-o--outfileWrite output to a specific file instead of stdout.
--configPath to a JSON config file.
-q--quietSuppress logging to stdout.
-h--helpShow help and exit.
-v--versionShow the version number and exit.

Indentation options

FlagLong formDefaultDescription
-s--indent-size4Number of spaces per indentation level.
-c--indent-char" "Character used for indentation.
-t--indent-with-tabsfalseIndent with tabs, overrides -s and -c.
--indent-empty-linesfalseKeep indentation on empty lines.
--editorconfigfalseUse EditorConfig to set options from .editorconfig.

Output formatting

FlagLong formDefaultDescription
-e--eolfirst newline in file, else \nLine terminator character(s).
-n--end-with-newlinefalseEnd output with a newline.
-p--preserve-newlinestruePreserve existing line breaks. Use --no-preserve-newlines to disable.
-m--max-preserve-newlines10Maximum blank lines to preserve in one chunk.
-w--wrap-line-length0Maximum characters per line. 0 disables wrapping.
-b--brace-stylecollapseBrace style: collapse, expand, end-expand, none, collapse-preserve-inline.
--templatingautoTemplating languages to recognize. auto enables all in HTML context. Values: auto, none, django, erb, handlebars, php, smarty, angular.

Attribute wrapping

FlagLong formDefaultDescription
-A--wrap-attributesautoAttribute wrapping strategy: auto, force, force-aligned, force-expand-multiline, aligned-multiple, preserve, preserve-aligned.
-M--wrap-attributes-min-attrs2Minimum number of attributes before forced-wrap modes activate.
-i--wrap-attributes-indent-size(indent-size)Indentation size for wrapped attributes. Ignored when wrap-attributes is aligned.

Content handling

FlagLong formDefaultDescription
-I--indent-inner-htmlfalseIndent the contents of <head> and <body> elements.
-H--indent-handlebarsfalseIndent Handlebars block expressions and their content.
-S--indent-scriptsnormalIndentation of <script> tag content: keep, separate, or normal.
-d--inline(standard inline tags)Comma-separated list of tags to treat as inline elements.
--inline_custom_elementstrueTreat custom elements (hyphenated names) as inline.
-U--unformatted[]Tags whose content should not be reformatted at all.
-T--content_unformattedpre,textareaTags whose content (not the tags themselves) should not be reformatted.
-E--extra_linershead,body,/htmlTags that should have an extra newline prepended before them.
--unformatted_content_delimiter""Keep text content together between occurrences of this string.

Examples

# Beautify from stdin to stdout
cat index.html | html-beautify -

# Beautify a file, output to stdout
html-beautify index.html

# Replace the file in-place
html-beautify --replace index.html

# Write to a new file with 2-space indentation
html-beautify -s 2 -o dist/index.html src/index.html

# Indent inner HTML and force-wrap attributes
html-beautify --indent-inner-html --wrap-attributes force-expand-multiline index.html

# End with a newline, preserve up to 2 blank lines
html-beautify --end-with-newline --max-preserve-newlines 2 index.html

# Do not reformat content inside <pre> and <code> tags
html-beautify --content_unformatted pre,code index.html
You can also run the HTML beautifier via the main js-beautify command with the --type html flag:
js-beautify --type html index.html

Directives

You can embed directives directly in your HTML to control the beautifier on a per-block basis:
<!-- beautify ignore:start -->
<div class="some  messy  spacing"></div>
<!-- beautify ignore:end -->

<!-- beautify preserve:start -->
<ul>
    <li>Keep this formatting</li>
</ul>
<!-- beautify preserve:end -->