HTML attribute explained

HTML attributes are special words used inside the opening tag to control the element's behaviour. HTML attributes are a modifier of a HTML element type. An attribute either modifies the default functionality of an element type or provides functionality to certain element types unable to function correctly without them. In HTML syntax, an attribute is added to a HTML start tag.

Several basic attributes types have been recognized, including: (1) required attributes, needed by a particular element type for that element type to function correctly; (2) optional attributes, used to modify the default functionality of an element type; (3) standard attributes, supported by many element types; and (4) event attributes, used to cause element types to specify scripts to be run under specific circumstances.

Some attribute types function differently when used to modify different element types. For example, the attribute name is used by several element types, but has slightly different functions in each.[1]

Description

HTML attributes generally appear as name–value pairs, separated by =, and are written within the start tag of an element, after the element's name:

element content

Where element names the HTML element type, and attribute is the name of the attribute, set to the provided value. The value may be enclosed in single or double quotes, although values consisting of certain characters can be left unquoted in HTML (but not XHTML).[2] [3] Leaving attribute values unquoted is considered unsafe.[4]

Although most attributes are provided as paired names and values, some affect the element simply by their presence in the start tag of the element[5] (like the ismap attribute for the img element[6]).

The abbreviation element, abbr, can be used to demonstrate these various attributes:

HTML

This example displays as

HTML in blue without being a link, and in most browsers, pointing the cursor at the abbreviation should display the title text "Hypertext Markup Language" within a floating yellow background (tooltip).

Centered text

In this other example, your text will look like this:

Centered text

Most elements also take the language-related attributes lang and dir.

Common attributes

Usually, HTML elements can take any of several most common standard attributes (See the complete list):

Varieties

HTML attributes are generally classified as required attributes, optional attributes, standard attributes, and event attributes:

Required and optional

Used by two elements

Used by multiple elements

Standard attributes

Standard attributes are also known as global attributes, and function with a large number of elements.[17] They include the basic standard attributes: these include accesskey, class, contenteditable, contextmenu, data, dir, hidden, id, lang, style, tabindex, title. There are also some experimental ones. Both xml:lang and xml:base have been deprecated. The multiple aria-* attributes improve accessibility. The event handler attributes are listed later on.

Technically all standard attributes must be accepted by all elements, though they will not function with some elements.[18] The table below lists some common standard attributes, and some elements they can function with.

Elementidclassstyletitledirlangxml:langaccesskeytabindex
id
dirlangxml:lang
dirlangxml:lang
dirlangxml:lang
dirlangxml:lang
titledirlangxml:lang
idclassstyletitle
idclassstyletitle
idclassstyletitle
idclassstyletitle
idclassstyletitle
idclassstyletitledirlang
idclassstyletitledirlang
idclassstyletitledirlang
idclassstyletitledirlang
idclassstyletitledirlang
idclassstyletitledirlang
idclassstyletitledirlang
idclassstyletitledirlang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
,,,,, idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:lang
idclassstyletitledirlangxml:langaccesskey
idclassstyletitledirlangxml:langaccesskey
idclassstyletitledirlangxml:langtabindex
idclassstyletitledirlangxml:langtabindex
idclassstyletitledirlangxml:langaccesskeytabindex
idclassstyletitledirlangxml:langaccesskeytabindex
idclassstyletitledirlangxml:langaccesskeytabindex
idclassstyletitledirlangxml:langaccesskeytabindex
idclassstyletitledirlangxml:langaccesskeytabindex

Event attributes

The standard attributes include the event handler attributes. They are all prefixed on-:

Event attributes, added in HTML version 4, allow an element to specify scripts to be run under specific circumstances. The table below lists some common event handler attributes, and some elements they can function with.

ElementAtAtAtAtAtAtAtAtAtAtAtAtAtAtAtAtAt
onloadonunload
onloadonunloadonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
,,,,, onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onabortonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyup
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyuponbluronfocus
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyuponbluronfocus
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyuponbluronfocus
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyuponbluronfocus
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyuponbluronfocus
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyuponbluronfocusonchange
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyuponbluronfocusonchangeonselect
onclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonkeyuponbluronfocusonchangeonselect

See also

References

  1. Web site: Index of the HTML 4 Attributes . W3C . 13 February 2015.
  2. Web site: On SGML and HTML . World Wide Web Consortium . November 16, 2008.
  3. Web site: XHTML 1.0 – Differences with HTML 4. World Wide Web Consortium. November 16, 2008.
  4. Web site: Jukka. Korpela. Why attribute values should always be quoted in HTML. Cs.tut.fi . July 6, 1998. November 16, 2008.
  5. Web site: Tags used in HTML . World Wide Web Consortium . November 3, 1992 . November 16, 2008.
  6. Web site: Objects, Images, and Applets in HTML documents . World Wide Web Consortium . December 24, 1999. November 16, 2008.
  7. However, multiple identifiers may apply to the same element; in particular an element may be inside another element, each having an identifier.
  8. Web site: HTML id. W3Schools. 2020-04-27 . live . https://web.archive.org/web/20200427024838/https://www.w3schools.com/html/html_id.asp . Apr 27, 2020 .
  9. Web site: HTML Global id Attribute. W3Schools. 2020-04-27.
  10. Web site: HTML Classes. W3Schools. 2020-04-27.
  11. Web site: HTML Global class Attribute. W3Schools. 2020-04-27.
  12. Web site: HTML Styles. W3Schools. 2023-05-12.
  13. Web site: HTML Global style Attribute. W3Schools. 2023-05-12.
  14. Web site: CSS Syntax. W3Schools. 2023-05-12.
  15. Web site: HTML Global Attributes . W3Schools.
  16. Web site: HTML Event Attributes . W3Schools.
  17. Web site: Global attributes – HTML (HyperText Markup Language) . MDN Web Docs . 2015-02-12.
  18. Web site: HTML reference – HTML (HyperText Markup Language) . MDN Web Docs . 13 February 2015.