📌 HTML5 Style Guide — Basic Rules and Recommendations

This guide provides recommended rules and practices to make your HTML code more readable, consistent, and easier to maintain.

1. Always Declare the Doctype

The doctype tells the browser that the page uses HTML5.

HTML
<!DOCTYPE html>

This declaration should always be at the very top of the document.

2. Use Lowercase for Tag Names

HTML is case-insensitive, but using lowercase improves readability and consistency.

HTML

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

This declaration should always be at the very top of the document.

3. Always Close HTML Elements

Some HTML elements work without a closing tag, but closing tags are recommended to avoid issues, especially in larger projects.

HTML

<section>
    <p>Paragraph text.</p>
</section> 
                    
<section>
    <p>Paragraph text.
</section> 
                

4. Use Lowercase for Attribute Names

Attributes like href or class should always be lowercase.

HTML

<a href="page.html">Link</a> 
                    
<a hHREF="page.html">Link</a> 
                

5. Always Quote Attribute Values

Although HTML allows unquoted values, using quotes is best practice.

HTML

<img src="image.jpg" alt="Image description"> <!-- Good -->
                    
<img src=image.jpg alt=Image> <!-- Not recommended -->
                

Quotes are required when the value contains spaces.

6. Specify alt, width, and height for Images

  1. alt: Text shown if the image cannot load; important for accessibility.
  2. width & height: Helps browsers allocate space and prevents layout shifts.
HTML
<img src="logo.png" alt="Logo" width="100" height="100">

7. Avoid Spaces Around the Equal Sign

HTML

<link rel="stylesheet" href="style.css"> <!-- Good -->
<link rel = "stylesheet" href = "style.css"> <!-- Not recommended -->
                

8. Avoid Long Lines

Long lines can create horizontal scrolling and reduce readability. Break logical blocks into separate lines.

9. Use Indentation and Blank Lines

Use consistent indentation (e.g., 2 spaces) and blank lines to improve readability, but avoid excessive empty lines.

10. Always Include <title>

The <title> element sets the page title:

  1. Visible in the browser tab
  2. Used when bookmarking
  3. Important for SEO
HTML
<title>Page Title</title>

11. Include <html> and <body>

Pages may technically work without them, but always include <html> and <body> to prevent potential issues in older browsers or DOM scripts.

12. Include <head> for Clarity

Browsers can add <head> automatically, but explicitly writing it keeps the structure clear.

13. Closing Empty Elements

Some elements like <meta>, <img> and <br> are self-closing:

HTML
<meta charset="UTF-8">

If working in XHTML or XML contexts, you may need:

HTML
<meta charset="UTF-8" />

14. Specify Page Language with lang

Add the lang attribute to the <html> tag:

HTML
<html lang="en-us">

15. Metadata

  1. Specify character encoding:
HTML
<meta charset="UTF-8">
  1. Set viewport for responsive design:
HTML
<meta name="viewport" content="width=device-width, initial-scale=1.0">

16. HTML Comments

Use comments to explain code:

HTML

<!-- Short comment -->
<!--
    Long comment block.
    Can span multiple lines.
-->
                

17. Including CSS

Include external CSS files without specifying type (defaults to text/css):

HTML
<link rel="stylesheet" href="styles.css">

18. Including JavaScript

IExternal scripts don’t require type="text/javascript" anymore:

HTML
<script src="script.js""></script>

19. Accessing Elements with JavaScript

Consistent ID and class naming is crucial to prevent errors:

HTML
document.getElementById("Demo")   // Different from "demo"

20. Use Lowercase for File Names

Some servers are case-sensitive, so using lowercase consistently avoids errors:

  1. Examples: logo.png, style.css, script.js

21. Use Correct File Extensions

  1. HTML: .html or .htm
  2. CSS: .css
  3. JavaScript: .js

Correct extensions ensure proper browser and server handling.

22. Default File Names

If no file name is specified in a URL (e.g., www.site.com/), the server usually loads a default file like index.html. Make sure your main page matches this naming convention.

  • Why These Guidelines Matter

  • HTML is forgiving, but following a style guide improves code quality.
  • Readability, consistency, and maintainability are enhanced.
  • Reduces errors in larger projects or team environments.
  • Easier to maintain, update, and scale code in the long term.