HTML attribute

(Redirected from Html attribute)

HTML attributes are special words used inside the opening tag to control the element's behaviour. It is a piece of markup language used to adjust the behavior or display of an HTML element.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.

Doctype HTML is a declaration that tells the browser what version of HTML the document is written in.

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

edit

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 attribute="value">element content</element>

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:

<abbr id="anId" class="aClass" style="color:blue;" title="Hypertext Markup Language">HTML</abbr>

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).

<div style="text-align: center;">Centered text</div>

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

edit

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

  • The id attribute provides a document-wide unique identifier for an element.[7][8][9] This can be used as CSS selector to provide presentational properties, by browsers to focus attention on the specific element, or by scripts to alter the contents or presentation of an element. Appended to the URL of the page, the URL directly targets the specific element within the document, typically a sub-section of the page. For example, the ID "Attributes" in http://en.wikipedia.org/wiki/HTML#Attributes (to refer to the section "Attributes" in the "HTML" page).
  • The class attribute provides a way of classifying similar elements. Multiple class names can be added by separating them with spaces.[10][11] Semantically, for example, classes are used in microformats. Additionally authors of style sheets can construct selectors that match elements by class for styling purposes. For example, a HTML document might use the designation class="notation" to indicate that all elements with this class value are subordinate to the main text of the document. Such elements might be gathered together as footnotes on a page—instead of appearing in the place suggested by their position within the HTML source. The style sheet author might also define a rule with the .notation selector and define the property font-size: small;.
  • The style attribute provides a way of applying element-specific style rules. Multiple style declarations can be added by separating them with semicolons and an optional space, where each declaration includes a CSS property name and a value separated by a colon and an optional space (Example: style="color: red; text-align: center;").[12][13][14] The style attribute can be used on any HTML element (it will validate on any HTML element; however, it is not necessarily useful). It is considered better practice to add the style information to a style sheet, often accomplished with selectors that match the element class or ID. Sometimes, however, inline styles are favored where style sheets are considered too cumbersome for a simple and specific or ad hoc style specification.
  • The title attribute is used to attach subtextual explanation to an element. In most browsers this attribute is displayed as what is often referred to as a tooltip.

Varieties

edit

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

  • Usually the required and optional attributes modify specific HTML elements
  • While the standard attributes can be applied to most HTML elements.[15]
  • Event attributes, added in HTML version 4, allow an element to specify scripts to be run under specific circumstances.[16]

Required and optional

edit

Used by two elements

edit

Used by multiple elements

edit

Standard attributes

edit

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.[17] 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.

Element id class style title dir lang xml:lang accesskey tabindex
<param> id
<head> dir lang xml:lang
<html> dir lang xml:lang
<meta> dir lang xml:lang
<title> dir lang xml:lang
<style> title dir lang xml:lang
<applet> id class style title
<br> id class style title
<frame> id class style title
<frameset> id class style title
<iframe> id class style title
<basefont> id class style title dir lang
<center> id class style title dir lang
<dir> id class style title dir lang
<font> id class style title dir lang
<menu> id class style title dir lang
<s> id class style title dir lang
<strike> id class style title dir lang
<u> id class style title dir lang
<abbr> id class style title dir lang xml:lang
<acronym> id class style title dir lang xml:lang
<address> id class style title dir lang xml:lang
<b> id class style title dir lang xml:lang
<big> id class style title dir lang xml:lang
<blockquote> id class style title dir lang xml:lang
<body> id class style title dir lang xml:lang
<caption> id class style title dir lang xml:lang
<cite> id class style title dir lang xml:lang
<code> id class style title dir lang xml:lang
<col> id class style title dir lang xml:lang
<colgroup> id class style title dir lang xml:lang
<dd> id class style title dir lang xml:lang
<del> id class style title dir lang xml:lang
<dfn> id class style title dir lang xml:lang
<div> id class style title dir lang xml:lang
<dl> id class style title dir lang xml:lang
<dt> id class style title dir lang xml:lang
<em> id class style title dir lang xml:lang
<fieldset> id class style title dir lang xml:lang
<form> id class style title dir lang xml:lang
<hr> id class style title dir lang xml:lang
<h1>, <h2>, <h3>, <h4>, <h5>, <h6> id class style title dir lang xml:lang
<i> id class style title dir lang xml:lang
<img> id class style title dir lang xml:lang
<ins> id class style title dir lang xml:lang
<kbd> id class style title dir lang xml:lang
<li> id class style title dir lang xml:lang
<link> id class style title dir lang xml:lang
<map> id class style title dir lang xml:lang
<noframes> id class style title dir lang xml:lang
<noscript> id class style title dir lang xml:lang
<ol> id class style title dir lang xml:lang
<optgroup> id class style title dir lang xml:lang
<option> id class style title dir lang xml:lang
<p> id class style title dir lang xml:lang
<pre> id class style title dir lang xml:lang
<q> id class style title dir lang xml:lang
<samp> id class style title dir lang xml:lang
<small> id class style title dir lang xml:lang
<span> id class style title dir lang xml:lang
<strong> id class style title dir lang xml:lang
<sub> id class style title dir lang xml:lang
<sup> id class style title dir lang xml:lang
<table> id class style title dir lang xml:lang
<tbody> id class style title dir lang xml:lang
<td> id class style title dir lang xml:lang
<tfoot> id class style title dir lang xml:lang
<th> id class style title dir lang xml:lang
<thead> id class style title dir lang xml:lang
<tr> id class style title dir lang xml:lang
<tt> id class style title dir lang xml:lang
<ul> id class style title dir lang xml:lang
<var> id class style title dir lang xml:lang
<label> id class style title dir lang xml:lang accesskey
<legend> id class style title dir lang xml:lang accesskey
<object> id class style title dir lang xml:lang tabindex
<select> id class style title dir lang xml:lang tabindex
<a> id class style title dir lang xml:lang accesskey tabindex
<area> id class style title dir lang xml:lang accesskey tabindex
<button> id class style title dir lang xml:lang accesskey tabindex
<input> id class style title dir lang xml:lang accesskey tabindex
<textarea> id class style title dir lang xml:lang accesskey tabindex

