eGovernment Resource Centre - Victoria, Australia

Victorian
Government
Contact Centre
1300 366 356

Main Menu

Categories, RSS and More


Main Categories


Shorten URL

Please enter the URL that you'd like to shorten or click here to shorten current:


What's this? loading...

Rate this Site

Thanks for visiting. To help us help you find what you need please take our 1 minute Visitor Survey.


Members Forum

The Forum is the place for members to post their opinions and exchange ideas.

Creating valid HTML pages

Web sites are written in specific languages, and just like human languages, they have their own grammar, vocabulary and syntax. Every web page written in these computer languages are supposed to follow these rules. However just like texts in a human language can include spelling or grammatical errors, web pages using computer languages can also include these types of mistakes. The process of verifying whether a web page actually follows the rules for the language(s) it uses is called validation, and the tool used for that is a validator. A web page that passes this process with success is called valid or that it complies with its DOCTYPE definition.

With these concepts in mind, we can define "markup validation" as the process of checking a web page against the grammar it claims to be using.

Adapted from the W3C Validation service

Relationship to checkpoints:

Checkpoint 3.2 requires that documents validate to published formal grammars. The most common DOCTYPE definitions are:

HTML 4.01 Transitional:

  • <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  • "http://www.w3.org/TR/html4/loose.dtd">

HTML 4.01 Strict

  • <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  • "http://www.w3.org/TR/html4/strict.dtd">

XHTML 1.0 Transitional

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  • "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

XHTML 1.0 Strict

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  • "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  •  
  • DOCTYPE definitions must be the first line of code in a web page.
  •  

Ensuring a page is valid

There are some simple things you can do to ensure that your documents validate.

A. DOCTYPE definition

Ensure that all pages include a DOCTYPE definition in the first line of code

Correct

  • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  • <html>
  • <head>
  • <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

Incorrect:

  • <html>
  • <head>
  • <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

B. Close relevant tags

Ensure that all relevant tags are closed properly.

Correct:

  • <p>Join the thousands of people who have immigrated to Victoria.</p>
  • <p>So why not see if you can immigrate and live in Victoria ?</p>

Incorrect:

  • <p> Join the thousands of people who have immigrated to Victoria .
  • <p> So why not see if you can immigrate and live in Victoria ?

C. Self-close tags

In XHTML, make sure you self-close tags when they don't have end tags.

Correct:

  • <img src="test.jpg" height="32" width="32" alt=" " />

Incorrect:

  • <img src="test.jpg" height="32" width="32" alt=" ">

D. Don't transpose tags

Don't transpose tags. Make sure you close tags in the order they are opened.

Correct:

  • <strong><a href="www.vic.gov.au">Victoria Online</a></strong>

Incorrect:

  • <strong><a href="www.vic.gov.au">Victoria Online</strong></a>

E. Nest block level and inline elements properly

Don't nest block level elements inside inline elements.

Correct:

  • <p><strong>Drink drivers are warned that the chances of getting caught are now higher than ever.</strong></p>

Incorrect:

  • <strong><p>Drink drivers are warned that the chances of getting caught are now higher than ever.</p></strong>

F. Don't omit tags

Don't omit compulsory tags, even if the web page displays properly without them.

Correct:

  • <table><tr><td> My eGov allows you to rate content and bookmark your favourite resources.</td></tr></table>

Incorrect:

  • <table><td> My eGov allows you to rate content and bookmark your favourite resources.</td></table>

G. Escape relevant entities

Escape relevant entities.

Correct:

  • Then we went to the Elephant &amp; Wheelbarrow

Incorrect:

  • Then we went to the Elephant & Wheelbarrow

Testing a site for HTML validity

The W3C maintains a validation service that can test a site one page at a time via URL or page upload. Alternatively, you can use the WDG HTML Validator to validate your entire site.

Common errors

There are many common errors of invalid sites. The W3C Validation service maintains a list of errors and their meanings. This document can assist in determining errors if they occur.

A. Missing DOCTYPE

No DOCTYPE found! Attempting validation with XHTML 1.0 Transitional.

