Falytom logoFALYTOM
Games
ToolsUI ComponentsServicesProductsAbout
LoginSign Up
menu
  1. Home
  2. /Web Development
  3. /Html
  4. /Html Best Practices
Your Progress0%
0 of 76 completed

Topics

Getting Started

  • What is HTML?
  • How the Web Works
  • Setting Up Your Environment
  • Your First HTML Page

HTML Document Structure

  • The DOCTYPE Declaration
  • The HTML Element
  • The Head Section
  • Meta Tags
  • The Body Element
  • HTML Comments

Text and Formatting

  • Headings (h1-h6)
  • Paragraphs
  • Line Breaks and Horizontal Rules
  • Text Formatting (Bold, Italic, etc.)
  • Quotations and Citations
  • Preformatted Text and Code
  • Subscript and Superscript

Links and Navigation

  • Creating Links
  • Absolute vs Relative URLs
  • Link Targets and Attributes
  • Email and Phone Links
  • Internal Page Links

Images and Media

  • Adding Images
  • Image Attributes
  • Image Formats
  • Figure and Figcaption
  • Responsive Images
  • Audio Elements
  • Video Elements
  • Embedding Content

Lists

  • Unordered Lists
  • Ordered Lists
  • Description Lists
  • Nested Lists

Tables

  • Table Basics
  • Table Headers and Footers
  • Spanning Columns and Rows
  • Table Captions
  • Styling Tables

Forms - Part 1 (Basics)

  • Introduction to Forms
  • Text Input Fields
  • Labels and Placeholders
  • Textarea Element
  • Checkboxes
  • Radio Buttons

Forms - Part 2 (Advanced)

  • Select Dropdowns
  • File Upload Inputs
  • HTML5 Input Types
  • Form Validation
  • Form Buttons
  • Fieldsets and Legends
  • Form Accessibility

Semantic HTML

  • What is Semantic HTML?
  • Header and Footer
  • Nav Element
  • Main and Article
  • Section and Aside
  • Div vs Semantic Elements
  • Time and Mark Elements

HTML5 Features

  • HTML5 Overview
  • Canvas Element
  • SVG in HTML
  • Geolocation API
  • Drag and Drop
  • Local Storage
  • Web Workers

Web Accessibility

  • Introduction to Accessibility
  • ARIA Attributes
  • Keyboard Navigation
  • Alt Text Best Practices
  • Color Contrast and Typography
  • Testing for Accessibility

Best Practices & SEO

  • HTML Best Practices
  • SEO Fundamentals
  • Meta Tags for SEO
  • Structured Data

HTML Best Practices

Writing clean, maintainable HTML

HTML best practices are guidelines and conventions that help you write clean, maintainable, and professional code. Following these practices makes your code easier to read, debug, and collaborate on, while also improving performance, accessibility, and SEO.

Document Structure

Always Use DOCTYPE

Start every HTML document with the HTML5 DOCTYPE declaration:

doctype.html
<!-- ✅ ALWAYS include DOCTYPE -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Page Title</title>
</head>
<body>
  <!-- Content -->
</body>
</html>

<!-- ❌ NEVER omit DOCTYPE -->
<html>
  <!-- This triggers quirks mode in browsers -->
</html>

Always Specify Language

language.html
<!-- ✅ GOOD: Specify language -->
<html lang="en">

<!-- ✅ GOOD: For multilingual content -->
<html lang="en">
<body>
  <p>This is English text.</p>
  <p lang="es">Este es texto en español.</p>
  <p lang="fr">Ceci est du texte en français.</p>
</body>
</html>

<!-- ❌ BAD: Missing language -->
<html>

Set Character Encoding

charset.html
<!-- ✅ GOOD: UTF-8 as first meta tag -->
<head>
  <meta charset="UTF-8">
  <title>Page Title</title>
</head>

<!-- ❌ BAD: Missing charset or wrong position -->
<head>
  <title>Page Title</title>
  <meta charset="UTF-8"> <!-- Should be first -->
