Labs ICT

HTML Style Guide

HTML style guides and best practices help developers write clean, maintainable, and standards-compliant code. Following consistent coding standards improves collaboration, reduces errors, and makes code easier to understand and maintain. This guide covers essential HTML styling practices, naming conventions, and organizational patterns.

HTML Document Structure

Here are the recommended practices for structuring HTML documents:

Document Type and Language

The HTML5 doctype declaration is required for modern HTML documents.

<!-- Always use HTML5 doctype -->
<!DOCTYPE html>

<!-- Specify the language for accessibility and SEO -->
<html lang="en">

<!-- For other languages -->
<html lang="es"> <!-- Spanish -->
<html lang="fr"> <!-- French -->
<html lang="de"> <!-- German -->
<html lang="zh-CN"> <!-- Chinese Simplified -->

Head Section Standards

The <head> section should include essential meta tags, title, and links to resources in a specific order for optimal performance and SEO.

<head>
  <!-- Character encoding (must be first) -->
  <meta charset="UTF-8">
  
  <!-- Viewport for responsive design -->
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <!-- Page title (required, descriptive, under 60 chars) -->
  <title>Page Title - Site Name</title>
  
  <!-- Meta description (150-160 characters) -->
  <meta name="description" content="A brief description of the page content for SEO.">
  
  <!-- Stylesheets (external first, then internal) -->
  <link rel="stylesheet" href="styles.css">
  <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap">
  
  <!-- Favicon -->
  <link rel="icon" href="favicon.ico">
  
  <!-- Preload critical resources -->
  <link rel="preload" href="critical.css" as="style">
  <link rel="preload" href="font.woff2" as="font" type="font/woff2" crossorigin>
  
  <!-- JavaScript (defer non-critical scripts) -->
  <script src="analytics.js" async></script>
</head>

HTML Formatting Standards

Consistent formatting improves code readability and maintainability.

Indentation and Spacing

Use consistent indentation (2 or 4 spaces) and spacing to enhance readability.

<!-- Use 2 or 4 spaces for indentation (be consistent) -->
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Page Title</title>
</head>
<body>
  <div class="container">
    <header>
      <h1>Heading</h1>
      <nav>
        <ul>
          <li><a href="#home">Home</a></li>
          <li><a href="#about">About</a></li>
        </ul>
      </nav>
    </header>
    
    <main>
      <section>
        <h2>Section Title</h2>
        <p>Paragraph content here.</p>
      </section>
    </main>
  </div>
</body>
</html>

Note: if you're using a code editor with auto-formatting, make sure it's configured to use the recommended indentation and spacing.

Attribute Formatting

For readability, place each attribute on a new line for elements with multiple attributes. For simple elements, attributes can be on the same line.

<!-- Good: Attributes on separate lines for readability -->
<img 
  src="image.jpg" 
  alt="Description of image" 
  width="300" 
  height="200" 
  loading="lazy"
  class="responsive-image">

<!-- Acceptable: Single line for simple elements -->
<a href="#home" class="nav-link">Home</a>

<!-- Good: Boolean attributes without values -->
<input type="checkbox" checked disabled required>
<video controls autoplay muted></video>

<!-- Bad: Inconsistent formatting -->
<img src="image.jpg" alt="Description" width="300"
height="200" class="image">

Naming Conventions

Consistent naming conventions for classes, IDs, and other attributes improve code clarity and maintainability.

Class and ID Names

Use meaningful, descriptive names for classes and IDs. Avoid using presentational names that describe how something looks rather than what it represents.

<!-- Use kebab-case for class and ID names -->
<div class="main-header"></div>
<div class="navigation-menu"></div>
<div class="content-wrapper"></div>
<div id="contact-form"></div>

<!-- Be descriptive and meaningful -->
<div class="user-profile-card"></div>
<div class="submit-button primary"></div>
<div class="error-message"></div>

<!-- Avoid: -->
<div class="div1"></div> <!-- Not descriptive -->
<div class="red-text"></div> <!-- Presentational -->
<div class="box"></div> <!-- Too generic -->

Note: IDs should be unique within a page and used for specific elements that require unique styling or JavaScript targeting. For reusable styles, prefer using classes.

BEM Methodology

The Block Element Modifier (BEM) methodology is a popular naming convention for classes that promotes modular and reusable code. It consists of three parts: Block, Element, and Modifier.

<!-- Block Element Modifier (BEM) methodology -->