The DOCTYPE Declaration was not recognized or is missing. This probably means that the Formal Public Identifier contains a spelling error, or that the Declaration is not using correct syntax. Validation has been performed using a default "fallback" Document Type Definition that closely resembles "XHTML 1.0 Transitional", but the document will not be valid until you have corrected this problem with the DOCTYPE Declaration.

Lean how to add a doctype to your document from our FAQ.

This error is caused when the page hasn't specified a DOCTYPE definition, or if this definition is in the wrong place in the document. The DOCTYPE definition always has to be the first line of code.

B. Missing attribute

Error Line 190 column 79: required attribute "alt" not specified .

…._images/home/sale/sale-static2.gif" / > </a><div style="width: 271px; height: 6

The attribute given above is required for an element that you've used, but you have omitted it. For instance, in most HTML and XHTML document types the "type" attribute is required on the "script" element and the "alt" attribute is required for the "img" element.

Typical values for type are type="text/css" for <style> and type="text/javascript" for <script>.

This error is caused by a missing ALT attribute in an IMG tag.

C. Tag is not self-closing

Error Line Line 332 column 131: end tag for "img" omitted, but OMITTAG NO was specified.

….e/products/get_into_business.gif"></a > </td></tr>

You may have neglected to close an element, or perhaps you mean to "self-close" an element, that is, ending it with "/>" instead of ".".

Although the error highlights the </a> tag, this error occurred because the web page had a DOCTYPE definition of XHTML Transitional. This DOCTYPE definition requires, in this instance that the IMG tag ends with a self-closing tag, ie: />

D. Forbidden elements are used

Error Line 719 column 118: there is no attribute "onMouseOver".

…ormation' target="_self' onMouseOver= ' self.status='Carry-on Baggage'; return

You have used the attribute named above in your document, but the document type you are using does not support that attribute for this element. This error is often caused by incorrect use of the "Strict" document type with a document that uses frames (eg., you must use the "Transitional" document type to get the "target" attribute), or by using vendor proprietary extensions such as "marginheight" (this is usually fixed by using CSS to achieve the desired effect instead).

This error may also result if the element itself is not supported in the document type you are using, as an undefined element will have no supported attributes; in this case, see the element-undefined error message for further information.

How to fix: check the spelling and case of the element and attribute, (Remember XHTML is all lower-case) and/or check that they are both allowed in the chosen document type, and/or use CSS instead of this attribute.

Some DOCTYPE definitions forbid certain elements, for example, this web page has a DOCTYPE definition of XHTML Transitional. The attribute ONMOUSEOVER is forbidden in this DOCTYPE.

E. Invalid attribute

Error Line 48 column 59: value of attribute "ID" invalid: "_" cannot start a name.

<form name="_ct10" method="post" action="default.aspx" id=" _ ct10">

It is possible that you violated the naming convention for this attribute. For example, id and name attributes must begin with a letter, not a digit.

Certain attributes must follow certain rules. In this example, the attribute ID begins with an underscore. The attribute ID must only begin with a letter.

F. Start tag missing

Error Line 103 column 381: end tag for element "TD" which is not open.

…/" target=" ">Who\ 's who</a></div></td > </tr></table></div><div class="herotime

This error occurs when a start tag is missing, or an extra end tag has been included.

G. Non-unique ID

Error Line 17 column 48: ID "LOGIN_USER" already defined.

…UT type="password" class="input" id=" login_user" style="width:80±;" name="lo

An "id" is a unique identifier. Each time this attribute is used in a document it must have a different value. If you are using this attribute as a hook for style sheets it may be more appropriate to use classes (which group elements) than id (which are used to identify exactly one element).

This error occurs when an ID has already been defined earlier in the document. This is often the case when a field ID is not unique.

Further Information

DOCTYPE definitions

Code specifications

HTML Validators

Why validation is important

Information on validation

Added: 1 July 2007 Page views: 5,413 Rating: 0 Votes: 0

Last updated: 1 July 2007


Review and Rate this Article