HTML Guidelines

General Formatting 

Use a new line for every block, list, or table element, and indent every such child element. Independent of the styling of an element (as CSS allows elements to assume a different role per display property), put every block, list, or table element on a new line. Also, indent them if they are child elements of a block, list, or table element.

How to use it

Picture

Omitting <html> and <body>?

  • In the HTML5 standard, the <html> tag and the <body> tag can be omitted.

    Omitting <head>?

    In the HTML5 standard, the <head> tag can also be omitted.

    By default, browsers will add all elements before <body>, to a default <head> element. You can reduce the complexity of HTML, by omitting the <head> tag:

    Meta Data

    The <title> element is required in HTML5. Make the title as meaningful as possible:

    To ensure proper interpretation, and correct search engine indexing, both the language and the character encoding should be defined as early as possible in a document:

    Setting The Viewport

    HTML5 introduced a method to let web designers take control over the viewport, through the <meta> tag. The viewport is the user's visible area of a web page. It varies with the device, and will be smaller on a mobile phone than on a computer screen.

    You should include the following <meta> viewport element in all your web pages:

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    A <meta> viewport element 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.

    HTML Comments

    Short comments should be written on one line, like this:

    <!-- This is a comment -->

    Comments that spans more than one line, should be written like this:

    <!--

    This is a long comment example. This is a long comment example. This is a long comment example. This is a long comment example.

    -->

    Long comments are easier to observe if they are indented two spaces.

    Explain code as needed, where possible. Use comments to explain code: What does it cover, what purpose does it serve, why is respective solution used or preferred?

Use Lower Case:  Element Names and attribute names

HTML5 allows mixing uppercase and lowercase letters in both element names and attribute names.

  • We recommend using lowercase element names because:

    • • Mixing uppercase and lowercase names is bad

      • Developers normally use lowercase names (as in XHTML)

      • Lowercase look cleaner

      • Lowercase are easier to write

      <section>

          <p>This is a paragraph.</p>

      </section>

      Close All HTML Elements

      In HTML5, you don't have to close all elements (for example the <p> element). We recommend closing all HTML elements.

      Quote Attribute Values

      HTML5 allows attribute values without quotes. When quoting attributes values, use double quotation marks.

      We recommend quoting attribute values because:

      • Mixing uppercase and lowercase values is bad

      • Quoted values are easier to read

      • You MUST use quotes if the value contains spaces

      Image Attributes

      Always add the "alt" attribute to images. This attribute is important when the image for some reason cannot be displayed. Also, always define image width and height. It reduces flickering because the browser can reserve space for the image before loading.

      Spaces and Equal Signs

      HTML5 allows spaces around equal signs. But space-less is easier to read, and groups entities better together.

      Avoid Long Code Lines

      When using an HTML editor, it is inconvenient to scroll right and left to read the HTML code. Try to avoid code lines longer than 80 characters.

  • Blank Lines and Indentation

    Do not add blank lines 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.

    Do not use unnecessary blank lines and indentation. It is not necessary to indent every element. Don’t use tabs or mix tabs and spaces for indentation.

{# TODO(john.doe): revisit centering #} 

<center>Test</center>

<!-- TODO: remove optional tags -->

<ul>

    <li>Apples</li>

    <li>Oranges</li>

</ul>

Action Items

Mark todos and action items with TODO. Highlight todos by using the keyword TODO only, not other common formats like @@.

Append a contact (username or mailing list) in parentheses as with the format TODO(contact). Append action items after a colon as in TODO: action item.

Style Sheets

Use simple syntax for linking to style sheets (the type attribute is not necessary):

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

Short rules can be written compressed, on one line, like this:

p.intro {

    font-family: Verdana;

    font-size: 16em;

}

Long rules should be written over multiple lines:

body {

    background-color: lightgrey;

    font-family: "Arial Black", Helvetica, sans-  serif;

    font-size: 16em;

    color: black;

}

• Only use quotes around values if the value contains spaces

• Place the closing bracket on a new line, without leading spaces

• Avoid lines over 80 characters

Place the opening bracket on the same line as the selector

• Use one space before the opening bracket

• Use two spaces of indentation

• Use semicolon after each property-value pair, including the last

Differences Between .htm and .html

There is no difference between the .htm and .html extensions. Both will be treated as HTML by any web browser or web server.

The differences are cultural:

.htm "smells" of early DOS systems where the system limited the extensions to 3 characters.

.html "smells" of Unix operating systems that did not have this limitation. 

Technical Differences

When a URL does not specify a filename (like http://www.w3schools.com/css/), the server returns a default filename.

Common default filenames are index.html, index.htm, default.html, and default.htm. If your server is configured only with "index.html" as default filename, your file must be named "index.html", not "index.htm."

However, servers can be configured with more than one default filename, and normally you can set up as many default filenames as needed.

Anyway, the full extension for HTML files is .html, and there's no reason it should not be used.

About Files 

Loading JavaScript in HTML

Use simple syntax for loading external scripts (the type attribute is not necessary):

<script src="myscript.js">

File Extensions

  • HTML files should have a .html extension (not .htm).

  • CSS files should have a .css extension.

  • JavaScript files should have a .js extension.

Use Lower Case File Names

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" or "london.jpg".

If you use a mix of upper and lower case, you have to be extremely consistent.

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 lower case file names.

File Extensions

  • HTML files should have a .html or .htm extension.

  • CSS files should have a .css extension.

  • JavaScript files should have a .js extension.

Protocol

Omit the protocol from embedded resources.

Omit the protocol portion (http:, https:) from URLs pointing to images and other media files, style sheets, and scripts unless the respective files are not available over both protocols.

Omitting the protocol—which makes the URL relative—prevents mixed content issues and results in minor file size savings.

Any Questions?


For more information or comments please write to us at cemexgo.developerscenter@cemex.com and we will gladly assist you as soon as possible.

Support Links

For more reference please visit the following links:

http://getbem.com/

Battling BEM – 5 common problems and how to avoid them