Learning HTML 3.2 by Examples, section 3 General remarks on the syntax of HTML:


For each element, a set of possible attributes is defined. This set can be empty or rather large, but most elements accept one or a few attributes. In almost all cases the attributes are optional.

An attribute specification consists of the following, in this order:

Attributes, if present, are written into the start tag of an element, after the element name. They must be separated from the element name and from each other by blanks or newlines. The order of attribute specifications in a tag is insignificant.

It is always safe to enclose the attribute value in quotes, using either single quotes ('80') or double quotes ("80"), using matching quotes of course. The string in quotes must not contain the quote, so if the data contains a double quote, use single quotes for quoting, and vice versa. Some browsers do not treat quotes within quotes correctly, so if you need to include a quote as data character into an attribute value, it is probably safest to present them using character escapes: double quote " as " and single quote ' as '.

In general, using double quotes is preferable, since for the human eye single quotes are sometimes difficult to distinguish from other characters like accents. Moreover, some browsers do not handle single quotes correctly.

You can also omit the quotes from an attribute value if the value consists of the following characters only (cf to the technical concept of name):

Thus, WIDTH=80 and ALIGN=CENTER are legal shorthands for WIDTH="80" and ALIGN="CENTER". A reference to a URL like HREF=foo.html is acceptable, but in general URLs must be quoted when used in attributes, e.g. HREF="http://www.hut.fi/". - Some browsers are more permissive. Some browsers may even accept elements with a starting quote but without any closing quote. Such use is very bad practise.

Often an attribute value is keyword-like: it must be one of a set of particular strings (case-sensitively and allowing quotes around). For example, if a P element contains an ALIGN attribute, its value must be one of LEFT, CENTER, RIGHT. There are also attributes for which a string in general can be used as value; technically speaking, such attributes are declared as having CDATA value in the formal SGML description (DTD) for HTML. Even within such attribute values, no HTML tags are recognized. On the other hand, escape sequences are recognized and interpreted within attribute values. (At least they should. Some browsers don't get this right.) This in turn means that if any of the characters &<> is to appear within an attribute value, it is safest to present it using the escape notation &amp; or &lt; or &gt; respectively.

There is a minimized syntax for attributes when the attribute value is the same as the attribute name. For instance, <UL COMPACT="COMPACT"> can be abbreviated as <UL COMPACT> (and it is common practise to do so). Some browsers even require minization for some attributes (COMPACT, ISMAP, CHECKED, NOWRAP, NOSHADE, NOHREF), so perhaps it is best to use the minimized syntax when applicable.

Date of last update: 2010-12-16.
This page belongs to the free information site IT and communication, section Web authoring and surfing, by Jukka "Yucca" Korpela.