Use this tool to indent HTML code correctly. Paste minified HTML, HTML on one line or HTML with incorrect spaces. The tool puts each tag on its own line and moves each child tag one level in from its parent. The colored guides show each nesting level, so you can see the structure of the page immediately.
How to Indent HTML Code
- Paste your HTML in the box above, or click Upload file.
- Select 2 spaces, 4 spaces or Tabs.
- Examine the result. The line numbers and the guides show each level.
- Click Copy indented HTML or Download .html.
A full page and a small part of a page both work. You do not have to add <html> or <body> tags.
Input:
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/blog">Blog</a></li>
</ul>
</nav>
Output (2 spaces):
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/blog">Blog</a></li>
</ul>
</nav>
HTML Indentation Rules
- Indent each child element one level. A
<li>is one level in from its<ul>. The<ul>is one level in from its<nav>. - Put the closing tag at the same level as the opening tag. Then you can find the end of each block quickly.
- Use one indent size in the file. Do not mix 2 spaces, 4 spaces and tabs.
- Keep short inline elements on one line. Tags such as
<a>,<b>and<span>can stay inside the line of text. - Indent
<head>and<body>inside<html>. Some teams do not indent these two tags. Both styles are correct if you use one style in all files.
2 Spaces, 4 Spaces or Tabs?
| Indent | Use it when |
|---|---|
| 2 spaces | You want the default. The Google HTML/CSS Style Guide uses 2 spaces. Deep nesting stays readable. |
| 4 spaces | Your project uses 4 spaces for its other code, for example Python or PHP templates. |
| Tabs | Your team wants each person to set the indent width in their own editor. |
Indentation does not change how the browser shows your page. Browsers ignore the spaces between tags. The tool keeps the spaces inside your text, inside <pre> blocks and next to inline tags, so the page looks the same.
Find Unclosed and Incorrect Tags
If the HTML has a structure problem, the tool shows the line and the column of the problem. For example:
Line 1, column 17: Unexpected closing tag "span".
This error tells you that a </span> has no <span> before it. Correct the tag at that position. The tool then indents the code again. Correct indentation also shows missing closing tags: when a block does not move back out to its level, a closing tag is missing.
Auto-Indent HTML in Your Code Editor
| Editor | Indent the full file | Indent selected lines |
|---|---|---|
| VS Code (Windows) | Shift+Alt+F | Ctrl+] (in) and Ctrl+[ (out) |
| VS Code (Mac) | Shift+Option+F | Cmd+] (in) and Cmd+[ (out) |
| VS Code (Linux) | Ctrl+Shift+I | Ctrl+] (in) and Ctrl+[ (out) |
| WebStorm and PhpStorm | Ctrl+Alt+L (Cmd+Option+L on a Mac) | Tab (in) and Shift+Tab (out) |
| Sublime Text | Edit > Line > Reindent | Ctrl+] (in) and Ctrl+[ (out) |
| Notepad++ | Use a plugin such as XML Tools > Pretty print. It works only on well-formed markup. | Tab (in) and Shift+Tab (out) |
| Chrome DevTools | Sources panel > click the {} (pretty print) button | Not available |
Indent Text on a Web Page (CSS)
Do you want to indent the text that people see on the page, not the code? Use CSS. Do not use many characters, because they break on small screens.
/* Indent the first line of each paragraph */
p {
text-indent: 2em;
}
/* Move a full block to the right */
.note {
margin-left: 2rem;
}
Common Mistakes
- You indent with spaces and tabs in one file. The code then looks different in each editor. Select one option in this tool and indent the full file again.
- You add
to indent text. Usetext-indentormargin-leftin CSS. - You edit minified HTML manually. Indent it first, edit it, and then minify it again if necessary.
- You ignore an error. The tool does not guess where a missing tag goes. Correct the tag at the line that the error shows.
Explore More Free Developer Tools
- Convert rich text to plain text: remove all HTML tags and keep only the text.
- Convert table to Markdown: change an HTML table into a Markdown table.
- Markdown to CSV: change a Markdown table into a CSV file.
- Slug generator: make clean URL slugs for your pages and links.
- Compare URLs: find the differences between two links.
- Screaming snake case converter: make constant names for your scripts and config files.
Your HTML stays in your browser. The tool does not send your code to a server.
Paste your HTML above and get correctly indented code now.