HTML(5) Code Conventions
HTML Code Conventions
Many web developers know little about HTML code conventions.
From 2000 to 2010, many web developers switched from HTML to XHTML.
By using XHTML, developers gradually developed better HTML writing conventions.
For HTML5, we should form better code conventions; several suggestions are provided below.
Use the Correct Document Type
The document type declaration is located on the first line of the HTML document:
<
!DOCTYPE
htmlIf you want to use lowercase like other tags, you can use the following code:
<
!doctype
htmlHTML5 element names can use both uppercase and lowercase letters.
Lowercase letters are recommended:
- Mixing uppercase and lowercase styles is very bad.
- Developers usually use lowercase (similar to XHTML).
- The lowercase style looks cleaner.
- Lowercase letters are easier to write.
Not recommended:
<
SECTION
<
pThis is a paragraph.
<
/p
<
/SECTION
<
Section
<
pThis is a paragraph.
<
/p
<
/SECTION
<
section
<
pThis is a paragraph.
<
/p
<
/sectionIn HTML5, you do not have to close all elements (for example
elements), but we recommend adding a closing tag to every element.
Not recommended:
<
section
<
pThis is a paragraph.
<
p
This is a paragraph.
<
/section
Recommended:
<
section
<
pThis is a paragraph.
<
/p
<
pThis is a paragraph.
<
/p
<
/sectionIn HTML5, empty HTML elements do not have to be closed either:
We can write it like this:
<
meta
charset=
"utf-8"Or like this:
<
meta
charset=
"utf-8"
/The slash (/) is mandatory in XHTML and XML.
If you expect XML software to use your pages, this style is a good choice.
Use Lowercase Attribute Names
HTML5 attribute names allow both uppercase and lowercase letters.
We recommend using lowercase attribute names:
- Using both uppercase and lowercase at the same time is a very bad habit.
- Developers usually use lowercase (similar to XHTML).
- The lowercase style looks cleaner.
- Lowercase letters are easier to write.
Not recommended:
<
div
CLASS=
"menu"Recommended:
<
div
class=
"menu"HTML5 attribute values do not have to be quoted.
We recommend using quotes for attribute values:
- Quotes are required if the attribute value contains spaces.
- Mixed styles are not recommended; a consistent style is suggested.
- Using quotes for attribute values makes them easier to read.
In the following example, the attribute value contains spaces but is not quoted, so it will not work:
<
table
class=table striped>
The following uses double quotes and is correct:
<
table
class="table striped"
Image Attributes
Images usually use the alt attribute. It can replace the image when the image cannot be displayed.
<img src="html5.gif" alt="HTML5">Defining the size of an image reserves the specified space during loading and reduces flickering.
<
img
src=
"html5.gif"
alt=
"HTML5"
style=
"width:128px;height:128px
"Spaces can be used before and after the equal sign.
<
link
rel =
"stylesheet"
href =
"styles.css"
But we recommend using fewer spaces:
<
link
rel=
"stylesheet"
href=
"styles.css"When using an HTML editor, scrolling code left and right is inconvenient.
Try to keep each line of code under 80 characters.
Blank Lines and Indentation
Do not add blank lines without reason.
Add a blank line for each logical block to make it easier to read.
Use two spaces for indentation; TAB is not recommended.
Do not use unnecessary blank lines and indentation between short pieces of code.
Unnecessary blank lines and indentation:
<
body
<
h1Runoob Tutorial
<
/h1
<
h2
HTML
<
/h2
<
pRunoob Tutorial, where you learn not just technology, but also dreams.
Runoob Tutorial, where you learn not just technology, but also dreams.
Runoob Tutorial, where you learn not just technology, but also dreams,
Runoob Tutorial, where you learn not just technology, but also dreams.
<
/p
<
/body
<
body
<
h1Runoob Tutorial
<
/h1
<
h2
<
/h2
<
pRunoob Tutorial, where you learn not just technology, but also dreams.
Runoob Tutorial, where you learn not just technology, but also dreams.
Runoob Tutorial, where you learn not just technology, but also dreams.
Runoob Tutorial, where you learn not just technology, but also dreams.
<
/p
<
/body
<
table
<
tr
<
th
Name
<
/th
<
th
Description
<
/th
<
/tr
<
tr
<
td
A
<
/td
<
tdDescription of A
<
/td
<
/tr
<
tr
<
td
B
<
/td
<
tdDescription of B
<
/td
<
/tr
<
/table
<
ol
<
li
London
<
/li
<
li
Paris
<
/li
<
li
Tokyo
<
/li
<
/olIn standard HTML5, the and tags can be omitted.
The following HTML5 document is valid:
Example:
<
!DOCTYPE
html
<
head
<
titlePage title
<
/title
<
/head
<
h1This is a heading
<
/h1
<
pThis is a paragraph.
<
/p
Try it »
Omitting the and tags is not recommended.
The element is the root element of the document and is used to describe the language of the page:<
!DOCTYPE
html
<
html
lang=
"zh"Declaring the language is for the convenience of screen readers and search engines.
Omitting or will cause crashes in DOM and XML software.
Omitting will cause errors in older browsers (IE9).
Omitting ?
In standard HTML5, the tag can be omitted.
By default, browsers add the content before to a default element.
Example
<
!DOCTYPE
html
<
html
<
titlePage title
<
/title
<
body
<
h1This is a heading
<
/h1
<
pThis is a paragraph.
<
/p
<
/body
<
/htmlTry it »
![]() |
Omitting the head tag is not yet recommended. |
|---|
Metadata
In HTML5, the
<
title
Runoob Tutorial
<
/title
The title and language let search engines quickly understand your page's topic:
<
!DOCTYPE
html
<
html
lang=
"zh"
<
head
<
meta
charset=
"UTF-8"
<
titleRunoob Tutorial
<
/title
<
/headComments can be written in in:
Longer comments can be written in written on separate lines in:
Indenting the first character of a long comment by two spaces makes it easier to read.
Style Sheet
Style sheets use a concise syntax (the type attribute is optional):
<
link
rel=
"stylesheet"
href=
"styles.css"Short rules can be written on one line:
p.into
{
font-family:
Verdana;
font-size:
16em;
}
Long rules can be written on multiple lines:
body
{
background-color:
lightgrey;
font-family:
"Arial Black", Helvetica, sans-serif;
font-size:
16em;
color:
black;
}
- Put the left brace on the same line as the selector.
- Add a space between the left brace and the selector.
- Use two spaces for indentation.
- Add a space between the colon and the attribute value.
- Use a space after commas and symbols.
- End each attribute and value with a semicolon.
- Use quotes only when an attribute value contains spaces.
- Put the right brace on a new line.
- At most 80 characters per line.
![]() | Adding a space after commas and colons is a common rule. |
|---|
Loading JavaScript in HTML
Use a concise syntax to load an external script file (the type attribute is optional):
<
script
src=
"myscript.js"Poorly formed HTML may cause JavaScript execution errors.
The following two JavaScript statements produce different results:
Example
var
obj = getElementById(
"Demo"
)
var
obj = getElementById(
"demo"
)
Try it »
Use the same naming conventions for JavaScript in HTML as much as possible.
See the JavaScript Code Conventions。
Use lowercase file names
Most web servers (Apache, Unix) are case-sensitive: london.jpg cannot be accessed as London.jpg.
Other web servers (Microsoft, IIS) are case-insensitive: london.jpg can be accessed as London.jpg or london.jpg.
You must maintain a consistent style, and we recommend using lowercase file names consistently.
File extension
HTML file extensions can be .html (or .htm)。
CSS file extension is .css 。
JavaScript file extension is .js 。
The Difference Between .htm and .html
There is essentially no difference between .htm and .html extension files. Both browsers and web servers treat them as HTML files.
The difference is:
.htm was used in early DOS systems, where file extensions were limited to three characters.
In Unix systems there is no particular restriction on suffixes, so .html is generally used.
Technical Differences
If a URL does not specify a file name (such as http://www.runoob.com/css/), the server returns a default file name. Usually the default file names are index.html, index.htm, default.html, and default.htm.
If the server is configured with only "index.html" as the default file, you must name your file "index.html", not "index.htm".
However, servers can usually be configured with multiple default files, and you can set the default file name as needed.
In any case, the complete HTML extension is ".html".
