BEST PRACTICE12 min25 XP

HTML Best Practices

Learn HTML Best Practices with syntax-highlighted examples, live output, and Shiv AI.

HTML Best Practices

Professional HTML code follows proven standards that make sites faster, more accessible, better ranked by Google, and easier to maintain. These are the rules that separate amateur HTML from production-ready code.

1. Always Declare DOCTYPE and Language

Example — Correct Document Start
<!-- Always: DOCTYPE on line 1, lang on html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Page Title | Site Name</title>
</head>
lang="en" tells Google your language. charset="UTF-8" prevents broken characters. viewport prevents mobile zoom-out.

2. Use Semantic Tags, Not Divs Everywhere

Example — Semantic vs Div Soup
<!-- BAD: div soup - Google understands nothing -->
<div class="header"><div class="nav">...</div></div>
<div class="content"><div class="article">...</div></div>

<!-- GOOD: semantic HTML - Google reads this clearly -->
<header><nav>...</nav></header>
<main><article>...</article></main>
Semantic HTML is both an SEO and accessibility win. Use the right tag for the job every time.

3. Images Must Have alt, width, height

Example — Correct Image Tag
<!-- BAD -->
<img src="hero.jpg"/>

<!-- GOOD -->
<img
  src="hero.webp"
  alt="Students learning HTML at CIWeb"
  width="800"
  height="400"
  loading="lazy"
/>
alt = SEO + accessibility. width/height = no layout shift. loading=lazy = faster load. All three are required.

4. Links Must Be Descriptive

Example — Good vs Bad Link Text
<!-- BAD: "click here" means nothing to Google -->
<a href="/html-course">Click here</a>

<!-- GOOD: Google reads this as a ranking signal -->
<a href="/html-course">Start the free HTML course</a>

<!-- External links must have noopener -->
<a href="https://github.com"
   target="_blank"
   rel="noopener noreferrer">View on GitHub</a>
Google uses anchor text to understand the destination page topic. Descriptive links = better ranking signals.

5. Every Form Input Needs a Label

Example — Labeled Form Fields
<!-- BAD: placeholder disappears when user types -->
<input type="email" placeholder="Email"/>

<!-- GOOD: visible label always present -->
<label for="email">Email Address</label>
<input
  type="email"
  id="email"
  name="email"
  autocomplete="email"
  required/>
autocomplete helps browsers fill fields faster. required prevents empty submission. label stays visible as user types.

The Complete Best Practices Checklist

Production HTMLChecklist
DOCTYPE + langAlways on every single page. No exceptions
charset + viewportFirst two meta tags in every head section
Semantic tagsheader nav main article section aside footer. Not div soup
One h1 per pageClear primary topic signal for Google
img alt + sizealt for SEO, width+height for Core Web Vitals, loading=lazy
Descriptive linksNever "click here". noopener on external links
Labeled formsEvery input has a label. autocomplete on personal fields
Validate your HTMLUse validator.w3.org before deploying
Test accessibilityTab through your page with keyboard. Use a screen reader
Mobile firstTest on actual phone. Google indexes mobile version first
Google Lighthouse

Run Google Lighthouse (in Chrome DevTools) on your pages. It scores Performance, SEO, Accessibility, and Best Practices out of 100. Aim for 90+ on all four. This is the industry standard HTML quality test.

Why These Rules Exist

Every best practice was created as a solution to a real problem: broken layouts on mobile, blind users unable to use forms, Google unable to understand page structure, security attacks via open redirects. The rules exist because developers learned from painful mistakes at scale.

CIWeb HTML Course • 22 Lessons • Free

Explain with Analogies

Shiv AI explains this lesson using only real-life analogies. No code at all.

Click the Explain tab to generate analogies automatically.
index.html
Live Preview
Start coding - Shiv AI monitors automatically

What does Google Lighthouse measure?

ExplanationGoogle Lighthouse scores your page on four pillars: Performance, SEO, Accessibility, and Best Practices — all out of 100. Aim for 90+ on all four before deploying any website.
Build Challenge
Best-Practice Portfolio
Build a complete portfolio page following all HTML best practices: DOCTYPE, semantic tags, proper images, descriptive links, and labeled forms.
Requirements (6 to pass)
DOCTYPE
semantic tags
img with alt
descriptive link
labeled form
meta description
solution.html
Preview
Community Wall
Lesson 21
0/500
0/20 lines
as
Final Project Showcase
Lesson 22 only

Support CIWeb + Get +50 AI Messages

Just Rs.11 keeps CIWeb free for everyone and gets you 50 extra Shiv AI messages.

AI
Shiv AI
Online