</head>

Use Semantic HTML

Choose HTML elements based on their meaning, not their appearance:

semantic-html.html
<!-- ❌ BAD: Using divs for everything -->
<div class="header">
  <div class="nav">
    <div class="nav-item">Home</div>
    <div class="nav-item">About</div>
  </div>
</div>
<div class="main">
  <div class="article">
    <div class="article-title">Article Title</div>
    <div class="article-content">Content here...</div>
  </div>
</div>
<div class="footer">
  Footer content
</div>

<!-- ✅ GOOD: Using semantic elements -->
<header>
  <nav>
    <a href="/">Home</a>
    <a href="/about">About</a>
  </nav>
</header>
<main>
  <article>
    <h1>Article Title</h1>
    <p>Content here...</p>
  </article>
</main>
<footer>
  Footer content
</footer>

Proper Nesting and Structure

proper-nesting.html
<!-- ❌ BAD: Improper nesting -->
<p>This is a paragraph
  <div>This div is inside a paragraph</div>
</p>

<!-- ✅ GOOD: Proper nesting -->
<div>
  <p>This is a paragraph</p>
  <div>This div is a sibling</div>
</div>

<!-- ❌ BAD: Unclosed tags -->
<div>
  <p>Paragraph
  <span>Text
</div>

<!-- ✅ GOOD: All tags closed -->
<div>
  <p>Paragraph</p>
  <span>Text</span>
</div>

Use Lowercase for Tags and Attributes

lowercase.html
<!-- ❌ BAD: Mixed or uppercase -->
<DIV CLASS="container">
  <P>Text</P>
</DIV>

<!-- ✅ GOOD: Lowercase -->
<div class="container">
  <p>Text</p>
</div>

<!-- ❌ BAD: Uppercase attributes -->
<img SRC="image.jpg" ALT="Description">

<!-- ✅ GOOD: Lowercase attributes -->
<img src="image.jpg" alt="Description">

Always Quote Attribute Values

quote-attributes.html
<!-- ❌ BAD: Unquoted attributes -->
<div class=container id=main>
<img src=photo.jpg alt=Photo>

<!-- ✅ GOOD: Quoted attributes -->
<div class="container" id="main">
<img src="photo.jpg" alt="Photo">

<!-- ✅ GOOD: Use double quotes consistently -->
<a href="/page" title="Link to page" class="link">Link</a>

Self-Closing Tags

self-closing.html
<!-- ✅ GOOD: HTML5 style (no trailing slash) -->
<img src="photo.jpg" alt="Photo">
<br>
<hr>
<input type="text">
<meta charset="UTF-8">

<!-- ✅ ALSO ACCEPTABLE: XHTML style (with trailing slash) -->
<img src="photo.jpg" alt="Photo" />
<br />
<input type="text" />

<!-- Choose one style and be consistent throughout your project -->

Use Meaningful IDs and Classes

meaningful-names.html
<!-- ❌ BAD: Generic or cryptic names -->
<div class="box1">
<div id="x">
<div class="red-text">

<!-- ✅ GOOD: Descriptive names -->
<div class="product-card">
<div id="main-navigation">
<div class="error-message">

<!-- ❌ BAD: Presentational names -->
<button class="blue-button">
<div class="left-column">

<!-- ✅ GOOD: Functional names -->
<button class="primary-action">
<div class="sidebar">

Consistent Indentation

indentation.html
<!-- ❌ BAD: Inconsistent indentation -->
<div>
<p>Text</p>
  <div>
<span>More text</span>
  </div>
</div>

<!-- ✅ GOOD: Consistent 2-space indentation -->
<div>
  <p>Text</p>
  <div>
    <span>More text</span>
  </div>
</div>

<!-- ✅ ALSO GOOD: Consistent 4-space indentation -->
<div>
    <p>Text</p>
    <div>
        <span>More text</span>
    </div>
