HTML Style Guide


A good HTML style guide helps you write clean, consistent and easy-to-maintain code. When multiple developers work on the same project or when your files grow large, a consistent structure becomes important. In this chapter, you will learn the recommended rules for formatting, naming and organising your HTML. These guidelines make your pages easier to read, debug and extend.

Why Follow an HTML Style Guide

A style guide ensures that your code stays readable. It avoids messy formatting and unpredictable patterns. When you follow a uniform style:

  • the code becomes easier to scan

  • debugging becomes faster

  • teamwork becomes smoother

  • files look professional and organised

  • browsers render pages without confusion

A style guide does not change how HTML works. It only standardises how you write it.

Always Use Lowercase Tags

HTML is not case-sensitive, but using lowercase tags keeps your code clean and consistent.

Example (recommended)

<p>This is a paragraph.</p>

Example (not recommended)

<P>This is a paragraph.</P>

Lowercase tags are easier to read and match common best practices.

Use Lowercase Attribute Names

Just like tags, keep attribute names lowercase.

<input type="text" placeholder="Enter name">

Avoid mixed or uppercase attributes because they make the code look inconsistent.

Always Close Your Tags Properly

HTML5 allows some tags to be self-closing, but for clarity use proper closing tags whenever required.

Good practice

<div>
  <p>Hello</p>
</div>

Avoid

<div>
  <p>Hello
</div>

Unclosed tags often break layouts.

Use Proper Indentation

Indentation makes the structure visible. Each nested element should be indented by two spaces or four spaces. Stick to one style throughout your project.

Example

<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
  </ul>
</nav>

Indentation helps you understand parent-child relationships instantly.

Keep Attribute Values in Quotes

Always wrap attribute values inside double quotes.

<input type="text" name="username">

Some values work without quotes, but that is error-prone and not preferred.

Use Meaningful Class and ID Names

Name your classes and IDs based on their purpose, not their appearance.

Good examples

  • header

  • footer

  • main-nav

  • product-card

Bad examples

  • blue-text

  • big-div

  • box1

Meaningful names make your code understandable even months later.

Use HTML5 Semantic Tags

Semantic tags describe the purpose of each section. This improves readability and accessibility.

Use:

  • header

  • main

  • footer

  • nav

  • section

  • article

  • aside

Example

<article>
  <h2>Blog Title</h2>
  <p>Blog content...</p>
</article>

Semantic tags help both developers and search engines.

Keep Lines Short

Avoid writing very long lines inside your HTML. Short and clean lines make your code easier to scan.

Instead of:

<p class="intro" id="first-intro" style="color: #333; font-size: 16px;">Welcome to our tutorial on HTML styling. This text explains why formatting is important.</p>

Prefer:

<p class="intro" id="first-intro">
  Welcome to our tutorial on HTML styling. This text explains why formatting is important.
</p>

Use Comments to Explain Sections

Comments help when you revisit code after months.

<!-- Main navigation -->
<nav>
  ...
</nav>

Use comments sparingly but effectively.

Avoid Inline Styles

Inline styles make your HTML cluttered and hard to maintain. Prefer external CSS files.

Not recommended

<p style="color: red;">Error message</p>

Recommended

<p class="error">Error message</p>

This separates structure from design.

Use Alt Text for Images

Always add descriptive alt text. This improves accessibility and SEO.

<img src="team-photo.jpg" alt="Team members standing together">

Alt text describes the image when it fails to load or when a screen reader is used.

Keep File Names Simple and Lowercase

File names should be short, lowercase and use hyphens instead of spaces.

Recommended:

  • about-us.html

  • contact-form.html

Avoid:

  • AboutUs.HTML

  • contact form.html

Consistent naming prevents errors and looks professional.

Use Boolean Attributes Properly

Some attributes don't need a value. Their presence alone is meaningful.

Examples:

  • checked

  • disabled

  • required

  • readonly

<input type="checkbox" checked>

Using checked="checked" is allowed but unnecessary.

Organise Your Code in Logical Blocks

Group related elements together.

Example

<!-- Header -->
<header>...</header>

<!-- Main content -->
<main>...</main>

<!-- Footer -->
<footer>...</footer>

This structure keeps the page easy to understand.

Avoid Extra Spaces

Extra spaces or blank lines can make code look messy. Keep spacing consistent.

Bad example

<div>


  <p>Hello</p>

</div>

Good example

<div>
  <p>Hello</p>
</div>

Use Meta Tags Properly

Meta tags set the character set, description and viewport.

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

These two should appear in every modern HTML page.

Example of Clean HTML Structure

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Simple Page</title>
</head>
<body>

  <header>
    <h1>Website Title</h1>
  </header>

  <main>
    <section>
      <h2>About Us</h2>
      <p>Welcome to our website.</p>
    </section>
  </main>

  <footer>
    <p>Copyright 2025</p>
  </footer>

</body>
</html>

This example follows most of the style rules discussed above.

Summary of HTML Style Guide

A consistent HTML style guide helps you write clean, readable and well-structured code. You learned how to format tags, indent code, name attributes, use semantic elements, avoid inline styles and follow proper commenting and spacing practices. These guidelines make your HTML easier to work with and ensure a professional codebase that stays maintainable in the long run.


Practice Questions

Q1. Format the given messy HTML code according to style guide rules.

Q2. Write semantic HTML for a blog post including header, main, and footer sections.

Q3. Create a navigation menu with consistent indentation and attribute order.

Q4. Correctly use void tags like <img> and <br> without closing tags.

Q5. Identify and fix inline styles by moving them to external CSS.

Q6. Use descriptive class and id names for a form section.

Q7. Add comments to explain a complex section of HTML code.

Q8. Write an accessible image tag with alt text following best practices.

Q9. Create a consistent attribute order for a list of links.

Q10. Refactor an HTML page to keep lines under 100 characters.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top