How often have you wrote a block of HTML without realising the code you've written might not be ideal?
HTML has always been that kid in the corner where nobody talks to because
CSS have always stolen the attention.
Now keep that picture in your mind cause I am going to go through some simple tips which together, make a difference and help bring that kid in the centre again 😁.
These are part of a perspective of creating a clean, maintainable and scalable code, that will make a good use of the semantic markup elements of
HTML5 and that will render correctly in supported browsers.
So just to stop me from going on and on about why, let's have a look at the what.
Starting from the top of your
index.html, make sure you're declaring a
DOCTYPE. This will activate the standard mode in all browsers and let them know how the document should be interpreted. Keep in mind that
DOCTYPE is not an HTML element.
HTML5 it looks like:
Curious to find out more about other doc types? Have a look at this reference document.
Some tags are optional in HTML5, mostly because the element is implicitly present. Believe it or not, you can omit the
<html> tag and the page will render just fine.
<!DOCTYPE HTML> <head> <title>Hello</title> </head> <body> <p>Welcome to this example.</p> </body> </html>
Above is a valid
HTML, however, there are some cases where you can't do so, such as tags followed by comments:
<!DOCTYPE HTML> <!-- where is this comment in the DOM? --> <head> <title>Hello</title> </head> <body> <p>Welcome to this example.</p> </body> </html>
Above is invalid because the parse tree changes due to comment being outside of
You should always consider closing your tags because some browsers will have trouble rendering your page. However, it's recommended to keep those for readability and other reasons which I will go through later.
<div id="example"> <img src="example.jpg" alt="example" /> <a href="#" title="test">example</a> <p>example</p> </div>
Above are all valid tags. But there are also some exceptions 👇🏼 to this rule.
Self-closing tags are valid, but not required. These elements include:
<br>, <hr>, <img>, <input>, <link>, <meta>, <area>, <base>, <col>, <command>, <embed>, <keygen>, <param>, <source>, <track>, <wbr>
Note: Normal elements can never have self-closing tags.
Above is invalid obviously.
Define your character set upfront. It is best practice to put it right on top inside
<head> <title>This is a super duper cool title, right 😥?</title> <meta charset="utf-8"> </head>
Above is invalid and title wouldn't render properly. Instead move the character set deceleration on top.
<head> <meta charset="utf-8"> <title>This is a super duper cool title, right 😃?</title> </head>
Another reason to not omit the optional tags is when using attributes. In this case you can and you should define the language of your web page. This is really important for accessibility and searching.
<html lang="fr-CA"> ... </html>
Never, never, ever omit the title tag. It's super bad for accessibility, and I will personally never use your site because I can't find it 2 seconds after opening it and 20 more tabs later 😁 (the browser tab won't have anything to show).
This is a really useful tag which should be used with cautious. This will set the base URL for the app. Once set, all links will be relative to this base URL, which might cause some unwanted behaviour:
<base href="http://www.example.com/" />
With above set,
href="#internal" will be interpreted as
href="example.org" will be interpreted as
This meta tag is very useful although it's not strictly part of best practices. This is super useful for search engines when they crawl your site.
<meta name="description" content="HTML best practices">
Although you can create your UX engineer's wire frames with just
divs, it doesn't mean you should. Semantic HTML gives meaning to your page rather than just presentations. Tags like
nav, etc are all semantic tags. If you use a
p tag, users will know this represents a paragraph of text and browsers know how to represent them.
Semantic HTML is way beyond the scope of this article. But you should defo check them out and use them as if they're your pen when you're writing. Can you write without a pen ❓ (or mouse 😁)?
<hr> is not a formatting element, so stop using it to format your content. In HTML5 this tag represents a thematic break of your content. A proper use of it might look like this:
<p>Paragraph about puppies</p> <p>Paragraph about puppies' favourite foods</p> <p>Paragraph about puppies' breeds</p> <hr> <p>Paragraph about why I am shaving my head 😂</p>
title attribute is a powerful tool which can help clarify an action or the purpose of an element on the page like a tooltip. However, it's not interchangeable with other attributes like
alt on an image.
From the HTML5 spec:
Relying on the
titleattribute is currently discouraged as many user agents do not expose the attribute in an accessible manner as required by this specification (e.g., requiring a pointing device such as a mouse to cause a tooltip to appear, which excludes keyboard-only users and touch-only users, such as anyone with a modern phone or tablet).
I've faced many code bases where there are a mix of both in the markup. This is not good especially if you are using a framework which depends on single quote like
php and also when
you're using a single quote in a sentence just like I did now. Another reason is to keep it consistent which is always good. Don't do:
<img alt="super funny meme" src='/img/meme.jpg'>
<img alt="super funny meme" src="/img/meme.jpg">
When it comes to having boolean values for attributes, it's recommended to omit those as they don't add any value and also increase your markup's weight.
<audio autoplay="autoplay" src="podcast.mp3"> <!-- instead 👇🏼 --> <audio autoplay src="podcast.mp3">
There is no need to add
type attribute to
style tags. You also get a validation error from some services such as W3C's markup validation tool.
You can use services like W3C's markup validation to make sure you have a valid markup 😍.
What you write in your HTML file is the content, how it looks like is presentation. Leave the presentation to CSS and don't use inline styles. This will benefit both your developers and browsers to digest your mark up.
These are just a tip of the iceberg in terms of small things to keep in mind when writing markup. There are heaps of good resources you can have a look for deeper insights, and I highly recommend you to skim through them at least once.
Hope you've enjoyed the read. And stay classy 😊, with your markup 😁.