HTML Style Guide
In this page:
The Importance of lowercase
Although HTML is case-insensitive, writing tags and attributes in lowercase is the industry standard. It is recommended by the W3C and required by stricter formats like XHTML.
Example: The Importance of lowercase
<p>Correct: lowercase tag names</p>
<P>Incorrect: uppercase tag names</P>
Standardizing Attribute Quotes
HTML allows you to omit quotes around attribute values if they do not contain spaces. However, omitting quotes is a bad habit that can cause layout-breaking errors when values contain spaces or special characters.
Example: Standardizing Attribute Quotes
<!-- Correct: quotes protect values with spaces -->
<img src="my photo.jpg" alt="My photo">
<!-- Bad: omitting quotes breaks multi-word values -->
<img src=my photo.jpg alt=My photo>
Consistent Indentation and Nesting
Indentation does not affect how the browser renders your page, but it is vital for readability. Indenting nested child elements by 2 or 4 spaces makes your page structure easy to read at a glance.
Example: Consistent Indentation and Nesting
<div>
<p>Indented by 2 spaces</p>
</div>
Closing Self-Closing Elements
In modern HTML5, adding a trailing slash to self-closing elements (like or ) is optional. However, choosing one style and sticking to it consistently throughout your project is essential for clean code.
Example: Closing Self-Closing Elements
<br>
<br />
Naming Files and Folders
Web servers are case-sensitive. To prevent broken links when you publish your site, you should always name your files and folders in lowercase, using hyphens instead of spaces to separate words.
Example: Naming Files and Folders
<link rel="stylesheet" href="css/main-styles.css">
- Mixing uppercase and lowercase letters within your HTML tag names.
- Omitting double quotes on attribute values that contain spaces.
- Using spaces instead of hyphens when naming your HTML files and asset folders.
- Write all HTML tag and attribute names in lowercase for professional consistency.
- Always wrap attribute values in double quotes, even if they are single words.
- Use clear indentation and consistent naming patterns (using hyphens for file names) to keep your project organized.
Browsers are very forgiving with poorly formatted HTML, but following a style guide is essential for keeping your code readable and easy to maintain.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: