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.
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.
Copy
<html lang="en"> <!-- For English content --></html><html lang="es"> <!-- For Spanish content --></html><html lang="zh-Hans"> <!-- For Simplified Chinese content --></html>
Create a logical document outline using heading elements (<h1> through <h6>):
Copy
<!-- Bad: Skipping heading levels --><h1>Website Title</h1><h3>Section Title</h3><!-- Skipped h2 --><!-- Bad: Using headings for styling --><h2>This text is not a heading but I want it big</h2><!-- Good: Proper heading hierarchy --><h1>Website Title</h1><h2>Section Title</h2><h3>Subsection Title</h3>
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.
<!-- Unordered list for items with no specific order --><ul> <li>Item 1</li> <li>Item 2</li></ul><!-- Ordered list for sequential items --><ol> <li>First step</li> <li>Second step</li></ol><!-- Description list for name-value pairs --><dl> <dt>HTML</dt> <dd>HyperText Markup Language</dd> <dt>CSS</dt> <dd>Cascading Style Sheets</dd></dl>
<!-- Informative image with descriptive alt text --><img src="chart.png" alt="Bar chart showing sales growth from 2020 to 2023" /><!-- Decorative image with empty alt text --><img src="decorative-divider.png" alt="" />
For decorative images that don’t add meaning to the content, use an empty
alt attribute (alt="") so screen readers will skip them.
When HTML semantics aren’t sufficient, use ARIA (Accessible Rich Internet Applications) attributes to enhance accessibility:
Copy
<!-- Custom toggle button --><div role="button" tabindex="0" aria-pressed="false">Toggle Feature</div><!-- Status message --><div role="status" aria-live="polite">Your changes have been saved</div><!-- Error message --><div role="alert">Please correct the errors in the form</div>
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.
Make sure all interactive elements are keyboard accessible:
Copy
<!-- Bad: Div used as a button without keyboard support --><div class="button" onclick="doSomething()">Click Me</div><!-- Good: Native button element with keyboard support --><button type="button" onclick="doSomething()">Click Me</button><!-- Good: Custom element with keyboard support --><div role="button" tabindex="0" onclick="doSomething()" onkeydown="handleKeydown(event)"> Click Me</div>
<!-- Bad: Unclosed or improperly closed elements --><div> <p>This paragraph is not closed</p> <p>This creates ambiguity</p></div><!-- Good: Properly closed elements --><div> <p>This paragraph is properly closed</p> <p>This creates clear structure</p></div>
Create unique, descriptive titles and meta descriptions for each page:
Copy
<head> <title>Beginner's Guide to HTML Best Practices | YourSite</title> <meta name="description" content="Learn essential HTML best practices for writing clean, accessible, and SEO-friendly code. Perfect for beginners and experienced developers." /></head>
Use headings to structure your content in a way that helps both users and search engines understand the hierarchy of information:
Copy
<h1>Main Topic of the Page</h1><h2>Important Subtopic</h2><p>Content about the subtopic...</p><h2>Another Important Subtopic</h2><p>Content about this subtopic...</p><h3>Detail of the Subtopic</h3><p>More specific content...</p>
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:
Copy
<!-- Bad: Non-descriptive link text --><p>To learn more about HTML, <a href="/html-guide">click here</a>.</p><!-- Good: Descriptive link text --><p> Check out our <a href="/html-guide">comprehensive HTML guide</a> to learn more.</p>
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.
Protect against Cross-Site Scripting (XSS) attacks:
Copy
<!-- Bad: Directly embedding user input --><div id="userContent"></div><script> // DANGEROUS: Directly inserting user input into the DOM document.getElementById('userContent').innerHTML = userProvidedContent;</script><!-- Good: Using textContent instead of innerHTML for user-generated content --><div id="userContent"></div><script> // SAFER: Using textContent prevents script execution document.getElementById('userContent').textContent = userProvidedContent;</script>
Always use HTTPS for external resources to prevent man-in-the-middle attacks:
Copy
<!-- Bad: Using HTTP --><script src="http://example.com/script.js"></script><!-- Good: Using HTTPS --><script src="https://example.com/script.js"></script><!-- Also good: Protocol-relative URL (inherits the protocol of the page) --><script src="//example.com/script.js"></script>
Add comments to explain complex structures or non-obvious code:
Copy
<!-- Navigation for mobile devices --><nav class="mobile-nav"> <!-- ... --></nav><!-- This section uses a complex nested grid layout. The outer grid has 3 columns, and the middle column contains a nested 2-row grid.--><div class="complex-layout"> <!-- ... --></div>
Comments should explain “why” rather than “what” when the code itself clearly
shows what’s happening.
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.