HTML stands for HyperText Markup Language. It is a standard markup language for documents to be in a web browser. It can be along with Cascading Style Sheets (CSS) for the look and JavaScript (JS) for the behavior.
The first version of HTML was written by Tim Berners-Lee in 1993. Today, HTML has the fifth version. I remember the moment I coded HTML5. It was in 2013 for my final project and research paper. It was a social network. HTML5 was still new and not many web browsers support it yet.
Perhaps many of you have heard that content is a king and design is a queen. HTML is a content. It is not a programming language. It is easy to learn. The design or CSS makes a great experience for users when reading HTML. The HTML should be easy to read by machines such as search engines and humans. That's why Semantic HTML is important. Good semantic HTML also improves the accessibility of web documents. For example, screen readers can read the document correctly.
There are many resources out there that can help you learn HTML. I'm here to show you common mistakes that people love to make. They are not just about Semantic HTML. I hope this note can help or improve how to code HTML. Feel free to leave me a comment if I missed something.
First, let me remind you how to start the HTML. The <!DOCTYPE html> defines that this document is an HTML5 document. I told you that today is the HTML5 era. The <head> contains meta information. The <body> is for the content.
I used two headings here. <h1> and <h2>. <h1> is important on every page and should contain only one. <h2> is the subtitle of the page and it could be many on every page. You can use <h3> as a subtitle of the <h2> and so on until <h6>.
I used rel="noopener noreferrer" for the link. The noopener instructs the browser to navigate to the target resource without granting the new browsing context access to the document that opened it. The noreferrer instructs the browser to omit the Referer header and otherwise leak no referrer information. It is a recommendation to get an SEO score.
I used the alt tag to show text if the picture doesn't load and figure to show the picture as a figure of the page.
The <strong> and <b> will bold the text. But they have different meanings in search engines. The <strong> means it is important.
Yeah, that's how to make a nested list. You put the <ul> or <ol> before the </li> tag.
The <header>, <footer>, <section>, <article>, and <nav> are from HTML5. Today, W3C recommends them. I still found some developers are not familiar with them. The code above just an example how to use them.
<!DOCTYPE html><html><head><title>Bad Page</title></head><body><h1>Bad Page</h1><p>A common mistake.</p><div><div><ahref="#"><div><imgsrc="https://picsum.photos/seed/picsum/300"alt="Item 1"></div><div>Item 1</div><div>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div></a></div><div><ahref="#"><div><imgsrc="https://picsum.photos/seed/picsum/300"alt="Item 2"></div><div>Item 2</div><div>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div></a></div><div><ahref="#"><div><imgsrc="https://picsum.photos/seed/picsum/300"alt="Item 3"></div><div>Item 2</div><div>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div></a></div></div><div><ahref="#">Page 1</a><ahref="#">Page 2</a><ahref="#">Page 3</a></div></body></html>
A common mistake some developers make. Sometimes, I found some images without the alt attribute. As we see in the code above, the a tag is an inline tag but covers div tags which are block tags.
<!DOCTYPE html><html><head><title>Good Page</title></head><body><h1>Good Page</h1><p>It is better.</p><ul><li><figure><ahref="#"><imgsrc="https://picsum.photos/seed/picsum/300"alt="Item 1"></a></figure><p><ahref="#">Item 1</a></p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p></li><li><figure><ahref="#"><imgsrc="https://picsum.photos/seed/picsum/300"alt="Item 2"></a></figure><p><ahref="#">Item 2</a></p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p></li><li><figure><ahref="#"><imgsrc="https://picsum.photos/seed/picsum/300"alt="Item 3"></a></figure><p><ahref="#">Item 3</a></p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p></li></ul><ol><li><ahref="#">Page 1</a></li><li><ahref="#">Page 2</a></li><li><ahref="#">Page 3</a></li></ol></body></html>
The code above is better than the previous one. Now, the machines and humans know that there is a list of items and a list of pages. The inline tags should be in block tags.
Style
1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html><html><head><title>Style</title><style>.success{color:green}</style></head><body><p><spanstyle="color:red">Inline</span> styles are not recommended.</p><p><spanclass="success">Classes</span> are better.</p></body></html>
CSS inline styles are mostly created by WYSIWYG (What You See Is What You Get) editors. They are bad because they have the potential to create repetition. CSS styles should be in CSS classes. The second paragraph for an example.
Comment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html><html><head><title>Comment</title></head><body><!-- A good comment is to show a piece of information. --><!--
This is a very bad comment for HTML.
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ultrices pulvinar nulla, id gravida felis mattis vitae. Pellentesque vel turpis odio. Curabitur risus elit, dignissim sodales purus eget, imperdiet sollicitudin sem. Quisque ac mi diam. Cras aliquet metus nec purus egestas luctus. Mauris condimentum commodo neque, a lacinia risus ultricies sit amet. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce ut blandit metus, ut pulvinar nisl. Morbi venenatis velit nec enim aliquet egestas.</p>
<p>Sed non purus leo. Suspendisse aliquam efficitur diam id pharetra. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin nulla nunc, ultrices nec vestibulum sit amet, tempus ac lectus. Sed vulputate imperdiet justo, at tempus orci ultricies ac. Vivamus ac mollis magna, a pellentesque nibh. Mauris nec ex at metus mollis consectetur sit amet ac lacus. In non porttitor magna. Fusce a odio eget orci tincidunt lacinia. Fusce vel mi sed felis sollicitudin semper et at quam. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut ut lacus eget eros molestie rhoncus. Cras sit amet ipsum maximus, eleifend mi hendrerit, auctor urna.</p>
<p>Aliquam sollicitudin in nulla eu pellentesque. Curabitur consequat justo ut augue egestas, in luctus sem faucibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam semper imperdiet velit, id auctor sapien malesuada dictum. Proin porttitor volutpat rhoncus. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Maecenas varius convallis nibh, non gravida orci. Nam venenatis auctor metus. Suspendisse nunc justo, condimentum id mollis ac, tempus in justo. Etiam porttitor tortor ligula, ut pharetra tellus volutpat in. Nam id risus elementum, hendrerit lacus ut, varius tortor. Vestibulum efficitur malesuada aliquam. Nullam urna enim, cursus vitae dolor a, porttitor blandit lorem. Fusce egestas porta mi, vitae tincidunt ante congue id.</p>
<p>Suspendisse in aliquam orci. Sed vulputate urna ut diam accumsan dignissim. Suspendisse sed semper sem. Proin dapibus, diam iaculis auctor hendrerit, elit ipsum vulputate erat, et fermentum sem nibh id turpis. Nunc placerat ante ut libero ultrices, vitae tempor nisl bibendum. Nam dictum gravida arcu eu condimentum. Integer venenatis imperdiet cursus. Donec urna augue, fringilla nec placerat non, aliquam nec ligula. Praesent et nisi sit amet mauris faucibus efficitur. Pellentesque volutpat eget nunc eget gravida. Curabitur suscipit urna eget velit auctor, at efficitur nisl dictum.</p>
<p>Donec viverra massa in ullamcorper aliquet. Praesent at nisi vestibulum, volutpat mauris ac, pretium odio. Vestibulum ac eros at ex congue commodo eget ac diam. Donec sagittis elementum hendrerit. Maecenas at malesuada nisl, ac blandit nulla. Integer condimentum sit amet magna at semper. Integer nisi mi, porta id ante at, rutrum tempus magna. Nam ipsum libero, volutpat sed ornare vel, ornare id risus.</p>
--></body></html>
A comment in HTML should be a piece of information for developers. Comments are not for dead codes. It is even worse for HTML. It makes the HTML file heavy to load.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"lang="en"xml:lang="en"><head><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"/><title>Email</title></head><body><tableborder="0"cellpadding="0"cellspacing="0"width="100%"><tr><tdalign="center"valign="top"><tableborder="0"cellpadding="0"cellspacing="0"width="600"><tr><tdalign="center"valign="top"><tableborder="0"cellpadding="0"cellspacing="0"width="600"><tr><td><imgsrc="https://picsum.photos/seed/picsum/600/150"alt="Banner"/></td></tr></table></td></tr><tr><tdalign="center"valign="top"><tableborder="0"cellpadding="0"cellspacing="0"width="600"><tr><tdvalign="top"><tableborder="0"cellpadding="20"cellspacing="0"width="100%"><tr><tdvalign="top"><h1>Heading 1</h1><h2>Heading 2</h2><p><strong>Lorem ipsum</strong> dolor sit amet, consectetur adipiscing elit. Aenean ultrices pulvinar nulla, id gravida felis mattis vitae. Pellentesque vel turpis odio. Curabitur risus elit, dignissim sodales purus eget, imperdiet sollicitudin sem. Quisque ac mi diam. Cras aliquet metus nec purus egestas luctus. Mauris condimentum commodo neque, a lacinia risus ultricies sit amet. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce ut blandit metus, ut pulvinar nisl. Morbi venenatis velit nec enim aliquet egestas.</p></td></tr></table></td></tr></table></td></tr><tr><tdalign="center"valign="top"><tableborder="0"cellpadding="10"cellspacing="0"width="600"><tr><tdvalign="top"><tableborder="0"cellpadding="10"cellspacing="0"width="100%"><tr><tdcolspan="2"valign="middle"><ahref="#">unsubscribe from this list</a> | <ahref="#">update subscription preferences</a></td></tr></table></td></tr></table></td></tr></table></td></tr></table></body></html>
Coding an HTML for email isn't much different than coding a website was back in the late '90s. Layouting using table. I used XHTML 1.0 instead of HTML5 for strict purpose and because many recommendations from many email providers. We can check whether our XHTML is valid or not at here, https://validator.w3.org. More information about XHTML 1.0 can be found here, https://www.w3.org/2010/04/xhtml10-strict.html.
The for attribute on the <label> is equal with the id of the <input> instead of name.
Closing
Feel free to clone the repository, https://github.com/aristorinjuang/html, of examples above and try it on your own. You can check my references for more information.