HTML
HTML introduction
HTML basics
HTML text format 1
HTML text format 2
HTML text format 3
HTML links
HTML images
HTML image maps
HTML forms
Fieldset/legend tags
Email links
HTML labels
HTML tables
HTML frames
HTML backgrounds
HTML colors
HTML color shades
HTML color usage
HTML marquees
HTML fonts
HTML entities
HTML stylesheets
HTML layout
HTML div/span
HTML audio
HTML video
HTML objects
HTML d'load media
HTML meta tags
HTML scripts
HTML declarations
HTML head section
HTML document types
HTML tag rules
HTML things to avoid
URL formatting
Encoding and decoding
HTML use/access
HTML publish work
History of HTML
HTML summary

Programming

Programming intro
Java

Markup

First webpage guide
XHTML

Style & Layout

CSS

Browser scripting

Javascript
VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

HTML tag rules

There exists a certain set of rules when working with HTML tags that you should follow if you want your pages to validate with an HTML validator. While your pages may still display as you intend them to display even if you break one or more of these rules, the code will not be semantic. The goal is to have valid code as well as display a webpage as intended.

This lesson focuses on:

Correct tag order

Keep track of your code to make sure that there exists a proper tag order. If you are working with a large set of code, it can sometimes get confusing, so keep track of your code as you're working with it so you don't get lost in it.

Example of incorrect tag order:

<html> <head> <title>Incorrect tag order</title> </head> <body> <p> <b>Correct tag order</p> <br /> Important to keep your code valid </b> </body> </html>

The above example fixed:

<html> <head> <title>Incorrect tag order</title> </head> <body> <p> <b>Correct tag order</b> <br /> Important to keep your code valid </p> </body> </html>

Lowercase tags

ALL tags AND attributes (as well as attribute values) in HTML should be lowercase.

While having tags and/or attributes in uppercase will result in a validated page if you're using the Transitional DTD or the Frameset DTD, it will not result in a validated page if you're using the Strict DTD.

As deprecated features become less and less popular, one day all HTML documents will use the Strict DTD. It is good to get into the habit of using the Strict DTD convention now, as it will one day be the norm.

Example of using uppercase tags and attributes

<html> <head> <TITLE>Uppercase tags and attributes</TITLE> </head> <body> <P> HTML is cool </P> <br /><br /> <a HREF="http://www.yahoo.com" TARGET="_blank"> Yahoo search engine </a> </body> </html>

The above example fixed:

<html> <head> <title>Uppercase tags and attributes</title> </head> <body> <p> HTML is cool </p> <br /><br /> <a href="http://www.yahoo.com" target="_blank"> Yahoo search engine </a> </body> </html>

Closing tags

Tags should always be closed. An unclosed tag can lead to strange results that can be tedious and sometimes confusing to fix. For example, not closing the <a> can result in a much bigger chunk of text turning into a link than you were going for.

Example of not closing tags:

<html> <head> <title>Closing tags</title> </head> <body> <a href="http://www.yahoo.com" target="_blank"> Yahoo search engine <br /><br /> Here is some text </body> </html>

Output:

Notice how in the above example the text "Here is some text" becomes part of the link even though that's not what we intended. This happened because we did not close the <a> tag.

The above example fixed:

<html> <head> <title>Closing tags</title> </head> <body> <a href="http://www.yahoo.com" target="_blank"> Yahoo search engine </a> <br /><br /> Here is some text </body> </html>

Output:

Yahoo search engine Here is some text

In this example, the text "Yahoo search engine" is the link, and the text "Here is some text" remains as regular text like we intended.

Tags that have no end tags

What about tags that have no end tags?

Remember that those tags end with a / character.

Example:

<img src="apple.jpg" alt="apple" /> <hr /> <br /> <meta name="keywords" content="softcover books, hardcover books, literature" />

Quoting attributes

Attributes should always be quoted. Even though a page may appear as you intend it to even if you don't quote attributes, the page will not validate with an HTML validator. Also, if the value given to the attribute has at least one space, the page may actually not appear as you intend it to.

Example of not quoting attributes:

<html> <head> <title>Quoting attributes</title> </head> <body> <input type="button" value=I am a button /> <br /><br /> <a href=http://www.yahoo.com target="_blank"> Yahoo search engine </a> </body> </html>

Output:

In the above example, we omitted quotes from the value attribute of the <input> tag which in this case displays a button. The text that is supposed to be displayed on the button is "I am a button", but only "I" is displayed since it cuts off after the space. For the href attribute in the <a> tag, we omitted quotes as well, but it still works as intended.

The above example fixed:

<html> <head> <title>Quoting attributes</title> </head> <body> <input type="button" value="I am a button" /> <br /><br /> <a href="http://www.yahoo.com" target="_blank"> Yahoo search engine </a> </body> </html>

Output:

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments