HTML Style Guide
π§Ύ Why Follow an HTML Style Guide?
A consistent HTML Style Guide ensures your code is readable, maintainable, and scalable. Whether you're working solo or in a team, clean code helps avoid confusion and improves collaboration. πΌπ οΈ
>>βAny fool can write code that a computer can understand. Good programmers write code that humans can understand.β β Martin Fowler π¨βπ»
π Naming Conventions
- β Use lowercase for all HTML element names: <div>, <section>
- π‘ Use hyphens (not underscores or camelCase) for class names: menu-item
- π IDs should also be lowercase and descriptive: main-header
π§Ή Code Formatting
- β Use proper indentation (usually 2 or 4 spaces)
- π§± Nest elements properly for clarity
- β Always close tags (even optional ones like <li>)
Well-Formatted HTML
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>Note
β οΈ Avoid using tab and space interchangeably. Stick to one indentation style consistently.
ποΈ File Organization
- π§Ύ Keep HTML files organized in a logical folder structure
- π Separate CSS and JavaScript into their own folders (e.g., css/, js/)
- πΌοΈ Store images in a dedicated images/ folder
π§ͺ Use Semantic HTML
Always prefer semantic tags like <header>, <main>, <article>, and <footer> over generic <div> when possible.
Note
π‘ Semantic elements help improve accessibility, SEO, and clarity.
π€ Quotes and Attributes
- β Always use double quotes for attribute values: <img src="logo.png" alt="Logo" />
- β Always include the alt attribute for images
π§Ό Commenting
- π¬ Use comments to describe sections: <!-- Main Navigation -->
- β Keep comments brief and meaningful
HTML Comments
<!-- Start of Header -->
<header>
<h1>My Site</h1>
</header>π Best Practices Summary
| Rule | Do | Don't |
|---|---|---|
| Element Names | <section> | <SECTION> |
| Class Naming | card-title | CardTitle or card_title |
| Indentation | 2 or 4 spaces | No indentation or tabs + spaces mixed |
| Images | alt="..." provided | No alt attribute |
π Additional Resources
>>βCode is like humor. When you have to explain it, itβs bad.β β Cory House π