Following a set of HTML best practices for writing clean and maintainable code not only reduces the development and maintenance time of our websites but also improves site loading speed and positioning.
List of recommendations
Let’s move on to the list of the best tips that greatly simplify the entire process of work.
Doctype (Document Type Declaration or DTD) is part of the HTML code of the page, the “instruction” that declares the document type and explains to the browser in which version of the markup language it is laid out. The doctype must appear at the very top of the document before the <html> tag.
Through the document type declaration or DOCTYPE, we specify the standard used when building an HTML document. Thus, browsers that read this declaration will interpret the document with a particular language and version.
2.- Close tags
One of the first things to learn when learning HTML is that tags must be closed and in the same order, they were opened.
It should be noted that HTML5 introduced empty tags, which are a special case and do not need a closing tag.
3.- Use lower case letters in the label, attribute and value names.
Although this does not affect the performance of the page, it is recommended to always write labels, attributes, and values in lowercase, as this improves the readability of the code.
4.- Use ALT attribute in images
Images should always include this attribute, which, as its name suggests, provides alternative text to the image. Using it improves accessibility because, for example, screen readers rely on this attribute to provide context for images.
The referenced value should be descriptive enough for the content. If the image has no value (like an icon), it’s a good idea to keep including the attribute even if it’s empty.
5. Use consistent padding
This applies to any language: poorly indented or unindented code is largely unreadable.
Indentation is the use of spaces to the right of each line to improve code readability, so it’s an important aspect to always consider when programming.
We have several ways to do this with tabs (Tab) or spaces (1, 2, or whatever is preferred). This is a personal matter for everyone, although it could be based on the Google style guide, which recommends two spaces.
Whatever it is (with tabs, two spaces, three spaces, etc.), it must be consistent throughout the code.
6.- Separate content from the presentation
Don’t use inline styles. HTML is the content, and CSS provides the visual representation of that content. Don’t mix them up.
Mixing them results in slower pages that are difficult to read and maintain. Separating style and content will help the development team make changes and maintain them much more efficiently.
For this reason, it is always recommended to use external style sheets along with classes to apply styles as needed.
7.- Use Semantic Labels and Avoid Divitis
Divitis can be defined as the bad practice of using divs to organize all page content. Although this works, it worsens readability and overloads the code.
It makes more sense to keep our code as clean and small as possible, reducing the use of unnecessary classes and identifiers to bind styles to a single element.
To do this, we can use HTML5 structural elements in an appropriate way, for example:
<header>, <aside>, <section>, <article>, <menu>, <footer>…
8.- Omit the value of boolean attributes.
HTML has attributes known as boolean attributes. According to the standard, and despite what it might seem, these attributes do not allow true or false values, they are represented by including (true) or omitting (false) their name.
With these tips, you can avoid all possible errors and make your code more readable and cleaner. If you need help, then go now to the experts in this field.