全域屬性

全域屬性(Global attributes)是所有 HTML 元素共同的屬性,可以在所有的元素中使用。但在部分的元素上,屬性可能是沒有效果的。

Global attributes may be specified on all HTML elements, even those not specified in the standard. That means that any non-standard elements must still permit these attributes, even though using those elements means that the document is no longer HTML5-compliant. For example, HTML5-compliant browsers hide content marked as <foo hidden>...<foo>, even though <foo> is not a valid HTML element.

In addition to the basic HTML global attributes, the following global attributes also exist:

  • xml:lang and xml:base — these are inherited from the XHTML specifications and deprecated, but kept for compatibility purposes.
  • The multiple aria-* attributes, used for improving accessibility.
  • The event handler attributes: 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.

說明

accesskey (en-US)

Provides a hint for generating a keyboard shortcut for the current element. This attribute consists of a space-separated list of characters. The browser should use the first one that exists on the computer keyboard layout.

class (en-US)

Is a space-separated list of the classes of the element. Classes allows CSS and JavaScript to select and access specific elements via the class selectors (en-US) or functions like the method Document.getElementsByClassName().

contenteditable (en-US)

Is an enumerated attribute indicating if the element should be editable by the user. If so, the browser modifies its widget to allow editing. The attribute must take one of the following values: true or the empty string, which indicates that the element must be editable;

  • false, which indicates that the element must not be editable.
contextmenu (en-US)

Is the id of an <menu> (en-US) to use as the contextual menu for this element.

data-*

Forms a class of attributes, called custom data attributes, that allow proprietary information to be exchanged between the HTML and its DOM representation that may be used by scripts. All such custom data are available via the HTMLElement interface of the element the attribute is set on. The HTMLElement.dataset (en-US) property gives access to them.

dir (en-US)

Is an enumerated attribute indicating the directionality of the element's text. It can have the following values: ltr, which means left to right and is to be used for languages that are written from the left to the right (like English);

  • rtl, which means right to left and is to be used for languages that are written from the right to the left (like Arabic);
  • auto, which let the user agent decides. It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then apply that directionality to the whole element.
draggable (en-US) 實驗性質

Is an enumerated attribute indicating whether the element can be dragged, using the Drag and Drop API. It can have the following values: true, which indicates that the element may be dragged

  • false, which indicates that the element may not be dragged.
dropzone 實驗性質

Is an enumerated attribute indicating what types of content can be dropped on an element, using the Drag and Drop API. It can have the following values: copy, which indicates that dropping will create a copy of the element that was dragged

  • move, which indicates that the element that was dragged will be moved to this new location.
  • link, will create a link to the dragged data.
hidden (en-US)

Is a Boolean attribute indicates that the element is not yet, or is no longer, relevant. For example, it can be used to hide elements of the page that can't be used until the login process has been completed. The browser won't render such elements. This attribute must not be used to hide content that could legitimately be shown.

id (en-US)

Defines a unique identifier (ID) which must be unique in the whole document. Its purpose is to identify the element when linking (using a fragment identifier), scripting, or styling (with CSS).

備註: The following 5 attributes are part of the WHATWG HTML Microdata feature.

itemid (en-US) 實驗性質

The unique, global identifier of an item.

itemprop (en-US) 實驗性質

Used to add properties to an item. Every HTML element may have an itemprop attribute specified, where an itemprop consists of a name and value pair.

itemref (en-US) 實驗性質

Properties that are not descendants of an element with the itemscope attribute can be associated with the item using an itemref. Itemref provides a list of element ids (not itemids) with additional properties elsewhere in the document.

itemscope 實驗性質

Itemscope (usually) works along with itemtype (en-US) to specify that the HTML contained in a block is about a particular item. itemscope creates the Item and defines the scope of the itemtype associated with it. itemtype is a valid URL of a vocabulary (such as schema.org) that describes the item and its properties context.

itemtype (en-US) 實驗性質

Specifies the URL of the vocabulary that will be used to define itemprop's (item properties) in the data structure. Itemscope (en-US) is used to set the scope of where in the data structure the vocabulary set by itemtype will be active.

lang (en-US)

Participates in defining the language of the element, the language that non-editable elements are written in or the language that editable elements should be written in. The tag contains one single entry value in the format defines in the Tags for Identifying Languages (BCP47) IETF document. xml:lang has priority over it.

slot (en-US) 實驗性質

Assigns a slot in a shadow DOM (en-US) shadow tree to an element: An element with a slot attribute is assigned to the slot created by the <slot> (en-US) element whose name (en-US) attribute's value matches that slot attribute's value.

spellcheck 實驗性質

Is an enumerated attribute defines whether the element may be checked for spelling errors. It may have the following values: true, which indicates that the element should be, if possible, checked for spelling errors;

  • false, which indicates that the element should not be checked for spelling errors.
style (en-US)

Contains CSS styling declarations to be applied to the element. Note that it is recommended for styles to be defined in a separate file or files. This attribute and the <style> (en-US) element have mainly the purpose of allowing for quick styling, for example for testing purposes.

tabindex (en-US)

Is an integer attribute indicating if the element can take input focus (is focusable), if it should participate to sequential keyboard navigation, and if so, at what position. It can takes several values: a negative value means that the element should be focusable, but should not be reachable via sequential keyboard navigation;

  • 0 means that the element should be focusable and reachable via sequential keyboard navigation, but its relative order is defined by the platform convention;
  • a positive value which means should be focusable and reachable via sequential keyboard navigation; its relative order is defined by the value of the attribute: the sequential follow the increasing number of the tabindex. If several elements share the same tabindex, their relative order follows their relative position in the document).
title (en-US)

Contains a text representing advisory information related to the element it belongs to. Such information can typically, but not necessarily, be presented to the user as a tooltip.

translate (en-US)

Is an enumerated attribute that is used to specify whether an element's attribute values and the values of its Text (en-US) node children are to be translated when the page is localized, or whether to leave them unchanged. It can have the following values: empty string and "yes", which indicates that the element will be translated.

  • "no", which indicates that the element will not be translated.

規範

Specification
HTML Standard
# the-accesskey-attribute
HTML Standard
# attr-autocapitalize
HTML Standard
# attr-fe-autocomplete
HTML Standard
# dom-fe-autofocus
HTML Standard
# global-attributes:classes-2
HTML Standard
# attr-contenteditable
HTML Standard
# attr-data-*
HTML Standard
# the-dir-attribute
HTML Standard
# the-draggable-attribute
HTML Standard
# attr-enterkeyhint
Unknown specification
# element-attrdef-html-global-exportparts
HTML Standard
# the-hidden-attribute
HTML Standard
# global-attributes:the-id-attribute-2
HTML Standard
# the-inert-attribute
HTML Standard
# attr-inputmode
HTML Standard
# attr-is
HTML Standard
# attr-itemid
HTML Standard
# names:-the-itemprop-attribute
HTML Standard
# attr-itemref
HTML Standard
# attr-itemscope
HTML Standard
# attr-itemtype
HTML Standard
# attr-lang
HTML Standard
# attr-nonce
Unknown specification
# part-attr
HTML Standard
# attr-slot
DOM Standard
# ref-for-dom-element-slot①
HTML Standard
# attr-spellcheck
HTML Standard
# the-style-attribute
HTML Standard
# attr-tabindex
HTML Standard
# the-title-attribute
HTML Standard
# attr-translate

瀏覽器相容性

BCD tables only load in the browser

參見