</div>

Use Comments Wisely

comments.html
<!-- ✅ GOOD: Helpful section markers -->
<!-- Header Section -->
<header>
  <!-- Navigation -->
  <nav>
    <a href="/">Home</a>
  </nav>
</header>

<!-- Main Content -->
<main>
  <!-- Hero Section -->
  <section class="hero">
    <h1>Welcome</h1>
  </section>
</main>

<!-- ✅ GOOD: Explaining complex code -->
<!-- This hidden div is shown by JavaScript when form is invalid -->
<div id="error-container" hidden>
  <p>Please correct the errors below</p>
</div>

<!-- ❌ BAD: Obvious comments -->
<!-- This is a paragraph -->
<p>Text here</p>

<!-- ❌ BAD: Commented-out code (use version control instead) -->
<!-- <div class="old-design">
  <p>Old content</p>
</div> -->

Always Consider Accessibility

accessibility.html
<!-- ✅ GOOD: Images with alt text -->
<img src="chart.jpg" alt="Sales increased 40% in Q2">

<!-- ✅ GOOD: Form labels -->
<label for="email">Email Address</label>
<input type="email" id="email" name="email">

<!-- ✅ GOOD: Descriptive link text -->
<a href="/report.pdf">Download Q2 Financial Report (PDF, 2MB)</a>

<!-- ✅ GOOD: Proper heading hierarchy -->
<h1>Main Title</h1>
<h2>Section Title</h2>
<h3>Subsection Title</h3>

<!-- ❌ BAD: Missing accessibility attributes -->
<img src="chart.jpg">
<input type="text">
<a href="/report">Click here</a>

Optimize for Performance

Keep Markup Minimal

minimal-markup.html
<!-- ❌ BAD: Unnecessary wrapper divs -->
<div class="container">
  <div class="wrapper">
    <div class="inner">
      <div class="content">
        <p>Text</p>
      </div>
    </div>
  </div>
</div>

<!-- ✅ GOOD: Only necessary elements -->
<div class="container">
  <p>Text</p>
</div>

Load Resources Efficiently

resource-loading.html
<head>
  <meta charset="UTF-8">
  <title>Page Title</title>
  
  <!-- ✅ GOOD: Critical CSS inline -->
  <style>
    /* Critical above-the-fold styles */
  </style>
  
  <!-- ✅ GOOD: Preload important resources -->
  <link rel="preload" href="font.woff2" as="font" crossorigin>
  
  <!-- ✅ GOOD: External CSS with media queries -->
  <link rel="stylesheet" href="styles.css">
  <link rel="stylesheet" href="print.css" media="print">
</head>
<body>
  <!-- Content -->
  
  <!-- ✅ GOOD: Scripts at end of body -->
  <script src="app.js"></script>
  
  <!-- ✅ GOOD: Or use defer in head -->
  <!-- <script src="app.js" defer></script> -->
</body>

Validate Your HTML

Use the W3C Validator to check for errors:

validation.html
<!-- Common validation errors to avoid -->

<!-- ❌ BAD: Duplicate IDs -->
<div id="content">...</div>
<div id="content">...</div>

<!-- ✅ GOOD: Unique IDs -->
<div id="content-main">...</div>
<div id="content-sidebar">...</div>

<!-- ❌ BAD: Missing required attributes -->
<img src="photo.jpg">

<!-- ✅ GOOD: All required attributes present -->
<img src="photo.jpg" alt="Description">

<!-- ❌ BAD: Obsolete elements -->
<font color="red">Text</font>
<center>Centered text</center>

<!-- ✅ GOOD: Modern alternatives -->
<span style="color: red;">Text</span>
<div style="text-align: center;">Centered text</div>

Separation of Concerns

separation.html
<!-- ❌ BAD: Inline styles and scripts -->
<div style="color: red; font-size: 20px;" onclick="alert('Clicked')">
  Click me
</div>

