Always Specify alt, width, and height for Images
Always specify the alt attribute for images. This attribute is important if the image for some reason cannot be displayed.
Also, always define the width and height of images. This reduces flickering, because the browser can reserve space for the image before loading.
Good:
Bad:
Spaces and Equal Signs
HTML allows spaces around equal signs. But space-less is easier to read and groups entities better together.
Good:
Bad:
Avoid Long Code Lines
When using an HTML editor, it is NOT convenient to scroll right and left to read the HTML code.
Try to avoid too long code lines.
Blank Lines and Indentation
Do not add blank lines, spaces, or indentations without a reason.
For readability, add blank lines to separate large or logical code blocks.
For readability, add two spaces of indentation. Do not use the tab key.
Good:
Bad:
Good Table Example:
Good List Example:
Never Skip the < title > Element
The < title > element is required in HTML.
The contents of a page title is very important for search engine optimization (SEO)! The page title is used by search engine algorithms to decide the order when listing pages in search results.
The < title > element:
So, try to make the title as accurate and meaningful as possible:
Omitting < html > and < body >?
An HTML page will validate without the < html > and < body > tags:
However, we strongly recommend to always add the < html> and < body> tags!
Omitting < body > can produce errors in older browsers.
Omitting < html > and < body > can also crash DOM and XML software.
Omitting < head >?
The HTML < head > tag can also be omitted.
Browsers will add all elements before < body >, to a default < head > element.
However, we recommend using the < head > tag.
Close Empty HTML Elements?
In HTML, it is optional to close empty elements.
Allowed:
Also Allowed:
If you expect XML/XHTML software to access your page, keep the closing slash (/), because it is required in XML and XHTML.
Add the lang Attribute
You should always include the lang attribute inside the < html > tag, to declare the language of the Web page. This is meant to assist search engines and browsers.
Meta Data
To ensure proper interpretation and correct search engine indexing, both the language and the character encoding < meta charset="charset" > should be defined as early as possible in an HTML document:
Setting The Viewport
The viewport is the user's visible area of a web page. It varies with the device - it will be smaller on a mobile phone than on a computer screen.
You should include the following < meta > element in all your web pages:
This gives the browser instructions on how to control the page's dimensions and scaling.
The width=device-width part sets the width of the page to follow the screen-width of the device (which will vary depending on the device).
The initial-scale=1.0 part sets the initial zoom level when the page is first loaded by the browser.
Here is an example of a web page without the viewport meta tag, and the same web page with the viewport meta tag:
HTML Comments
Short comments should be written on one line, like this:
Comments that spans more than one line, should be written like this:
Long comments are easier to observe if they are indented with two spaces.
Using Style Sheets
Use simple syntax for linking to style sheets (the type attribute is not necessary):
Short CSS rules can be written compressed, like this:
Long CSS rules should be written over multiple lines:
Loading JavaScript in HTML
Use simple syntax for loading external scripts (the type attribute is not necessary):