<!-- Block: Standalone component -->
<div class="card"></div>

<!-- Element: Part of a block (block__element) -->
<div class="card">
  <div class="card__header"></div>
  <div class="card__content"></div>
  <div class="card__footer"></div>
</div>

<!-- Modifier: Variant of a block or element (block--modifier) -->
<div class="card card--featured"></div>
<div class="card__header card__header--large"></div>

<!-- Complete BEM example -->
<button class="btn btn--primary btn--large">
  <span class="btn__icon"></span>
  <span class="btn__text">Submit</span>
</button>

Code Organization

Organizing your HTML code in a logical and consistent manner makes it easier to read and maintain. This includes the order of elements, grouping related content, and structuring components.

Logical Element Order

Follow a logical order when structuring your HTML elements. This typically means starting with the document structure, followed by meta information, then the content in a semantic order.

<!-- Follow a logical order in HTML elements -->
<!-- 1. Document structure -->
<!DOCTYPE html>
<html lang="en">
<head>
  <!-- 2. Meta information (charset first) -->
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <!-- 3. Title -->
  <title>Page Title</title>
  
  <!-- 4. External resources (CSS first, then JS) -->
  <link rel="stylesheet" href="styles.css">
  <script src="script.js" defer></script>
</head>

<body>
  <!-- 5. Page content in semantic order -->
  <header></header>
  <nav></nav>
  <main>
    <article></article>
    <aside></aside>
  </main>
  <footer></footer>
  
  <!-- 6. Scripts at the end (unless async/defer) -->
  <script src="main.js"></script>
</body>
</html>

Component Structure

When building components, structure your HTML in a way that clearly defines the different parts of the component. This makes it easier to style and maintain.

<!-- Organize components logically -->
<!-- Header component -->
<header class="site-header">
  <div class="logo">
    <img src="logo.svg" alt="Site Logo">
  </div>
  <nav class="main-navigation">
    <ul class="nav-list">
      <li class="nav-item">
        <a href="#home" class="nav-link">Home</a>
      </li>
    </ul>
  </nav>
</header>

<!-- Card component -->
<article class="card">
  <header class="card__header">
    <h2 class="card__title">Card Title</h2>
    <time class="card__date" datetime="2024-01-15">Jan 15, 2024</time>
  </header>
  
  <div class="card__content">
    <img src="image.jpg" alt="Card image" class="card__image">
    <p class="card__description">Card description...</p>
  </div>
  
  <footer class="card__footer">
    <a href="#read-more" class="card__link">Read More</a>
  </footer>
</article>

Accessibility Standards

Writing accessible HTML ensures that your content can be used by everyone, including people with disabilities. Follow these best practices to improve accessibility.

Semantic HTML

Use semantic HTML elements to provide meaning and structure to your content. This helps screen readers and other assistive technologies understand the content better.

<!-- Use appropriate semantic elements -->
<header role="banner">
  <h1>Site Title</h1>
  <nav aria-label="Main navigation">
    <ul>
      <li><a href="#home">Home</a></li>
      <li><a href="#about">About</a></li>
    </ul>
  </nav>
</header>

<main role="main">
  <article>
    <h2>Article Title</h2>
    <p>Article content...</p>
  </article>
</main>

<aside role="complementary" aria-label="Sidebar">
  <h2>Related Content</h2>
  <ul>
    <li><a href="#related1">Related Article 1</a></li>
  </ul>
</aside>

<footer role="contentinfo">
  <p>Footer content</p>
</footer>

Image and Link Accessibility

Always provide descriptive alt text for images, use meaningful link text, and ensure that interactive elements are accessible via keyboard.

<!-- Always provide alt text for images -->
<img src="photo.jpg" alt="A beautiful sunset over mountains">

<!-- Decorative images -->
<img src="decoration.png" alt="" role="presentation">

<!-- Images with complex descriptions -->
<img src="chart.png" alt="Sales chart showing 25% growth in Q4">
<figure>
  <img src="complex-chart.png" alt="">
  <figcaption>
    Figure 1: Complex sales data showing quarterly growth trends from Q1 to Q4 2024,
    with a 25% increase in the final quarter compared to the same period last year.
  </figcaption>
</figure>

<!-- Descriptive links -->
<a href="#report" title="Download annual financial report (PDF, 2MB)">
  Download Annual Report
</a>

<!-- Skip links for keyboard navigation -->
<a href="#main-content" class="skip-link">Skip to main content</a>