Event attributes

edit

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

  • onabort
  • onautocomplete
  • onautocompleteerror
  • onblur
  • oncancel
  • oncanplay
  • oncanplaythrough
  • onchange
  • onclick
  • onclose
  • oncontextmenu
  • oncuechange
  • ondblclick
  • ondrag
  • ondragend
  • ondragenter
  • ondragexit
  • ondragleave
  • ondragover
  • ondragstart
  • ondrop
  • ondurationchange
  • onemptied
  • onended
  • onerror
  • onfocus
  • oninput
  • oninvalid
  • onkeydown
  • onkeypress
  • onkeyup
  • onload
  • onloadeddata
  • onloadedmetadata
  • onloadstart
  • onmousedown
  • onmouseenter
  • onmouseleave
  • onmousemove
  • onmouseout
  • onmouseover
  • onmouseup
  • onmousewheel
  • onpause
  • onplay
  • onplaying
  • onprogress
  • onratechange
  • onreset
  • onresize
  • onscroll
  • onseeked
  • onseeking
  • onselect
  • onshow
  • onsort
  • onstalled
  • onsubmit
  • onsuspend
  • ontimeupdate
  • ontoggle
  • onvolumechange
  • onwaiting

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.

Element At At At At At At At At At At At At At At At At At
<frameset> onload onunload
<body> onload onunload onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<abbr> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<acronym> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<address> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<b> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<big> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<blockquote> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<caption> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<center> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<cite> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<code> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<col> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<colgroup> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<dd> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<del> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<dfn> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<dir> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<div> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<dl> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<dt> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<em> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<fieldset> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<h1>, <h2>, <h3>, <h4>, <h5>, <h6> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<hr> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<i> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<ins> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<kbd> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<legend> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<li> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<link> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<map> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<menu> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<noframes> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<noscript> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<object> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<ol> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<optgroup> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<option> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<p> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<pre> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<q> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<s> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<samp> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<small> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<span> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<strike> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<strong> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<sub> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<sup> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<table> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<tbody> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<td> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<tfoot> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<th> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<thead> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<tr> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<tt> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<u> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<ul> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<var> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<img> onabort onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup
<a> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup onblur onfocus
<area> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup onblur onfocus
<button> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup onblur onfocus
<form> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup onblur onfocus
<label> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup onblur onfocus
<select> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup onblur onfocus onchange
<input> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup onblur onfocus onchange onselect
<textarea> onclick ondblclick onmousedown onmousemove onmouseout onmouseover onmouseup onkeydown onkeypress onkeyup onblur onfocus onchange onselect

See also

edit

References

edit
  1. ^ "Index of the HTML 4 Attributes". W3C. Retrieved 13 February 2015.
  2. ^ "On SGML and HTML". World Wide Web Consortium. Retrieved November 16, 2008.
  3. ^ "XHTML 1.0 – Differences with HTML 4". World Wide Web Consortium. Retrieved November 16, 2008.
  4. ^ Korpela, Jukka (July 6, 1998). "Why attribute values should always be quoted in HTML". Cs.tut.fi. Retrieved November 16, 2008.
  5. ^ "Tags used in HTML". World Wide Web Consortium. November 3, 1992. Retrieved November 16, 2008.
  6. ^ "Objects, Images, and Applets in HTML documents". World Wide Web Consortium. December 24, 1999. Retrieved 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. ^ "HTML id". W3Schools. Archived from the original on Apr 27, 2020. Retrieved 2020-04-27.
  9. ^ "HTML Global id Attribute". W3Schools. Retrieved 2020-04-27.
  10. ^ "HTML Classes". W3Schools. Retrieved 2020-04-27.
  11. ^ "HTML Global class Attribute". W3Schools. Retrieved 2020-04-27.
  12. ^ "HTML Styles". W3Schools. Retrieved 2023-05-12.
  13. ^ "HTML Global style Attribute". W3Schools. Retrieved 2023-05-12.
  14. ^ "CSS Syntax". W3Schools. Retrieved 2023-05-12.
  15. ^ "HTML Global Attributes". W3Schools.
  16. ^ "HTML Event Attributes". W3Schools.
  17. ^ a b c "Global attributes – HTML (HyperText Markup Language)". MDN Web Docs. Retrieved 2015-02-12.
  18. ^ "HTML reference – HTML (HyperText Markup Language)". MDN Web Docs. Retrieved 13 February 2015.