HTML Style Guide
HTML Style Guide
Writing clean and consistent HTML makes your code easier to read, understand, and maintain.
This style guide lists best practices and common conventions for writing well-structured HTML.
Always Declare the Document Type
The document type declaration should always be the first line in an HTML document.
<!DOCTYPE html>
Use Lowercase Element Names
HTML allows both uppercase and lowercase tag names, but lowercase is recommended for readability and consistency.
Good:
<body> <p>This is a paragraph.</p> </body>
Bad:
<BODY> <P>This is a paragraph.</P> </BODY>
Close All HTML Elements
Although some HTML elements do not strictly require closing tags, it is best practice to close all elements.
Good:
<section> <p>This is a paragraph.</p> <p>This is another paragraph.</p> </section>
Bad:
<section> <p>This is a paragraph. <p>This is another paragraph. </section>
Use Lowercase Attribute Names
Attribute names should always be written in lowercase.
Good:
<a href="https://www.w3schools.com/html/">Visit our HTML tutorial</a>
Bad:
<a HREF="https://www.w3schools.com/html/">Visit our HTML tutorial</a>
Always Quote Attribute Values
Attribute values should always be enclosed in quotes for clarity and reliability.
Good:
<table class="striped">
Bad:
<table class=striped>
Specify alt, width, and height for Images
Images should always include an alt attribute and explicit width and height to improve accessibility and layout stability.
<img src="html5.gif" alt="HTML5" style="width:128px;height:128px">
Avoid Extra Spaces Around Equal Signs
Do not add unnecessary spaces around equal signs in attributes.
Good:
<link rel="stylesheet" href="styles.css">
Bad:
<link rel = "stylesheet" href = "styles.css">
Indentation and Readability
Use consistent indentation and blank lines to separate logical sections of code.
Good example:
<body>Famous Cities
Tokyo
Tokyo is the capital of Japan.
London
London is the capital city of England.