Performance Best Practices

Optimizing your HTML for performance can improve page load times and user experience. Follow these best practices to enhance the performance of your web pages.

Resource Loading

Use resource hints like preload, prefetch, and preconnect to optimize the loading of critical resources. Also, use async or defer for non-critical JavaScript to prevent blocking the rendering of the page.

<head>
  <!-- Preload critical resources -->
  <link rel="preload" href="critical.css" as="style">
  <link rel="preload" href="hero-image.jpg" as="image">
  <link rel="preload" href="main-font.woff2" as="font" type="font/woff2" crossorigin>
  
  <!-- DNS prefetch for external domains -->
  <link rel="dns-prefetch" href="//fonts.googleapis.com">
  <link rel="dns-prefetch" href="//analytics.google.com">
  
  <!-- Preconnect for important external resources -->
  <link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>
  
  <!-- Async/defer for non-critical JavaScript -->
  <script src="analytics.js" async></script>
  <script src="main.js" defer></script>
  
  <!-- Load stylesheets with media queries -->
  <link rel="stylesheet" href="print.css" media="print">
  <link rel="stylesheet" href="mobile.css" media="(max-width: 768px)">
</head>

Image Optimization

Use appropriate image formats (JPEG for photos, PNG for transparency, SVG for simple graphics) and implement responsive images with srcset and the <picture> element to serve different images based on device capabilities.

<!-- Use appropriate image formats -->
<!-- JPEG for photographs -->
<img src="photo.jpg" alt="Photograph" loading="lazy">

<!-- PNG for transparency -->
<img src="logo.png" alt="Company logo" loading="lazy">

<!-- SVG for simple graphics -->
<img src="icon.svg" alt="Settings icon" loading="lazy">

<!-- Responsive images with srcset -->
<img 
  src="image-medium.jpg" 
  srcset="image-small.jpg 480w,
          image-medium.jpg 768w,
          image-large.jpg 1024w"
  sizes="(max-width: 480px) 480px,
         (max-width: 768px) 768px,
         1024px"
  alt="Responsive image"
  loading="lazy">

<!-- Picture element for different formats -->
<picture>
  <source media="(min-width: 768px)" srcset="image.webp" type="image/webp">
  <source media="(min-width: 768px)" srcset="image.jpg" type="image/jpeg">
  <img src="image-small.jpg" alt="Responsive image">
</picture>

Common Mistakes to Avoid

Avoiding common mistakes in HTML coding can improve the quality and maintainability of your code. Here are some of the most common mistakes to watch out for.

Bad vs Good Examples

Here are some examples of bad and good HTML practices to illustrate the differences:

<!-- Bad: Deprecated elements and inline styles -->
<font color="red" size="3">
  <b>Important text</b>
</font>
<center>
  <table width="100%" border="0">
    <tr><td>Content</td></tr>
  </table>
</center>

<!-- Good: Semantic HTML with CSS classes -->
<div class="alert alert-error">
  <strong class="alert-title">Important text</strong>
</div>
<div class="content-wrapper">
  <main class="main-content">Content</main>
</div>

HTML Validation

Validating your HTML code ensures that it adheres to web standards and is free of syntax errors. This can improve browser compatibility, accessibility, and overall code quality.

Common Validation Issues

Here are some common HTML validation issues and how to fix them:

<!-- Issue: Unclosed tag -->
<div>
  <p>Paragraph text</p>
</div> <!-- Missing closing div tag -->

<!-- Fix: Close all tags properly -->
<div>
  <p>Paragraph text</p>
</div>

<!-- Issue: Invalid nesting -->
<p>
  <div>Block element inside inline element</div>
</p>

<!-- Fix: Proper nesting -->
<div>
  <p>Inline element inside block element</p>
</div>

<!-- Issue: Missing alt attribute -->
<img src="image.jpg">

<!-- Fix: Add alt attribute -->
<img src="image.jpg" alt="Description of image">

