HTML Best Practices: Writing Clean, Accessible Code

Published: Reading time: 8 minutes

Learn essential HTML best practices for creating clean, accessible, and SEO-friendly web pages that work for everyone.

1. Proper Heading Structure

Using proper heading hierarchy is crucial for both SEO and accessibility. Screen readers rely on heading structure to navigate content, and search engines use it to understand page organization.

Avoid Multiple H1 Elements

Each page should have only one <h1> element representing the main topic of the page.

Don't do this:

<main>
  <div>
    <h1>Understanding Web Development</h1>
    <p>Web development involves creating websites and web applications.</p>
  </div>
  <div>
    <h1>Frontend Technologies</h1>
    <p>HTML, CSS, and JavaScript form the foundation of frontend development.</p>
  </div>
</main>

Do this instead:

<main>
  <div>
    <h1>Understanding Web Development</h1>
    <p>Web development involves creating websites and web applications.</p>
  </div>
  <div>
    <h2>Frontend Technologies</h2>
    <p>HTML, CSS, and JavaScript form the foundation of frontend development.</p>
  </div>
</main>

Don't Skip Heading Levels

Maintain a logical heading hierarchy without skipping levels to ensure a coherent structure.

Don't do this:

<h1>Web Development Fundamentals</h1>
<h3>Understanding HTML Structure</h3>
<h5>CSS Styling Techniques</h5>

Do this instead:

<h1>Web Development Fundamentals</h1>
<h2>Understanding HTML Structure</h2>
<h3>CSS Styling Techniques</h3>

2. Semantic Elements for Headers and Footers

HTML5 introduced semantic elements that provide meaning to the structure of web pages. Use <header> and <footer> instead of generic <div> elements.

Don't do this:

<div class="header">
  <a href="index.html">Home</a>
  <a href="#">Services</a>
  <a href="#">Portfolio</a>
</div>
<div class="footer">
  <a href="index.html">Home</a>
  <a href="#">Privacy Policy</a>
  <a href="#">Contact</a>
</div>

Do this instead:

<header>
  <nav>
    <a href="index.html">Home</a>
    <a href="#">Services</a>
    <a href="#">Portfolio</a>
  </nav>
</header>
<footer>
  <nav>
    <a href="index.html">Home</a>
    <a href="#">Privacy Policy</a>
    <a href="#">Contact</a>
  </nav>
</footer>

3. Image Captions with Figure Element

Use the <figure> and <figcaption> elements to associate images with their captions semantically.

Don't do this:

<div>
  <img src="responsive-design.jpg" alt="Example of responsive web design">
  <p>This diagram shows how responsive design adapts to different screen sizes.</p>
</div>

Do this instead:

<figure>
  <img src="responsive-design.jpg" alt="Example of responsive web design">
  <figcaption>This diagram shows how responsive design adapts to different screen sizes.</figcaption>
</figure>
Placeholder image showing web development concepts
Example of a properly captioned image using the figure element.

4. Semantic Text Formatting

Use semantic HTML elements like <strong> and <em> instead of presentational tags like <b> and <i>.

Don't do this:

<p>
  It is <b>extremely important</b> to follow accessibility guidelines
  when <i>designing websites</i> for all users.
</p>

Do this instead:

<p>
  It is <strong>extremely important</strong> to follow accessibility guidelines
  when <em>designing websites</em> for all users.
</p>

5. Proper Element Nesting

Follow proper nesting rules: block-level elements can contain inline elements, but inline elements cannot contain block-level elements.

Don't do this:

<a href="#">
  <p>Click here to learn more about web accessibility standards.</p>
</a>

Do this instead:

<p>
  <a href="#">Click here</a> to learn more about web accessibility standards.
</p>

6. Consistent Structure and Indentation

Maintain consistent indentation (typically 2 or 4 spaces) to improve code readability and maintainability.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  </head>
  <body>
    <header>
      <h1>Website Title</h1>
    </header>
    <main>
      <p>Main content goes here.</p>
    </main>
    <footer>
      <p>Copyright information</p>
    </footer>
  </body>
</html>

7. Accessibility (WCAG Compliance)

Ensure your HTML is accessible to all users, including those using assistive technologies.

Proper Form Structure

Use <label> elements correctly associated with form controls, and group related fields with <fieldset> and <legend>.

<form action="#" method="POST" aria-labelledby="form-title" id="contact-form">
  <fieldset>
    <legend id="form-title">Contact Information</legend>

    <!-- Full Name Input -->
    <div>
      <label for="full-name">Full Name:</label>
      <input type="text" id="full-name" name="full-name" required aria-required="true" placeholder="John Doe">
    </div>

    <!-- Submit Button -->
    <div>
      <button type="submit" id="submit" disabled>Submit</button>
    </div>
  </fieldset>
</form>

Accessible Form Example

Contact Information

8. External Stylesheets

Keep CSS in external files rather than inline or embedded within <style> tags in HTML. This makes styles reusable, improves maintainability and keeps HTML clean.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Website</title>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <!-- Content -->
  </body>
</html>

9. Always Declare a Doctype

The DOCTYPE declaration is required for informing the browser what standards you're using. It should be the first line of your HTML document.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Document</title>
  </head>
  <body>
    <!-- Content -->
  </body>
</html>

Conclusion

Following HTML best practices ensures your websites are accessible, maintainable, and SEO-friendly. Remember that just because your code works doesn't mean you're following best practices.

Always validate your HTML using the W3C Markup Validation Service to catch potential issues early.

By implementing these practices, you'll create better experiences for all users and make your code easier to maintain and scale.