Table of contents
1.
Introduction
2.
Pre-requisites
3.
What are We Creating?
4.
How to Indent in HTML?
4.1.
1. Using Spaces for Indentation
4.2.
2. Using Tabs for Indentation
5.
Commonly Used Examples of Indentation in HTML
5.1.
Example 1: Indenting Nested Elements
5.2.
Example 2: Indentation in Lists
5.3.
Example 3: Indenting Forms
6.
Why is Indentation Important?
7.
Frequently Asked Questions
7.1.
Does HTML require indentation?
7.2.
How many spaces should I use for indentation in HTML?
7.3.
Can I use a code editor to format indentation automatically?
8.
Conclusion
Last Updated: Feb 24, 2025
Medium

How to indent in HTML?

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Indentation in HTML helps improve code readability and maintainability. While HTML itself does not enforce indentation, developers commonly use spaces or tabs to structure nested elements properly. You can manually indent using spaces or tabs in your code editor. Additionally, CSS properties like margin and padding can visually indent content. 

How to indent in HTML?

In this article, you will learn different methods to indent HTML code and content effectively.

Pre-requisites

Before we begin, you should have:

  • A basic understanding of HTML structure.
     
  • A code editor like VS Code, Sublime Text, or Notepad++.
     
  • A web browser to test the code.

What are We Creating?

In this article, we will learn how to indent HTML code properly to improve its readability. We will look at various ways to indent HTML, see real-world examples, and understand why indentation is a good coding practice.

How to Indent in HTML?

Indentation in HTML is done using spaces or tabs. While HTML does not enforce indentation rules, following best practices makes your code more readable and structured.

1. Using Spaces for Indentation

Most developers prefer using two or four spaces for indentation.

<!DOCTYPE html>
<html>
  <head>
    <title>Indentation Example</title>
  </head>
  <body>
    <h1>Welcome to HTML Indentation</h1>
    <p>This paragraph is properly indented.</p>
  </body>
</html>

 

Output

Output

In the above example:

  • The <head> and <body> tags are indented inside <html>.
     
  • The <title> and <h1> tags are further indented inside their parent tags.

2. Using Tabs for Indentation

Some developers use tabs instead of spaces. Most code editors allow you to convert tabs to spaces automatically.

<!DOCTYPE html>
<html>
<head>
<title>Tab Indentation Example</title>
</head>
<body>
<h1>Welcome</h1>
<p>This paragraph is indented using tabs.</p>
</body>
</html>


Output

Output

The choice between spaces and tabs depends on project guidelines, but consistency is key.

Commonly Used Examples of Indentation in HTML

Here are some examples where indentation is essential:

Example 1: Indenting Nested Elements

<!DOCTYPE html>
<html>
  <body>
    <div>
      <h2>Article Title</h2>
      <p>This is a paragraph inside a div.</p>
    </div>
  </body>
</html>

 

Output

Output

In this example, <h2> and <p> are properly indented inside the <div> tag.

Example 2: Indentation in Lists

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>
    Item 3
    <ul>
      <li>Sub-item 1</li>
      <li>Sub-item 2</li>
    </ul>
  </li>
</ul>

 

Output

Output

Here, sub-items inside <ul> are properly indented, making it easy to understand the hierarchy.

Example 3: Indenting Forms

<form>
  <label for="name">Name:</label>
  <input type="text" id="name" name="name">
  <br>
  <label for="email">Email:</label>
  <input type="email" id="email" name="email">
</form>

 

Output

Output

Each element inside the <form> tag is properly indented for better readability.

Why is Indentation Important?

Indentation is crucial in HTML for the following reasons:

  1. Readability: Indented code is easier to read and understand.
     
  2. Debugging: Well-structured code makes it easier to find and fix errors.
     
  3. Collaboration: Proper indentation helps teams work on the same codebase efficiently.
     
  4. Maintaining Code: Well-indented code is easier to update and modify.

Frequently Asked Questions

Does HTML require indentation?

No, HTML does not enforce indentation, but it is a best practice for readability and code maintenance.

How many spaces should I use for indentation in HTML?

Most developers use two or four spaces, but it depends on personal preference or project guidelines.

Can I use a code editor to format indentation automatically?

Yes, code editors like VS Code, Sublime Text, and Notepad++ offer auto-indentation features to format HTML code properly.

Conclusion

In this article, we learned on how to indent in HTML to improve code readability. While HTML ignores extra spaces and indentation, we can use CSS properties like margin and padding to visually indent elements. Additionally, using &nbsp; (non-breaking spaces) or the <blockquote> tag helps create indentation in content. Proper indentation enhances code organization and maintainability.

Live masterclass