Complete Style Guide Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>HTML Style Guide Example - WebDev Academy</title>
  
  <!-- SEO Meta Tags -->
  <meta name="description" content="Complete HTML style guide with best practices, naming conventions, and code organization examples.">
  <meta name="keywords" content="HTML, style guide, best practices, web development, coding standards">
  <meta name="author" content="WebDev Academy">
  
  <!-- Open Graph Meta Tags -->
  <meta property="og:title" content="HTML Style Guide - WebDev Academy">
  <meta property="og:description" content="Learn HTML best practices and coding standards">
  <meta property="og:type" content="article">
  <meta property="og:url" content="https://webdevacademy.com/html-style-guide">
  
  <!-- Stylesheets -->
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap">
  <link rel="stylesheet" href="styles.css">
  
  <!-- Favicon -->
  <link rel="icon" href="favicon.ico">
  <link rel="icon" href="favicon-16x16.png" sizes="16x16">
  <link rel="icon" href="favicon-32x32.png" sizes="32x32">
  
  <!-- Preload Critical Resources -->
  <link rel="preload" href="hero-image.jpg" as="image">
  <link rel="preload" href="main-font.woff2" as="font" type="font/woff2" crossorigin>
  
  <!-- Scripts -->
  <script src="analytics.js" async></script>
</head>

<body>
  <a href="#main-content" class="skip-link">Skip to main content</a>
  
  <header class="site-header" role="banner">
    <div class="container">
      <div class="site-header__logo">
        <a href="#home" aria-label="WebDev Academy home">
          <img src="logo.svg" alt="WebDev Academy" width="200" height="50">
        </a>
      </div>
      
      <nav class="main-navigation" aria-label="Main navigation">
        <ul class="main-navigation__list">
          <li class="main-navigation__item">
            <a href="#home" class="main-navigation__link main-navigation__link--active" aria-current="page">
              Home
            </a>
          </li>
          <li class="main-navigation__item">
            <a href="#tutorials" class="main-navigation__link">
              Tutorials
            </a>
          </li>
          <li class="main-navigation__item">
            <a href="#about" class="main-navigation__link">
              About
            </a>
          </li>
          <li class="main-navigation__item">
            <a href="#contact" class="main-navigation__link">
              Contact
            </a>
          </li>
        </ul>
      </nav>
    </div>
  </header>

  <main id="main-content" class="main-content" role="main">
    <div class="container">
      <section class="hero-section">
        <div class="hero-section__content">
          <h1 class="hero-section__title">
            HTML Style Guide
            <span class="hero-section__subtitle">Best Practices for Clean, Maintainable Code</span>
          </h1>
          
          <p class="hero-section__description">
            Learn the essential HTML coding standards, naming conventions, and organizational patterns 
            that will make your code more readable, maintainable, and professional.
          </p>
          
          <div class="hero-section__actions">
            <a href="#get-started" class="btn btn--primary btn--large">
              Get Started
            </a>
            <a href="#examples" class="btn btn--secondary btn--large">
              View Examples
            </a>
          </div>
        </div>
        
        <div class="hero-section__image">
          <picture>
            <source media="(min-width: 768px)" srcset="hero-large.webp" type="image/webp">
            <source media="(min-width: 768px)" srcset="hero-large.jpg" type="image/jpeg">
            <img 
              src="hero-small.jpg" 
              alt="HTML code example showing clean, well-structured markup"
              loading="lazy"
              class="hero-section__img">
          </picture>
        </div>
      </section>

      <section class="content-section">
        <header class="content-section__header">
          <h2 class="content-section__title">Key Principles</h2>
          <p class="content-section__description">
            Follow these fundamental principles for better HTML code
          </p>
        </header>
        
        <div class="principles-grid">
          <article class="principle-card">
            <div class="principle-card__icon">
              <img src="semantic-icon.svg" alt="Semantic HTML icon" width="48" height="48">
            </div>
            <h3 class="principle-card__title">Semantic HTML</h3>
            <p class="principle-card__description">
              Use appropriate semantic elements to give meaning to your content structure.
            </p>
            <a href="#semantic" class="principle-card__link">Learn More โ†’</a>
          </article>
          
          <article class="principle-card">
            <div class="principle-card__icon">
              <img src="accessibility-icon.svg" alt="Accessibility icon" width="48" height="48">
            </div>
            <h3 class="principle-card__title">Accessibility</h3>
            <p class="principle-card__description">
              Ensure your HTML is accessible to all users, including those with disabilities.
            </p>
            <a href="#accessibility" class="principle-card__link">Learn More โ†’</a>
          </article>
          
          <article class="principle-card">
            <div class="principle-card__icon">
              <img src="performance-icon.svg" alt="Performance icon" width="48" height="48">
            </div>
            <h3 class="principle-card__title">Performance</h3>
            <p class="principle-card__description">
              Optimize your HTML for fast loading and smooth user experience.
            </p>
            <a href="#performance" class="principle-card__link">Learn More โ†’</a>
          </article>
        </div>
      </section>

      <section class="code-examples-section">
        <header class="code-examples-section__header">
          <h2 class="code-examples-section__title">Code Examples</h2>
          <p class="code-examples-section__description">
            See practical examples of well-structured HTML code
          </p>
        </header>
        
        <div class="code-examples">
          <article class="code-example">
            <header class="code-example__header">
              <h3 class="code-example__title">Document Structure</h3>
              <p class="code-example__description">
                Proper HTML5 document structure with semantic elements
              </p>
            </header>
            
            <div class="code-example__content">
              <pre><code><!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>
  <header>Header</header>
  <main>Main Content</main>
  <footer>Footer</footer>
