Skip to main content
The HTML beautifier reformats HTML source code into consistently indented, readable output. It also beautifies embedded <script> and <style> blocks using the JavaScript and CSS beautifiers, respecting any options you provide for those languages.

Before and after

<!DOCTYPE html><html><head><title>Page</title><style>body{margin:0;color:#333}</style></head><body><div class="container"><h1>Hello</h1><p>This is a paragraph with <a href="/about">a link</a> and some text.</p></div><script>var x=1;function init(){console.log('ready');}</script></body></html>

Key options

indent_inner_html

When true, indents the content of <head> and <body> tags. Defaults to false.
<html>
<head>
    <title>Page</title>
</head>
<body>
    <p>Content</p>
</body>
</html>

indent_scripts

Controls the indentation level of code inside <script> tags.
ValueDescription
normalScript content is indented relative to the <script> tag (default)
keepScript content keeps its own indentation level
separateScript content starts at zero indentation
<body>
    <script>
        var x = 1;
        function init() {}
    </script>
</body>

wrap_line_length

Sets a maximum number of characters per line. Lines exceeding this length will be wrapped. Set to 0 to disable wrapping. Default is 250.
beautify_html(source, { wrap_line_length: 80 });

wrap_attributes

Controls how HTML tag attributes are wrapped when a tag has multiple attributes.
ValueDescription
autoWrap only when the line is too long (default)
forceWrap each attribute onto its own line, except the first
force-alignedWrap and align attributes to the first attribute’s position
force-expand-multilineWrap all attributes onto separate lines when any wrapping occurs
aligned-multipleAlign multiple attributes per line to the first attribute
preservePreserve existing attribute wrapping
preserve-alignedPreserve existing wrapping and align attributes
<input type="text" name="email" placeholder="Enter email" class="form-control">
Use wrap_attributes_min_attrs to set the minimum number of attributes before forcing wraps. Default is 2.

inline and unformatted

inline is a list of tag names that are treated as inline elements and will not trigger line breaks around them. By default it includes common inline elements like a, span, em, strong, img, input, and others. unformatted is a list of tags whose content the beautifier will not reformat. Unlike inline, using unformatted preserves whitespace inside the tag.
beautify_html(source, {
    // Add 'custom-tag' to the inline list
    inline: ['span', 'a', 'custom-tag'],
    // Do not reformat <code> blocks
    unformatted: ['code', 'pre']
});

content_unformatted

A list of tags whose entire content (including child elements) is left unformatted. Defaults to ["pre", "textarea"].
beautify_html(source, {
    content_unformatted: ['pre', 'textarea', 'script']
});

extra_liners

A list of tags that get an extra blank line inserted before them. Defaults to ["head", "body", "/html"], which produces the blank lines you see before <head>, <body>, and </html> in beautified output.
beautify_html(source, {
    // Remove the extra blank lines
    extra_liners: []
});

templating

Controls which template language syntaxes are recognized and preserved during beautification. When set to auto (the default in HTML), all supported template languages are enabled.
ValueDescription
autoEnable all template syntaxes (HTML default)
noneNo templating support
djangoDjango/Jinja2 {% %} and {{ }} tags
erbERB <% %> tags
handlebarsHandlebars/Mustache {{ }} tags
phpPHP <?php ?> tags
smartySmarty {$var} tags
angularAngularJS {{ }} expressions
beautify_html(source, {
    // Only recognize Handlebars syntax
    templating: ['handlebars']
});
In JavaScript beautification, templating defaults to none. If you are beautifying HTML that contains template syntax, use the HTML beautifier, not the JS beautifier.

Embedded JS and CSS

The HTML beautifier automatically beautifies code inside <script> and <style> blocks. You can pass JS and CSS options alongside HTML options — the beautifier routes them to the appropriate sub-beautifier.
const beautify = require('js-beautify');

beautify.html(source, {
    // HTML options
    indent_size: 4,
    indent_inner_html: false,
    wrap_attributes: 'force-aligned',

    // These apply to embedded <script> blocks
    js: {
        indent_size: 2,
        brace_style: 'collapse'
    },

    // These apply to embedded <style> blocks
    css: {
        indent_size: 2,
        newline_between_rules: true
    }
});

Usage

const beautify_html = require('js-beautify').html;

const ugly = '<div><p>Hello</p></div>';
const pretty = beautify_html(ugly, {
    indent_size: 2,
    wrap_line_length: 100
});

console.log(pretty);