HTML Coding Guidelines and Suggestions

Clean HTML code makes reading and understanding easier for others. Some instructions and advice on creating good HTML code are provided below.


Always Declare Document Type

Make sure to declare the document type as the first line in your document. The appropriate HTML document type is :

<!DOCTYPE html>

Use Lowercase Element and Attribute Names

HTML allows for both uppercase and lowercase letters in element and attribute names. However, we recommend to uses lowercase for element and attribute names due to the following considerations:

  • It doesn't look good to mix uppercase and lowercase names.
  • Common practice among developers is to use lowercase.
  • The lowercase format carries a more professional appearance.
  • Writing with lowercase letters is generally more convenient.

Good :-

<!-- Good -->
<p>This is a paragraph.</p>
<div>This is a div.</div>
<a href="https://www.simmanchith.com/">Visit Simmanchith</a>

<!-- Bad -->
<P>This is a paragraph.</P>
<DIV>This is a div.</div>
<a HREF="https://www.simmanchith.com/">Visit Simmanchith</a>

Close All HTML Elements

Closing all HTML elements is not necessary, But, it is highly advisable to ensure the closure of all HTML elements.

<!-- Good -->
  <p>This is a paragraph.</p>
  <div>This is a div.</div>  

  <!-- Bad -->
  <p>This is a paragraph.
  <p>This is a div.

Always Quote Attribute Values

HTML allows for unquoted attribute values. Nevertheless, we suggest using quotes for attribute values due to the following reasons:

  • Typically, programmers utilize quoted attribute values.
  • It is easier to read quoted values.
  • Quotations are necessary when values contain spaces.

Good :-

<!-- Good -->
<img src="computer.jpg" alt="computer-image" />

<!-- Bad -->
<img src="computer.jpg" alt=computer-image />

<!-- Very Bad and It does not work, because there are spaces in the value -->
<img src="computer.jpg" alt=computer image />

Always Specify alt, width, and height for Images

Always enter the alt image attribute. If the image is not displayed, that attribute is important.

Set the width and height of the images also constantly. The browser can reserve space for the picture before loading. This reduces splintering.

<!-- Good -->
<img src="computer.jpg" alt="Computer" style="width:50px;height:50px">

<!-- Bad -->
<img src="computer.jpg">

Spaces and Equal Signs

HTML permits the inclusion of spaces between attribute and value. However, organizing content without unnecessary spaces enhances readability and grouping efficiency.

Good :-

<!-- Good -->
<a href="https://www.simmanchith.com" alt="The best place to learn programming languages">

<!-- Bad -->
<a href = "https://www.simmanchith.com" alt = "The best place to learn programming languages">

Close Empty HTML Elements?

In HTML, some elements do not require closing tags because they are considered self-closing or void elements. These elements are written with a single tag and a forward slash (/) before the closing angle bracket, like this:

<!-- Allowed and OK -->
<img src="img-computer.jpg">
<br>
<hr>

<!-- Also Allowed and Good -->
<img src="img-computer.jpg" />
<br />
<hr />

Keep the closing slash (/) as XML and XHTML are necessary to get your page access by XML/XHTML.


Use Lower Case File Names

File names behave differently on various web servers. For instance, Apache and Unix are case-sensitive, while Microsoft and IIS are not. Mixing uppercase and lowercase letters can lead to issues, especially when switching servers. To prevent problems, always use lowercase file names.

Regarding file names, certain web servers (like Apache and Unix) are case sensitive, meaning "logo.jpg" and "Logo.jpg" are distinct. However, other servers (such as Microsoft and IIS) lack case sensitivity, treating "logo.jpg" and "Logo.jpg" as the same.


FAQ

What is an HTML style guide?

An HTML style guide is a set of guidelines and conventions that define coding standards and best practices for writing HTML code, ensuring consistency, readability, and maintainability across a project or organization.

Why is following an HTML style guide important?

Following an HTML style guide promotes code quality, improves collaboration among developers, enhances code readability, and simplifies maintenance and debugging processes.

What are some common HTML coding conventions?

Some common HTML coding conventions include consistent indentation using spaces or tabs, using lowercase for element and attribute names, closing all tags, using meaningful and descriptive class and ID names, and avoiding inline styles.

How can I ensure consistent indentation in my HTML code?

Consistent indentation can be achieved by using either spaces or tabs for each level of indentation and configuring your editor or IDE to apply the chosen indentation style automatically.

Should I use single quotes or double quotes for HTML attribute values?

It is generally recommended to use double quotes ("") for HTML attribute values to maintain consistency and readability. However, single quotes ('') are also acceptable as long as you are consistent throughout your codebase.

Should I use self-closing tags in HTML?

Self-closing tags, such as <br> or <input>, are required in XHTML, but in HTML5, they are optional. It is generally recommended to omit the closing slash in self-closing tags for consistency and better compatibility.

How should I format HTML comments in my code?

HTML comments should follow a consistent format, such as starting with <!-- and ending with -->. It is recommended to write descriptive comments that explain the purpose or functionality of the code.

Is it necessary to close all HTML tags?

Yes, it is important to close all HTML tags to ensure proper markup and avoid potential rendering issues. Unclosed tags can lead to unexpected results and may cause the browser to misinterpret the document structure.

What are some best practices for using classes and IDs in HTML?

Best practices for using classes and IDs include using descriptive and meaningful names, using classes for styling and grouping related elements, avoiding excessive use of IDs, and keeping the naming conventions consistent throughout the codebase.

Should I use inline styles in my HTML code?

Inline styles should generally be avoided as they can lead to code duplication and maintenance challenges. It is recommended to use external CSS files or embed styles using the <style> tag for better separation of concerns.

How can I ensure accessibility in my HTML code?

To ensure accessibility, you should use semantic HTML elements, provide alternative text for images using the alt attribute, use proper heading structure, label form elements, and follow the Web Content Accessibility Guidelines (WCAG) to make your content more accessible to all users.

Can I use HTML validation tools to check the correctness of my code?

Yes, there are various HTML validation tools available that can help you identify and fix errors or potential issues in your HTML code, ensuring compliance with standards and improving code quality.

Is it necessary to follow a specific HTML style guide?

While following an HTML style guide is not mandatory, it is highly recommended as it promotes consistency, readability, and maintainability. It also helps improve code collaboration among team members and makes code reviews more efficient.