HTML element
An HTML element is a type of HTML document component, one of several types of HTML nodes. HTML document is composed of a tree of simple HTML nodes, such as text nodes, and HTML elements, which add semantics and formatting to parts of document. Each element can have HTML attributes specified. Elements can also have content, including other elements and text.
Concepts
Document vs. DOM
HTML documents are delivered as "documents". These are then parsed, which turns them into the Document Object Model internal representation, within the web browser. Presentation by the web browser is then performed on this internal DOM, not the original document.Early HTML documents were largely invalid HTML and riddled with syntax errors. The parsing process was also required to "fix" these errors as best it could. The result was often not but was at least valid according to the HTML standard. Only in the rarest cases would the parser abandon parsing altogether.
Elements vs. tags
As is generally understood, the position of an element is indicated as spanning from a start tag, possibly including some child content, and is terminated by an end tag. This is the case for many, but not all, elements within an HTML document. The distinction is explicitly emphasised in HTML 4.01 Specification:Similarly the W3C Recommendadtion HTML 5.1 2nd Edition explicitly says:
and:
As HTML is based on SGML, its parsing also depends on the Document Type Definition, specifically an HTML DTD. The DTD specifies which element types are possible and also the valid combinations in which they may appear in a document. It is part of general SGML behavior that, where only one valid structure is , its explicit statement in any given document is not generally required. As a simple example, the tag indicating the start of a paragraph element should be complemented by a tag indicating its end. But since the DTD states that paragraph elements cannot be nested, an HTML document fragment is thus inferred to be equivalent to. Because this implication is based on the combination of the DTD and the individual document, it is not usually possible to infer elements from document tags but only by using an SGML—or HTML—aware parser with knowledge of the DTD. HTML5 creates a similar result by defining what tags can be omitted.
SGML vs. XML
SGML is complex, which has limited its widespread understanding and adoption. XML was developed as a simpler alternative. Although both can use the DTD to specify the supported elements and their permitted combinations as document structure, XML parsing is simpler. The relation from tags to elements is always that of parsing the actual tags included in the document, without the implied closures that are part of SGML.HTML as used on the current web is likely to be either treated as XML, by being XHTML, or as HTML5; in either case the parsing of document tags into DOM elements is simplified compared to legacy HTML systems. Once the DOM of elements is obtained, behavior at higher levels of interface is identical or nearly so.
%block;
vs. box
Part of this CSS presentation behavior is the notion of the "box model". This is applied to those elements that CSS considers to be "block" elements, set through the CSS declaration.HTML also has a similar concept, although different, and the two are very frequently confused.
%block;
and %inline;
are groups within the HTML DTD that group elements as being either "block-level" or "inline". This is used to define their nesting behavior: block-level elements cannot be placed into an inline context. This behavior cannot be changed; it is fixed in the DTD. Block and inline elements have the appropriate and different CSS behaviors attached to them by default, including the relevance of the box model for particular element types.Note though that this CSS behavior can, and frequently is, changed from the default. Lists with are
%block;
elements and are presented as block elements by default. However, it is quite common to set these with CSS to display as an inline list.Overview
Syntax
In the HTML syntax, most elements are written with a start tag and an end tag, with the content in between. An HTML tag is composed of the name of the element, surrounded by angle brackets. An end tag also has a slash after the opening angle bracket, to distinguish it from the start tag. For example, a paragraph, which is represented by the element, would be written as:In the HTML syntax, most elements are written...
However, not all of these elements the end tag, or even the start tag, to be present. Some elements, the so-called [|void elements], do not have an end tag. A typical example is the element. A void element's behavior is predefined, and it cannot contain any content or other elements. For example, an address would be written as:
P. Sherman
42 Wallaby Way
Sydney
When using XHTML, it is required to open and close all elements, including void elements. This can be done by placing an end tag immediately after the start tag, but this is not legal in HTML 5 and will lead to two elements being created. An alternative way to specify that it is a void element, which is compatible with both XHTML and HTML 5, is to put a
/
at the of the tag.P. Sherman
42 Wallaby Way
Sydney
HTML attributes are specified inside the start tag. For example, the element, which represents an abbreviation, expects a
title
attribute within its opening tag. This would be written as:abbr.
There are multiple kinds of HTML elements: void elements, raw text elements, and normal elements.
' only have a start tag, which contains any HTML attributes. They may not contain any children, such as text or other elements. For compatibility with XHTML, the HTML specification allows an optional space and slash. The space and slash are required in XHTML and other XML applications. Two common void elements are and . Other such elements are often place-holders which reference external files, such as the image element. The attributes included in the element will then point to the external file in question. Another example of a void element is, for which the syntax is:
This element points the browser at a style sheet to use when presenting the HTML document to the user. Note that in the HTML syntax attributes don't have to be quoted if they are composed only of certain characters: letters, digits, the hyphen-minus and the period. When using the XML syntax, on the other hand, all attributes must be quoted, and a spaced trailing slash is required before the last angle bracket:
' are constructed with:
- a start tag marking the beginning of an element, which may incorporate any number of HTML attributes;
- some amount of text content, but no elements ;
- an end tag, in which the element name is prefixed with a slash:. In some versions of HTML, the end tag is optional for some elements. The end tag is required in XHTML.
usually have both a start tag and an end tag, although for some elements the end tag, or both tags, can be omitted. It is constructed in a similar way:
- a start tag marking the beginning of an element, which may incorporate any number of HTML attributes;
- some amount of content, including text and other elements;
- an end tag, in which the element name is prefixed with a slash:.
Boolean attributes, on the other hand, don't require a value to be specified. An example is the
checked
for checkboxes:In the XML syntax, though, the name should be repeated as the value:
Informally, HTML elements are sometimes referred to as "tags", though many prefer the term tag strictly in reference to the markup delimiting the start and end of an element.
Element names may be written in any combination of upper or lower case in HTML, but must be in lower case in XHTML. The canonical form was upper-case until HTML 4, and was used in HTML specifications, but in recent years, lower-case has become more common.
Element standards
HTML elements are defined in a series of freely available open standards issued since 1995, initially by the IETF and subsequently by the W3C.During the browser wars of the 1990s, developers of user agents often developed their own elements, some of which have been adopted in later standards. Other user agents may not recognize non-standard elements, and they will be ignored, possibly causing the page to be displayed improperly.
In 1998, XML introduced mechanisms to allow anyone to develop their own elements and incorporate them in XHTML documents, for use with XML-aware user agents.
Subsequently, HTML 4.01 was rewritten in an XML-compatible form, XHTML 1.0. The elements in each are identical, and in most cases valid XHTML 1.0 documents will be valid or nearly valid HTML 4.01 documents. This article mainly focuses on real HTML, unless noted otherwise; however, it remains applicable to XHTML. See HTML for a discussion of the minor differences between the two.
Element status
Since the first version of HTML, several elements have become outmoded, and are deprecated in later standards, or do not appear at all, in which case they are invalid.In HTML 4.01 / XHTML 1.0, the status of elements is complicated by the existence of three types of DTD:
- Transitional, which contain deprecated elements, but which were intended to provide a transitional period during which authors could update their practices;
- Frameset, which are versions of the Transitional DTDs which also allow authors to write frameset documents;
- Strict, which is the up-to-date form of HTML.
The first Standard contained four deprecated elements, one of which was invalid in HTML 3.2. All four are invalid in HTML 4.01 Transitional, which also deprecated a further ten elements. All of these, plus two others, are invalid in HTML 4.01 Strict. While the frame elements are still current in the sense of being present in the Transitional and Frameset DTDs, there are no plans to preserve them in future standards, as their function has been largely replaced, and they are highly problematic for user accessibility.
A common source of confusion is the loose use of deprecated to refer to both deprecated and invalid status, and to elements which are expected to be formally deprecated in future.
Content vs. presentation and behavior
Since HTML 4, HTML has increasingly focused on the separation of content from presentation. This is often referred to as a separation of concerns. HTML is used to represent the structure or content of a document, its presentation remains the sole responsibility of CSS style sheets. A default style sheet is suggested as part of the CSS standard, giving a default rendering for HTML.Behavior is also kept separate from content, and is handled by scripts. Images are contained in separate graphics files, separate from text, though they can also be considered part of the content of a page.
Separation of concerns allows the document to be presented by different user agents according to their purposes and abilities. For example, a user agent can select an appropriate style sheet to present a document by displaying on a monitor, printing on paper, or to determine speech characteristics in an audio-only user agent. The structural and semantic functions of the markup remain identical in each case.
Historically, user agents did not always support these features. In the 1990s, as a stop-gap, presentational elements were added to HTML, at the cost of creating problems for interoperability and user accessibility. This is now regarded as outmoded and has been superseded by style sheet-based design; most presentational elements are now deprecated.
External image files are incorporated with the or elements. Where an image is not purely decorative, HTML allows replacement content with similar semantic value to be provided for non-visual user agents.
An HTML document can also be extended through the use of scripts to provide additional behaviors beyond the abilities of HTML hyperlinks and forms.
The elements and, with related HTML attributes, provide style sheets and scripts.
- In the document head, and may link to shared external documents, or and may contain embedded instructions.
- or can occur at any point in the document.
- The
style
attribute is valid in most document body elements for inclusion of inline style instructions. - Event-handling attributes, which provide links to scripts, are optional in most elements.
- For user agents which do not operate scripts, the element provides embedded alternative content where appropriate; however, it can only be used in the document head and in the body as a block-level element.
Document structure elements
Document head elements
Document body elements
In visual browsers, displayable elements can be rendered as either block or inline. While all elements are part of the document sequence, block elements appear within their parent elements:- as rectangular objects which do not break across lines;
- with block margins, width and height properties which can be set independently of the surrounding elements.
Block elements
Block elements, or block-level elements, have a rectangular structure. By default, these elements will span the entire width of its parent element, and will thus not allow any other element to occupy the same horizontal space as it is placed on.The rectangular structure of a block element is often referred to as the :File:W3C and Internet Explorer box models.svg|box model, and is made up of several parts. Each element contains the following:
- The content of an element is the actual text placed between the opening and closing tags of an element.
- The padding of an element is the space around the content but which still forms part of the element. Padding should not be used to create white space between two elements. Any background style assigned to the element, such as a background image or color, will be visible within the padding. Increasing the size of an element's padding increases the amount of space this element will take up.
- The border of an element is the absolute end of an element, and spans the perimeter of that element. The thickness of a border increases the size of an element.
- The margin of an element is the white-space that surrounds an element. The content, padding and border of any other element will not be allowed to enter this area, unless forced to do so by some advanced CSS placement. Using most standard DTDs, margins on the left and right of different elements will push each other away. Margins on the top or bottom of an element, on the other hand, will not stack, or will intermingle. This means that the white-space between these elements will be as big as the larger margin between them.
Basic text
Lists
type
attribute can be used to specify the kind of marker to use in the list, but style sheets give more control. The default is Arabic numbering. In an HTML attribute: ; or in a CSS declaration: – replacing foo
with one of the following:Style | HTML value | CSS value |
A, B, C ... | A | upper-alpha |
a, b, c ... | a | lower-alpha |
I, II, III ... | I | upper-roman |
i, ii, iii ... | i | lower-roman |
1, 2, 3 ... | 1 | decimal |
CSS provides several other options not available as pure-HTML markup, including
none
, and options for CJK, Hebrew, Georgian, and Armenian script.Other block elements
Inline elements
Inline elements cannot be placed directly inside the element; they must be wholly nested within block-level elements.Anchor
Phrase elements
General
Computer phrase elements
These elements are useful primarily for documenting computer code development and user interaction through differentiation of source code, variables, user input, and terminal or other output.Presentation
As visual presentational markup only applies directly to visual browsers, its use is discouraged. Style sheets should be used instead. Several of these elements are deprecated or invalid in HTML 4 / XHTML 1.0, and the remainder are invalid in the current draft of . The current draft of , however, re-includes,, and, assigning new semantic meaning to each. In an HTML5 document, the use of these elements is no longer discouraged, provided that it is semantically correct.Span
Other inline elements
andImages and objects
Forms
These elements can be combined into a form or in some instances used separately as user-interface controls; in the document, they can be simple HTML or used in conjunction with Scripts. HTML markup specifies the elements that make up a form, and the method by which it will be submitted. However, some form of scripts must be used to process the user's input once it is submitted.Tables
The format of HTML Tables was proposed in the HTML 3.0 Drafts and the later RFC 1942 HTML Tables. They were inspired by the CALS Table Model. Some elements in these proposals were included in HTML 3.2; the present form of HTML Tables was standardized in HTML 4.Frames
Frames allow a visual HTML Browser window to be split into segments, each of which can show a different document. This can lower bandwidth use, as repeating parts of a layout can be used in one frame, while variable content is displayed in another. This may come at a certain usability cost, especially in non-visual user agents, due to separate and independent documents being displayed adjacent to each other and being allowed to interact with the same parent window. Because of this cost, frames are only allowed in HTML 4.01 Frame-set. Iframes can also hold documents on different servers. In this case the interaction between windows is blocked by the browser. Sites like Facebook and Twitter use iframes to display content on third party websites. Google AdSense uses iframes to display banners on third party websites.In HTML 4.01, a document may contain a and a a and a, but not both a and a. However, can be used in a normal document body.
longdesc
attribute
In HTML, longdesc
is an attribute used within the,, or elements. It is supposed to be a URL to a document that provides a long description for the image, frame, or iframe in question. Note that this attribute should contain a URL, – as is commonly mistaken – the text of the description itself.longdesc
was designed to be used by screen readers to display image information for computer users with accessibility issues, such as the blind or visually impaired, and is widely implemented by both web browsers and screen readers. Some developers object that it is actually seldom used for this purpose because there are relatively few authors who use the attribute and most of those authors use it incorrectly; thus, they recommend deprecating longdesc
. The publishing industry has responded, advocating the retention of longdesc
.Example
Content of
description.html
:This is an image of a two-layered birthday cake.
...
Linking to the long description in the text
Since very few graphical browsers support making the link available natively, it is useful to include a link to the description page near the element whenever possible, as this can also aid sighted users.Example
Historic elements
The following elements were part of the early HTML developed by Tim Berners-Lee from 1989–91; they are mentioned in HTML Tags, but deprecated in HTML 2.0 and were never part of HTML standards.Non-standard elements
This section lists some widely used obsolete elements, which means they are not used in valid code. They may not be supported in all user agents.Comments
A comment in HTML uses the same syntax as the SGML comment or XML comment, depending on the doctype.Unlike most HTML tags, comments do not nest.
The markup will yield the comment and the text after it, or sometimes just, depending on browser.
Comments can appear anywhere in a document, as the HTML parser is supposed to ignore them no matter where they appear so long as they are not inside other HTML tag structures.
Comments can even appear before the doctype declaration; no other tags are permitted to do this.
However, not all browsers and HTML editors are fully compliant with the HTML syntax framework and may do unpredictable things under some syntax conditions. Defective handling of comments only affects about 5% of all browsers and HTML editors in use, and even then only certain versions are affected by comment mishandling issues.
There are a few compatibility quirks involving comments:
- Placing comments – or indeed any characters except for white-space – before the
doctype
will cause Internet Explorer 6 to use quirks mode for the HTML page. None of thedoctype
information will be processed. - For compatibility with some pre-1995 browsers, the contents of and elements are still sometimes surrounded by comment delimiters, and CSS- and script-capable browsers are written to specifically ignore that comment markup as not actually a comment. This means that attempts to actually comment out CSS and script markup by change the elements inside the comment to not be recognized, e.g.
- The BlueGriffon HTML editor, in versions 1.7., makes comments that are not embedded in the syntax structure; will show up on-screen. Other HTML editors may have this same defect.
HTML standards
;HTML 3.2
;HTML 4.01
;XHTML 1.0
;XHTML 1.1
;W3C HTML 5.2
;WHATWG HTML5 Living Standard