Code has been added to clipboard!

Style Guide and Coding Conventions: Understanding HTML Syntax

Reading time 4 min
Published Jun 22, 2017
Updated Oct 2, 2019

TL;DR – HTML syntax are rules and conventions that define correct usage of the markup language.

Basics of HTML Syntax

The first and most important thing to understand in HTML syntax is the usage of elements and tags. Every HTML element is defined by an opening tag and a closing tag wrapped around their content:

A pair of angle brackets (< and >) surround each tag. The difference between the opening tag and a closing tag is a single forward slash (/). See a simple example of using the HTML <style> tags:

Example
<head>
<style>

/* This is internal styling */
h1 {
    color: indianred;
}

p {
    color: red;
}

</style>
</head>

Tip: the HTML <style> tag will help you use HTML and CSS together to achieve both functionality and design.

These days, every developer is encouraged to use semantic tags. They help HTML5 structure a page better, and that in turn helps you create a nice HTML5 layout for your webpage.

Closing Elements

It is not necessary to close all elements in HTML5. However, if you're writing in XHTML or simply want a more readable and syntactically correct code, you should practice closing all the tags:

Example
<section>
  <p>This element is not closed which can cause problems in the future.
  <p>This is a closed element. The range where it starts and where it ends is clearly defined</p>
</section>

An empty element only has an opening tag and doesn't require closing. However, XHTML requires all elements to be closed. If there is a chance that XML software will be accessing your page in the future, you should close your empty elements, for it may cause inconveniences.

You can make an empty element close itself by adding a forward slash (/) before the second angle bracket (>):

Example
<meta charset="utf-8">   not closed

<meta charset="utf-8"/>  closed

HTML Attributes

You may add various attributes in the opening tag. They are written in name-value pairs with the value wrapped in double quotes:

Example
<track src="subtitles_en.vtt">

Uppercase or Lowercase?

In the code snippet below, you see the document type declaration. It should always be the first line of the document. Although all the examples you see work, HTML syntax rules recommend to stick to either uppercase or lowercase:

Example
<!DOCTYPE html>

<!DOCTYPE HTML>

<!doctype html>

<!Doctype Html>

Technically, you could mix uppercase and lowercase letters in HTML5. However, it's generally deemed good practice to use lowercase letters for element and attribute names. That improves both the look and the readability of your code:

Example
<section> 
  <p>I'm a paragraph that is easy to understand.</p>
</section>

Note: to prevent issues, it is recommended to always use lowercase when naming files too, as web servers are often case-sensitive.

Long Lines and Blank Spaces

All your HTML5 code should be viewable by scrolling from top to bottom. It is extremely inconvenient for a user to have to scroll sideways. It is also unprofessional regarding HTML syntax rules. Try not to make your code lines longer than 70 characters.

You shouldn't add blank lines without a good reason. You can add a blank line to separate large code blocks, but don't overuse them.

White space is tricky too. You can add two spaces for indentation:

Example
<!DOCTYPE html>
<html>
<body>

<section>

<h1>Without indentation it is hard to read</h1>

<p>This example has unnecessary blank lines and no indentation.</p>

</section>

  <section>
    <h1>Indentation is cool</h1>
    <p>This example has indentation instead of blank lines. It is easy to read and saves space.</p>
  </section>
</body>
</html>

However, avoid unnecessary spaces between symbols. E.g., while you technically can put spaces around = signs, HTML syntax recommends to avoid them for better readability:

Example
<link rel = "stylesheet" href = "styles_file.css">

<link rel="stylesheet" href="styles_file.css">

Adding HTML Comments

Code comments are written between <!-- and --> tags. They do not affect the web page on the user side, as their purpose is to only make notes for the developer:

Example
<!--
This is commented out text.
This is commented out text.
-->

<body>
  <p>This is visible <!-- this is an invisible comment --> text.</p>
</body>

HTML Syntax: Useful Tips

  • If you worry about forgetting to close your tags, start by typing both of them and then fill in the content.
  • You can also try syntax highlighting (e.g., from highlight.js if you think it will help you understand the HTML5 document structure easier.
  • If you are still unsure about your HTML syntax, try a simple syntax validator such as Freeformatter which will display all syntax errors.
Basics
Introduction
Syntax
Editors
Basic Examples
Head Section
<!DOCTYPE>
Tags and Elements
Semantic Elements
Tags Reference
Attributes
Comments
Block and Inline Elements
Forms
Form Elements
Input
Responsive Web Design
Inline Scripts
Uniform Resource Locator
Redirect
XHTML
Geolocation
Drag and Drop
Local Storage
Web Workers
Server-Sent Events
Character Encoding
Text Formatting
Quotation and Citation Elements
Headings
Paragraphs
Links
Tables
Lists
Symbols
Space
Tab
Styles
Computer Code
Layout
Classes
Colors
Images
iframes
Audio Player
Video Player
YouTube Videos
Multimedia
Canvas
SVG
<!-- -->
<a>
<abbr>
<acronym> DEPRECATED
<address>
<applet> DEPRECATED
<article>
<aside>
<audio>
<b>
<base>
<basefont> DEPRECATED
<bdi>
<bdo>
<big> DEPRECATED
<blink> DEPRECATED
<blockquote>
<body>
<br>
<button>
<canvas>
<caption>
<center> DEPRECATED
<cite>
<code>
<col>
<colgroup>
<datalist>
<dd>
<del>
<details>
<dfn>
<dialog>
<dir> DEPRECATED
<div>
<dl>
<dt>
<em>
<embed>
<fieldset>
<figcaption>
<figure>
<font> DEPRECATED
<footer>
<form>
<frame> DEPRECATED
<frameset> DEPRECATED
<h1> – <h6>
<head>
<header>
<hr>
<html>
<i>
<iframe>
<img>
<input>
<ins>
<kbd>
<keygen> DEPRECATED
<label>
<legend>
<li>
<link>
<main>
<map>
<mark>
<menu>
<menuitem> DEPRECATED
<meta>
<meter>
<nav>
<noframes> DEPRECATED
<noscript>
<object>
<ol>
<optgroup>
<option>
<output>
<p>
<param>
<pre>
<progress>
<q>
<rp>
<rt>
<ruby>
<s>
<samp>
<script>
<section>
<select>
<small>
<source>
<span>
<strike> DEPRECATED
<strong>
<style>
<sub>
<summary>
<sup>
<table>
<tbody>
<td>
<tfoot>
<th>
<thead>
<time>
<title>
<tr>
<track>
<tt> DEPRECATED
<u>
<ul>
<var>
<video>
<wbr>