BEST PRACTICES10 min25 XP

HTML Best Practices

Learn HTML Best Practices with live examples, interactive editor, and Shiv AI code review.

HTML Best Practices

Writing valid, clean, accessible HTML is a professional skill. These are the rules that distinguish beginner code from production-ready code that employers and users trust.

The 10 Essential Rules

Example 1 — Production-Ready HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>  <!-- 1. Always first -->
  <meta name="viewport" content="width=device-width,initial-scale=1"/>
  <title>Unique, Descriptive Title</title>  <!-- 2. Never empty -->
  <link rel="stylesheet" href="style.css"/>  <!-- 3. CSS in head -->
</head>
<body>
  <header>...</header>  <!-- 4. Semantic tags -->
  <main>
    <h1>One h1 per page</h1>  <!-- 5. ONE h1 -->
    <img
      src="photo.webp"  <!-- 6. WebP format -->
      alt="Descriptive alt text"  <!-- 7. Always alt -->
      width="800" height="400"  <!-- 8. Set dimensions -->
      loading="lazy"  <!-- 9. Lazy load -->
    />
  </main>
  <footer>...</footer>
  <script src="app.js"></script>  <!-- 10. JS at end of body -->
</body>
</html>
These 10 rules cover 90% of what separates a good HTML developer from a beginner. Follow all of them every time.
10 Best Practice Rules
1. DOCTYPE + charsetAlways. On every single file. No exceptions.
2. Non-empty titleUnique, descriptive, 50-60 chars. Shows in tabs and Google.
3. CSS in headNever inline styles for layout. CSS belongs in a file or style tag in head.
4. Semantic HTMLUse header, nav, main, article, section, aside, footer. Avoid div soup.
5. One h1 per pageThe main topic. Google and screen readers treat it specially.
6. WebP images30% smaller than JPEG. Use for all new photos.
7. alt on every imgDescribe what is in the image. Blank alt="" for decorative images.
8. Image dimensionswidth and height prevent layout shift (CLS). Required for Core Web Vitals.
9. loading=lazyOn all images and iframes below the fold. Speeds up initial load.
10. JS at end of bodyScripts block HTML parsing. Place before or use defer attribute.
Core Web Vitals = Google Ranking

Google's ranking algorithm directly measures Largest Contentful Paint (LCP), Cumulative Layout Shift (CLS), and Interaction to Next Paint (INP). HTML best practices — lazy loading, image dimensions, proper structure — directly improve these scores and your search ranking.

CIWeb HTML Course • 22 Lessons • Free

Explain with Analogies

Shiv AI explains this lesson using real-life analogies.

Click Explain tab to generate.
index.html
Preview
Write code then click Deep Analyze

Where should you place script tags for best performance?

ExplanationScript tags without async/defer block HTML parsing when encountered. Placing them before </body> or using the defer attribute ensures HTML is fully parsed before scripts run, making pages load faster.
Build Challenge
Best Practices Page
Build a complete page following all 10 best practices: DOCTYPE, charset, viewport, descriptive title, semantic tags, one h1, images with alt/width/height/loading, and JS at end of body.
Requirements (5 to pass)
DOCTYPE charset viewport
descriptive title
semantic tags
one h1
img with alt width height loading
solution.html
Preview
Community Wall
Lesson 21
0/500
0/20 lines
as
Recent Posts
Lesson 21
AI
Shiv AI
Online