Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML(5) Code Conventions

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

html

If you want to use lowercase like other tags, you can use the following code:

<

!doctype

html

HTML5 element names can use both uppercase and lowercase letters.

Lowercase letters are recommended:

Not recommended:

<

SECTION

<

p

This is a paragraph.

<

/p

<

/SECTION

<

Section

<

p

This is a paragraph.

<

/p

<

/SECTION

<

section

<

p

This is a paragraph.

<

/p

<

/section

In 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

<

p

This is a paragraph.

 

<

p

This is a paragraph.

<

/section

Recommended:

<

section

<

p

This is a paragraph.

<

/p

<

p

This is a paragraph.

<

/p

<

/section

In 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:

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:

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

<

h1

Runoob Tutorial

<

/h1

<

h2

HTML

<

/h2

<

p

    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,

    Runoob Tutorial, where you learn not just technology, but also dreams.

 

<

/p

<

/body

<

body

<

h1

Runoob Tutorial

<

/h1

<

h2

<

/h2

<

p

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.

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

<

td

Description of A

<

/td

<

/tr

<

tr

<

td

B

<

/td

<

td

Description of B

<

/td

<

/tr

<

/table

<

ol

<

li

London

<

/li

<

li

Paris

<

/li

<

li

Tokyo

<

/li

<

/ol

In standard HTML5, the and tags can be omitted.

The following HTML5 document is valid:

Example:

<

!DOCTYPE

html

<

head

<

title

Page title

<

/title

<

/head

<

h1

This is a heading

<

/h1

<

p

This 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

<

title

Page title

<

/title

<

body

<

h1

This is a heading

<

/h1

<

p

This is a paragraph.

<

/p

<

/body

<

/html

Try it »

Note Omitting the head tag is not yet recommended.

Metadata

In HTML5, the The element is required; the title name describes the topic of the page:</p> <p><</p> <p>title</p> <blockquote> </blockquote> <p>Runoob Tutorial</p> <p><</p> <p>/title</p> <blockquote> </blockquote> <p>The title and language let search engines quickly understand your page's topic:</p> <pre><code>< !DOCTYPE html < html lang= "zh" < head < meta charset= "UTF-8" < title</code></pre><p>Runoob Tutorial</p> <pre><code>< /title < /head</code></pre><p>Comments can be written in <!-- and --> in:</p> <!-- This is a comment --> <p>Longer comments can be written in <!-- and --> written on separate lines in:</p> <!--   This is a longer comment. This is a longer comment. This is a longer comment.   This is a longer comment. This is a longer comment. This is a longer comment. --> <p>Indenting the first character of a long comment by two spaces makes it easier to read.</p> <hr /> <h2>Style Sheet</h2> <p>Style sheets use a concise syntax (the type attribute is optional):</p> <pre><code>< link rel= "stylesheet" href= "styles.css"</code></pre><p>Short rules can be written on one line:</p> <p>p.into</p> <p>{</p> <p>font-family:</p> <p>Verdana;</p> <p>font-size:</p> <p>16em;</p> <p>}</p> <p>Long rules can be written on multiple lines:</p> <p>body</p> <p>{</p> <p> </p> <p>background-color:</p> <p>lightgrey;</p> <p> </p> <p>font-family:</p> <p>"Arial Black", Helvetica, sans-serif;</p> <p> </p> <p>font-size:</p> <p>16em;</p> <p> </p> <p>color:</p> <p>black;</p> <p>}</p> <ul> <li>Put the left brace on the same line as the selector.</li> <li>Add a space between the left brace and the selector.</li> <li>Use two spaces for indentation.</li> <li>Add a space between the colon and the attribute value.</li> <li>Use a space after commas and symbols.</li> <li>End each attribute and value with a semicolon.</li> <li>Use quotes only when an attribute value contains spaces.</li> <li>Put the right brace on a new line.</li> <li>At most 80 characters per line.</li> </ul> <table> <thead> <tr><th><img src="https://www.runoob.com/images/lamp.jpg" alt="Note" /></th><th>Adding a space after commas and colons is a common rule.</th></tr> </thead> </table> <hr /> <h2>Loading JavaScript in HTML</h2> <p>Use a concise syntax to load an external script file (the type attribute is optional):</p> <pre><code>< script src= "myscript.js"</code></pre><p>Poorly formed HTML may cause JavaScript execution errors.</p> <p>The following two JavaScript statements produce different results:</p> <h3>Example</h3> <p>var</p> <p>obj = getElementById(</p> <p>"Demo"</p> <p>)</p> <p>var</p> <p>obj = getElementById(</p> <p>"demo"</p> <p>)</p> <p>Try it »</p>

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".