A consistent, clean, and tidy HTML code makes it easier for others to read and understand your code.
Here are some guidelines and tips for creating good HTML code.
Example:
HTML
<!DOCTYPE html>
Always declare the document type as the first line in your document.
Example:
HTML
<!DOCTYPE html>
HTML allows mixing uppercase and lowercase letters in element names.
However, we recommend using lowercase element names, because:
Example:
HTML
<body>
<p>This is a paragraph.</p>
</body>
Example:
HTML
<BODY>
<P>This is a paragraph.</P>
</BODY>
In HTML, you do not have to close all elements (for example the <p>
element).
However, we strongly recommend closing all HTML elements, like this:
Example:
HTML
<section>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
</section>
Example:
HTML
<section>
<p>This is a paragraph.
<p>This is a paragraph.
</section>
HTML allows mixing uppercase and lowercase letters in attribute names.
However, we recommend using lowercase attribute names, because:
<a href="https://www.w3schools.com/html/">Visit our HTML tutorial</a>
<a HREF="https://www.w3schools.com/html/">Visit our HTML tutorial</a>
HTML allows attribute values without quotes.
However, we recommend quoting attribute values, because:
<table class="striped">
<table class=striped>
<table class=table striped>
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.
<img src="html5.gif" alt="HTML5" style="width:128px;height:128px">
<img src="html5.gif">
HTML allows spaces around equal signs. But space-less is easier to read and groups entities better together.
<link rel="stylesheet" href="styles.css">
<link rel = "stylesheet" href = "styles.css">
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.
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.
<body>
<h1>Famous Cities</h1>
<h2>Tokyo</h2>
<p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area, and the most populous metropolitan area in the world.</p>
<h2>London</h2>
<p>London is the capital city of England. It is the most populous city in the United Kingdom.</p>
<h2>Paris</h2>
<p>Paris is the capital of France. The Paris area is one of the largest population centers in Europe.</p>
</body>
<body>
<h1>Famous Cities</h1>
<h2>Tokyo</h2><p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area, and the most populous metropolitan area in the world.</p>
<h2>London</h2><p>London is the capital city of England. It is the most populous city in the United Kingdom.</p>
<h2>Paris</h2><p>Paris is the capital of France. The Paris area is one of the largest population centers in Europe.</p>
</body>
<table>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<tr>
<td>A</td>
<td>Description of A</td>
</tr>
<tr>
<td>B</td>
<td>Description of B</td>
</tr>
</table>
<ul>
<li>London</li>
<li>Paris</li>
<li>Tokyo</li>
</ul>
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:
<title>HTML Style Guide and Coding Conventions</title>
An HTML page will validate without the <html>
and <body>
tags:
Example:
HTML
<!DOCTYPE html>
<head>
<title>Page Title</title>
</head>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
The HTML <head> tag can also be omitted.
Browsers will add all elements before <body>
, to a default <head>
element.
Example:
HTML
<!DOCTYPE html>
<html>
<title>Page Title</title>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
In HTML, it is optional to close empty elements.
<meta charset="utf-8">
<meta charset="utf-8" />
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.
Example:
HTML
<html lang="en-us">
Example:
HTML
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
o 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:
Example:
HTML
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Page Title</title>
</head>
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:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
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:
Tip: If you are browsing this page with a phone or a tablet, you can click on the two links below to see the difference.
Short comments should be written on one line, like this:
Example:
HTML
<!--
This is a long comment example. This is a long comment example.
This is a long comment example. This is a long comment example.
-->
Use simple syntax for linking to style sheets (the type
attribute is not necessary):
Example:
HTML
<link rel="stylesheet" href="styles.css">
Example:
CSS
p.intro {font-family:Verdana;font-size:16em;}
Example:
CSS
body {
background-color: lightgrey;
font-family: "Arial Black", Helvetica, sans-serif;
font-size: 16em;
color: black;
}
Use simple syntax for loading external scripts (the type
attribute is not necessary):
Example:
HTML
<script src="myscript.js">
Example:
HTML
<script src="https://www.w3schools.com/lib/uic.js?v=1.0.5">
Using "untidy" HTML code can result in JavaScript errors.
These two JavaScript statements will produce different results:
Example:
HTML
<script>
// Only paragraph 2 will be overwritten
document.getElementById("demo").innerHTML = "HELLO.";
</script>
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<p id="Demo">This is paragraph 1.</p>
<p id="demo">This is paragraph 2.</p>
<script>
// Only paragraph 2 will be overwritten
document.getElementById("demo").innerHTML = "HELLO.";
</script>
</body>
</html>
Some web servers (Apache, Unix) are case sensitive about file names: "london.jpg" cannot be accessed as "London.jpg".
Other web servers (Microsoft, IIS) are not case sensitive: "london.jpg" can be accessed as "London.jpg".
If you use a mix of uppercase and lowercase, you have to be aware of this.
If you move from a case-insensitive to a case-sensitive server, even small errors will break your web!
To avoid these problems, always use lowercase file names!
Example:
HTML
bangladesh-cricket-video.mp4
HTML files should have a .html extension (.htm is allowed).
CSS files should have a .css extension.
JavaScript files should have a .js extension.
Example:
HTML
.mp4 .mp3 .html .php .3gp .apk
There is no difference between the .htm and .html file extensions!
Both will be treated as HTML by any web browser and web server.
When a URL does not specify a filename at the end (like "https://horje.com/"), the server just adds a default filename, such as "index.html", "index.htm", "default.html", or "default.htm".
If your server is configured only with "index.html" as the default filename, your file must be named "index.html", and not "default.html".
However, servers can be configured with more than one default filename; usually you can set up as many default filenames as you want.