Skip to main content

Introduction

HTML (HyperText Markup Language) is the foundation of any web page. While browsers are forgiving of poorly written HTML, following best practices ensures your code is maintainable, accessible, and performs well. This guide covers essential HTML best practices that every frontend developer should follow.
These best practices are not just about code aesthetics—they directly impact accessibility, SEO, performance, and maintainability of your websites.

Document Structure

Use the Correct DOCTYPE

Always start your HTML documents with the proper DOCTYPE declaration:

Specify the Language

Always specify the language of your document using the lang attribute on the <html> element. This helps screen readers pronounce content correctly and improves SEO.

Include Essential Meta Tags

Include important meta tags in the <head> section:
Keep your title under 60 characters and your description under 160 characters for optimal display in search engine results.

Semantic HTML

Use Semantic Elements

Use semantic HTML elements that clearly describe their meaning to browsers, developers, and assistive technologies:

Structural Elements

Use <header>, <nav>, <main>, <article>, <section>, <aside>, and <footer> to define the structure of your document.

Text Elements

Use <h1> through <h6>, <p>, <ul>, <ol>, <li>, <blockquote>, etc., for text content.

Interactive Elements

Use <button>, <a>, <form>, <input>, etc., for interactive elements.

Media Elements

Use <img>, <video>, <audio>, <figure>, <figcaption>, etc., for media content.

Use Headings Properly

Create a logical document outline using heading elements (<h1> through <h6>):
Don’t skip heading levels (e.g., from <h1> to <h3>), as this confuses screen readers and breaks the document outline. Also, don’t use heading elements just for styling purposes.

Use Lists Appropriately

Use the appropriate list type for your content:

Accessibility

Provide Alternative Text for Images

Always include the alt attribute for images:
For decorative images that don’t add meaning to the content, use an empty alt attribute (alt="") so screen readers will skip them.

Use ARIA Attributes When Necessary

When HTML semantics aren’t sufficient, use ARIA (Accessible Rich Internet Applications) attributes to enhance accessibility:
The first rule of ARIA is: “If you can use a native HTML element with the semantics and behavior you require already built in, instead of re-purposing an element and adding an ARIA role, state or property to make it accessible, then do so.” Only use ARIA when necessary.

Ensure Keyboard Accessibility

Make sure all interactive elements are keyboard accessible:

Use Labels for Form Controls

Always associate labels with form controls:

Performance

Optimize Resource Loading

Use appropriate attributes to optimize how resources are loaded:

preload

Tells the browser to download a resource as soon as possible

defer

Delays script execution until the HTML is fully parsed

async

Downloads the script asynchronously and executes it as soon as it’s available

loading="lazy"

Defers loading of images until they are near the viewport

Specify Image Dimensions

Always specify the width and height attributes for images to prevent layout shifts during page load:
Modern browsers use these dimensions to calculate the aspect ratio and reserve space for the image, even before it loads, reducing layout shifts.

Code Quality

Use Consistent Indentation and Formatting

Maintain consistent indentation and formatting for better readability:

Use Lowercase for Element Names and Attributes

While HTML is case-insensitive, using lowercase is a widely accepted convention:

Quote Attribute Values

Always quote attribute values, even when it’s technically optional:
Using quotes consistently helps avoid errors when attribute values contain spaces or special characters.

Close All Elements Properly

Ensure all elements are properly closed:

Use Meaningful Class and ID Names

Use descriptive, meaningful names for classes and IDs:
Follow a consistent naming convention like BEM (Block Element Modifier) or another methodology that works for your team.

SEO

Use Descriptive Title and Meta Description

Create unique, descriptive titles and meta descriptions for each page:

Structure Content with Headings

Use headings to structure your content in a way that helps both users and search engines understand the hierarchy of information:
Avoid generic link text like “click here” or “read more”. Instead, use descriptive text that indicates what the user will find when they click the link:

Validation

Validate Your HTML

Regularly validate your HTML using tools like the W3C Markup Validation Service to catch errors and ensure compliance with standards.
Even if your page looks correct in browsers, invalid HTML can cause unexpected behavior, accessibility issues, and problems with future browser updates.

Fix Validation Errors

Common validation errors to watch for and fix:
  1. Missing required attributes (like alt for images)
  2. Unclosed elements
  3. Duplicate IDs
  4. Improper nesting of elements
  5. Using deprecated elements or attributes

Responsive Design

Use the Viewport Meta Tag

Always include the viewport meta tag for responsive designs:

Use Responsive Images

Implement responsive images using the srcset and sizes attributes:
Or use the <picture> element for art direction:

Security

Prevent XSS Attacks

Protect against Cross-Site Scripting (XSS) attacks:

Use HTTPS for External Resources

Always use HTTPS for external resources to prevent man-in-the-middle attacks:

Add Security Headers

Implement security headers in your server configuration or through meta tags when possible:
While these meta tags can help, it’s better to implement security headers at the server level when possible.

Compatibility

Test Across Browsers and Devices

Regularly test your HTML across different browsers, devices, and screen sizes to ensure compatibility.

Use Feature Detection

When using newer HTML features, implement feature detection rather than browser detection:

Comments and Documentation

Use Comments Effectively

Add comments to explain complex structures or non-obvious code:
Comments should explain “why” rather than “what” when the code itself clearly shows what’s happening.

Document Third-Party Code

When including third-party code or widgets, add comments explaining what they are and where they came from:

Maintainability

Separate Structure, Presentation, and Behavior

Keep HTML (structure), CSS (presentation), and JavaScript (behavior) separate:

Use Templates for Repeated Content

For repeated content patterns, consider using client-side or server-side templates:

HTML5 Features

Use HTML5 Form Features

Take advantage of HTML5 form features for better user experience and validation:

Use HTML5 Semantic Elements

Use HTML5 semantic elements to improve document structure:

Checklist

Use this checklist to ensure you’re following HTML best practices:
1

Document Structure

  • Proper DOCTYPE declaration - [ ] Language specified with lang attribute - [ ] Character encoding declared - [ ] Viewport meta tag included
  • Descriptive title and meta description
2

Semantic HTML

  • Semantic elements used appropriately - [ ] Proper heading hierarchy - [ ] Lists used appropriately - [ ] Tables used only for tabular data
3

Accessibility

  • Alternative text for images - [ ] Form controls have associated labels
  • ARIA attributes used when necessary - [ ] Keyboard accessibility ensured - [ ] Color not used as the only means of conveying information
4

Performance

  • Resources loaded optimally (defer, async, preload) - [ ] Images have width and height attributes - [ ] Responsive images implemented
5

Code Quality

  • Consistent indentation and formatting - [ ] Lowercase element names and attributes - [ ] Attribute values quoted - [ ] Elements properly closed
  • Meaningful class and ID names
6

Validation

  • HTML validates without errors - [ ] No deprecated elements or attributes

Conclusion

Following these HTML best practices will help you create websites that are accessible, performant, maintainable, and SEO-friendly. Remember that HTML is the foundation of your web pages, and a solid foundation leads to a better overall user experience. As web standards evolve, stay updated with the latest best practices and recommendations from authoritative sources like the W3C, MDN Web Docs, and web.dev.