</body>
</html></code></pre>
            </div>
            
            <footer class="code-example__footer">
              <a href="#try-document" class="btn btn--primary">Try It Yourself</a>
            </footer>
          </article>
        </div>
      </section>
    </div>
  </main>

  <aside class="sidebar" role="complementary" aria-label="Additional resources">
    <div class="container">
      <section class="sidebar__section">
        <h3 class="sidebar__title">Quick Links</h3>
        <nav class="sidebar__nav">
          <ul class="sidebar__list">
            <li class="sidebar__item">
              <a href="#structure" class="sidebar__link">Document Structure</a>
            </li>
            <li class="sidebar__item">
              <a href="#formatting" class="sidebar__link">Code Formatting</a>
            </li>
            <li class="sidebar__item">
              <a href="#naming" class="sidebar__link">Naming Conventions</a>
            </li>
            <li class="sidebar__item">
              <a href="#validation" class="sidebar__link">HTML Validation</a>
            </li>
          </ul>
        </nav>
      </section>
      
      <section class="sidebar__section">
        <h3 class="sidebar__title">Resources</h3>
        <ul class="sidebar__list">
          <li class="sidebar__item">
            <a href="#w3c-validator" class="sidebar__link">
              W3C Validator
            </a>
          </li>
          <li class="sidebar__item">
            <a href="#html-spec" class="sidebar__link">
              HTML Specification
            </a>
          </li>
          <li class="sidebar__item">
            <a href="#accessibility-guide" class="sidebar__link">
              Accessibility Guide
            </a>
          </li>
        </ul>
      </section>
    </div>
  </aside>

  <footer class="site-footer" role="contentinfo">
    <div class="container">
      <div class="site-footer__content">
        <section class="site-footer__section">
          <h3 class="site-footer__title">About WebDev Academy</h3>
          <p class="site-footer__description">
            We provide comprehensive web development tutorials and resources 
            to help developers build better websites and applications.
          </p>
        </section>
        
        <nav class="site-footer__nav" aria-label="Footer navigation">
          <h3 class="site-footer__title">Quick Links</h3>
          <ul class="site-footer__list">
            <li class="site-footer__item">
              <a href="#privacy" class="site-footer__link">Privacy Policy</a>
            </li>
            <li class="site-footer__item">
              <a href="#terms" class="site-footer__link">Terms of Service</a>
            </li>
            <li class="site-footer__item">
              <a href="#sitemap" class="site-footer__link">Sitemap</a>
            </li>
            <li class="site-footer__item">
              <a href="#contact" class="site-footer__link">Contact Us</a>
            </li>
          </ul>
        </nav>
        
        <section class="site-footer__section">
          <h3 class="site-footer__title">Connect With Us</h3>
          <address class="site-footer__contact">
            <p>
              Email: <a href="mailto:info@webdevacademy.com" class="site-footer__link">
                info@webdevacademy.com
              </a><br>
              Phone: <a href="tel:+1234567890" class="site-footer__link">
                (123) 456-7890
              </a>
            </p>
          </address>
        </section>
      </div>
      
      <div class="site-footer__bottom">
        <p class="site-footer__copyright">
          © 2024 WebDev Academy. All rights reserved.
        </p>
        <p class="site-footer__credit">
          Built with semantic HTML, modern CSS, and JavaScript
        </p>
      </div>
    </div>
  </footer>

  <!-- Scripts at the end of body for better performance -->
  <script src="main.js" defer></script>
  <script src="analytics.js" async></script>

</body>
</html>

๐Ÿงช Quick Quiz

What is the recommended practice for HTML indentation?