Skip to content

A complete guide to modern HTML best practices, including syntax rules, semantic elements, accessibility tips, SEO guidelines, and clean code formatting — based on W3Schools + professional standards.

Notifications You must be signed in to change notification settings

elyas-saberi/html-best-practices-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 

Repository files navigation

🌐 HTML Best Practices — Full Guide (Based on W3Schools & Modern Standards)

A complete guide to modern HTML best practices, including syntax rules, semantic elements, accessibility tips, SEO guidelines, and clean code formatting — based on W3Schools + professional standards.


📑 Table of Contents


🧩 Introduction

This document provides clean, modern best practices for writing HTML.
It expands on the guidelines from W3Schools and adds professional standards used in production websites.


📏 General Syntax Rules

✔ Use lowercase for tags

<p>This is correct</p>
<P>This is NOT recommended</P>

✔ Close all tags

<br>
<img src="img.jpg" alt="Description">

✔ Always indent properly

Use 2 or 4 spaces, but never tabs.

VSCode can automatically insert spaces when you press the Tab key.


🧱 HTML Elements

✔ Nest elements properly

Correct:

<p><strong>Important text</strong></p>

Incorrect:

<p><strong>Important text</p></strong>

✔ Avoid unnecessary <div> usage

Use semantic tags such as <header>, <main>, <footer>, <nav>, <section>, <article>, <aside>.

Read more: Stop Using <div> Everywhere


🔤 Attributes

✔ Always use lowercase

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

✔ Use quotes around attribute values

<input type="text" value="John">

✔ Include required attributes

<img src="cat.jpg" alt="A cute cat">

🔘 Boolean Attributes

Correct:

<input type="checkbox" checked>

Avoid:

<input type="checkbox" checked="checked">

💬 Quotations

Use double quotes for attributes.

✔ Recommended:

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

💬 HTML Comments

✔ Use comments to describe code meaningfully

<!-- Navigation bar -->
<nav>...</nav>

✘ Avoid over-commenting trivial code.


🧠 HTML Head Best Practices

Always include:

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your Page Title</title>

Recommended additions:

<meta name="description" content="Short SEO-friendly description.">
<link rel="stylesheet" href="style.css">

📁 File Structure & Naming

project/
│ README.md
│ index.html
│ style.css
│ script.js
└── assets/
    ├── images/
    ├── icons/
    └── fonts/

Lowercase + hyphens:
my-website-page.html


✨ Code Formatting Tips

  • Use consistent indentation
  • Keep one element per line
  • Use blank lines between logical sections
  • Format attributes on new lines when long

Example:

<img 
  src="large-photo.jpg" 
  alt="Mountain landscape" 
  loading="lazy"
>

⚠️ Avoid Deprecated Tags

  • <center>
  • <font>
  • <marquee>

Use CSS instead.


🔲 Wrap Block Elements Correctly

Incorrect:

<span><div>Invalid</div></span>

Correct:

<div><span>Valid</span></div>

💻 Keep CSS & JS External

CSS:

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

JS:

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

🔍 Validate HTML

W3C Validator: https://validator.w3.org/


♿ Accessibility Tips

  • Add alt text to images
  • Use <label> with <input>
  • Use semantic HTML
  • Ensure color contrast
  • Add ARIA roles only when necessary

Example:

<label for="email">Email</label>
<input id="email" type="email">

🔍 SEO Best Practices

Use meaningful headings:

<h1>Main page title</h1>
<h2>Section title</h2>

Do not use headings for visual styling only (e.g., bold text).

Use descriptive links (avoid "click here").

✘ Incorrect:

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

If all the links say “Click here,” they become meaningless:

  • Click here
  • Click here
  • Click here

✔ Correct:

<p><a href="#">Learn more</a> about web accessibility.</p>

Descriptive ones are clear:

  • Download the accessibility guide
  • Read HTML best practices
  • View pricing

🚀 Performance Tips

  • Use loading="lazy" on images
  • Compress assets
  • Minimize CSS & JS
  • Preload critical assets
  • Keep DOM small

📋 Summary Table

Category Best Practice
Doctype Use <!DOCTYPE html>
Element Names Use lowercase
Attributes Use lowercase & quotes
Boolean Attributes Omit value
Encoding UTF-8
Semantic Tags Use proper structure
Deprecated Tags Avoid them
Comments Use proper comments
Block Elements Wrap properly
File Names lowercase + hyphen
Indentation Consistent
CSS/JS External files
Validation W3C Validator

About

A complete guide to modern HTML best practices, including syntax rules, semantic elements, accessibility tips, SEO guidelines, and clean code formatting — based on W3Schools + professional standards.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published