<!-- ✅ GOOD: Separate CSS and JavaScript -->
<div class="alert-button" id="alertBtn">
  Click me
</div>

<!-- In external CSS file -->
<style>
.alert-button {
  color: red;
  font-size: 20px;
}
</style>

<!-- In external JS file -->
<script>
document.getElementById('alertBtn').addEventListener('click', function() {
  alert('Clicked');
});
</script>

Mobile-First Viewport

viewport.html
<!-- ✅ ALWAYS include viewport meta tag -->
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Responsive Page</title>
</head>

<!-- ❌ NEVER do this (prevents zooming) -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

Organize Your Code

organization.html
<!DOCTYPE html>
<html lang="en">
<head>
  <!-- Meta Information -->
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <!-- Page Title and Description -->
  <title>Page Title</title>
  <meta name="description" content="Page description">
  
  <!-- Favicon -->
  <link rel="icon" href="/favicon.ico">
  
  <!-- Stylesheets -->
  <link rel="stylesheet" href="styles.css">
  
  <!-- Preload/Prefetch -->
  <link rel="preload" href="font.woff2" as="font" crossorigin>
</head>
<body>
  <!-- Skip Link -->
  <a href="#main" class="skip-link">Skip to main content</a>
  
  <!-- Header -->
  <header>
    <!-- Navigation -->
    <nav>
      <!-- Nav content -->
    </nav>
  </header>
  
  <!-- Main Content -->
  <main id="main">
    <!-- Page content -->
  </main>
  
  <!-- Footer -->
  <footer>
    <!-- Footer content -->
  </footer>
  
  <!-- Scripts -->
  <script src="app.js"></script>
</body>
</html>

Boolean Attributes

boolean-attributes.html
<!-- ✅ GOOD: Boolean attributes without value -->
<input type="checkbox" checked>
<input type="text" disabled>
<script src="app.js" defer>
<video autoplay muted>

<!-- ✅ ALSO ACCEPTABLE: With value -->
<input type="checkbox" checked="checked">
<input type="text" disabled="disabled">

<!-- ❌ BAD: Wrong values -->
<input type="checkbox" checked="true">
<input type="text" disabled="false">

Avoid Obsolete Elements and Attributes

obsolete.html
<!-- ❌ OBSOLETE: Don't use these -->
<font color="red">Text</font>
<center>Centered</center>
<marquee>Scrolling text</marquee>
<blink>Blinking text</blink>
<frame>
<frameset>

<!-- ❌ OBSOLETE: Attributes -->
<body bgcolor="white">
<table border="1">
<div align="center">

<!-- ✅ USE: Modern alternatives with CSS -->
<span style="color: red;">Text</span>
<div style="text-align: center;">Centered</div>
<div class="scrolling-text">Scrolling text</div>

Common Mistakes to Avoid

  • Missing DOCTYPE declaration
  • Missing or incorrect lang attribute
  • Missing charset meta tag
  • Using div and span for everything instead of semantic elements
  • Inline styles instead of external CSS
  • Missing alt attributes on images
  • Duplicate IDs
  • Improper nesting of elements
  • Unclosed tags
  • Mixing HTML versions or standards
  • Not validating HTML
  • Poor indentation and formatting

HTML Best Practices Checklist

Before Publishing:

  • ✓ DOCTYPE declaration present
  • ✓ Language specified in html tag
  • ✓ Character encoding declared
  • ✓ Semantic HTML elements used appropriately
  • ✓ All tags properly closed and nested
  • ✓ Lowercase tags and attributes
  • ✓ All attribute values quoted
  • ✓ Meaningful class and ID names
  • ✓ Consistent indentation
  • ✓ Images have alt attributes
  • ✓ Forms have labels
  • ✓ Proper heading hierarchy
  • ✓ No inline styles or scripts (when possible)
  • ✓ Viewport meta tag included
  • ✓ Code validated with W3C validator
  • ✓ Accessibility considered

Key Takeaways

  • Always start with DOCTYPE and specify language
  • Use semantic HTML elements based on meaning, not appearance
  • Keep code clean with consistent formatting and indentation
  • Quote all attribute values and use lowercase
  • Validate your HTML to catch errors early
  • Separate structure (HTML), presentation (CSS), and behavior (JS)
  • Always consider accessibility from the start
  • Use meaningful names for IDs and classes
  • Keep markup minimal and avoid unnecessary nesting
  • Include viewport meta tag for responsive design

What's Next?

Now that you understand HTML best practices, you can write clean, professional code that's easy to maintain. In the next lesson, we'll explore SEO fundamentals and how to optimize your HTML for search engines.

Know someone who'd find this guide helpful? Please share!

Previous
Testing for Accessibility
Next
SEO Fundamentals

Never Miss a New HTML Tutorial

Join 1,000+ developers learning HTML step-by-step. Get new tutorials, tips, and exclusive resources delivered to your inbox - completely FREE.

You might also like

UI Components

Ready-made components for your projects

Developer Tools

Boost productivity with handy generators

HTML Game

Learn HTML concepts through interactive gameplay

Join Our Web Development Training Program

Master frontend & backend development with hands-on projects.

Menu

Topics

Getting Started

  • What is HTML?
  • How the Web Works
  • Setting Up Your Environment
  • Your First HTML Page

HTML Document Structure

  • The DOCTYPE Declaration
  • The HTML Element
  • The Head Section
  • Meta Tags
  • The Body Element
  • HTML Comments

Text and Formatting

  • Headings (h1-h6)
  • Paragraphs
  • Line Breaks and Horizontal Rules
  • Text Formatting (Bold, Italic, etc.)
  • Quotations and Citations
  • Preformatted Text and Code
  • Subscript and Superscript

Links and Navigation

  • Creating Links
  • Absolute vs Relative URLs
  • Link Targets and Attributes
  • Email and Phone Links
  • Internal Page Links

Images and Media

  • Adding Images
  • Image Attributes
  • Image Formats
  • Figure and Figcaption
  • Responsive Images
  • Audio Elements
  • Video Elements
  • Embedding Content

Lists

  • Unordered Lists
  • Ordered Lists
  • Description Lists
  • Nested Lists

Tables

  • Table Basics
  • Table Headers and Footers
  • Spanning Columns and Rows
  • Table Captions
  • Styling Tables

Forms - Part 1 (Basics)

  • Introduction to Forms
  • Text Input Fields
  • Labels and Placeholders
  • Textarea Element
  • Checkboxes
  • Radio Buttons

Forms - Part 2 (Advanced)

  • Select Dropdowns
  • File Upload Inputs
  • HTML5 Input Types
  • Form Validation
  • Form Buttons
  • Fieldsets and Legends
  • Form Accessibility

Semantic HTML

  • What is Semantic HTML?
  • Header and Footer
  • Nav Element
  • Main and Article
  • Section and Aside
  • Div vs Semantic Elements
  • Time and Mark Elements

HTML5 Features

  • HTML5 Overview
  • Canvas Element
  • SVG in HTML
  • Geolocation API
  • Drag and Drop
  • Local Storage
  • Web Workers

Web Accessibility

  • Introduction to Accessibility
  • ARIA Attributes
  • Keyboard Navigation
  • Alt Text Best Practices
  • Color Contrast and Typography
  • Testing for Accessibility

Best Practices & SEO

  • HTML Best Practices
  • SEO Fundamentals
  • Meta Tags for SEO
  • Structured Data
Falytom logoFALYTOM

Learn technologies like HTML, CSS, JavaScript, React, NodeJS and more through interactive games, tutorials, components and free tools. Transform your businesses with AI chatbots, SEO-optimized websites, and professional development services.

© 2025 Tomilola Group
  • Facebook logo
  • Twitter logo
  • Instagram logo
  • LinkedIn logo
  • GitHub logo
  • YouTube logo