title | short-title | slug | page-type |
---|---|---|---|
HTML attribute reference |
Attributes |
Web/HTML/Attributes |
landing-page |
{{HTMLSidebar("Attributes")}}
Elements in HTML have attributes; these are additional values that configure the elements or adjust their behavior in various ways to meet the criteria the users want.
Attribute Name | Elements | Description |
---|---|---|
accept
|
{{ HTMLElement("form") }}, {{ HTMLElement("input") }} | List of types the server accepts, typically a file type. |
accept-charset
|
{{ HTMLElement("form") }} | List of supported charsets. |
accesskey
|
Global attribute | Keyboard shortcut to activate or add focus to the element. |
action
|
{{ HTMLElement("form") }} | The URI of a program that processes the information submitted via the form. |
align {{deprecated_inline}}
|
{{ HTMLElement("caption") }}, {{ HTMLElement("col") }}, {{ HTMLElement("colgroup") }}, {{ HTMLElement("hr") }}, {{ HTMLElement("iframe") }}, {{ HTMLElement("img") }}, {{ HTMLElement("table") }}, {{ HTMLElement("tbody") }}, {{ HTMLElement("td") }}, {{ HTMLElement("tfoot") }}, {{ HTMLElement("th") }}, {{ HTMLElement("thead") }}, {{ HTMLElement("tr") }} | Specifies the horizontal alignment of the element. |
allow
|
{{ HTMLElement("iframe") }} | Specifies a feature-policy for the iframe. |
alt
|
{{ HTMLElement("area") }}, {{ HTMLElement("img") }}, {{ HTMLElement("input") }} | Alternative text in case an image can't be displayed. |
as
|
{{ HTMLElement("link") }} | Specifies the type of content being loaded by the link. |
async
|
{{ HTMLElement("script") }} | Executes the script asynchronously. |
autocapitalize
|
Global attribute | Sets whether input is automatically capitalized when entered by user |
autocomplete
|
{{ HTMLElement("form") }}, {{ HTMLElement("input") }}, {{ HTMLElement("select") }}, {{ HTMLElement("textarea") }} | Indicates whether controls in this form can by default have their values automatically completed by the browser. |
autoplay
|
{{ HTMLElement("audio") }}, {{ HTMLElement("video") }} | The audio or video should play as soon as possible. |
background |
{{ HTMLElement("body") }}, {{ HTMLElement("table") }}, {{ HTMLElement("td") }}, {{ HTMLElement("th") }} |
Specifies the URL of an image file.
Note: Although browsers and email clients may still support this attribute, it is obsolete. Use CSS {{ Cssxref("background-image") }} instead. |
bgcolor |
{{ HTMLElement("body") }}, {{ HTMLElement("col") }}, {{ HTMLElement("colgroup") }}, {{ HTMLElement("marquee") }}, {{ HTMLElement("table") }}, {{ HTMLElement("tbody") }}, {{ HTMLElement("tfoot") }}, {{ HTMLElement("td") }}, {{ HTMLElement("th") }}, {{ HTMLElement("tr") }} |
Background color of the element. Note: This is a legacy attribute. Please use the CSS {{ Cssxref("background-color") }} property instead. |
border |
{{ HTMLElement("img") }}, {{ HTMLElement("object") }}, {{ HTMLElement("table") }} |
The border width. Note: This is a legacy attribute. Please use the CSS {{ Cssxref("border") }} property instead. |
capture
|
{{ HTMLElement("input") }} | From the Media Capture specification, specifies a new file can be captured. |
charset
|
{{ HTMLElement("meta") }} | Declares the character encoding of the page or script. |
checked
|
{{ HTMLElement("input") }} | Indicates whether the element should be checked on page load. |
cite
|
{{ HTMLElement("blockquote") }}, {{ HTMLElement("del") }}, {{ HTMLElement("ins") }}, {{ HTMLElement("q") }} | Contains a URI which points to the source of the quote or change. |
class
|
Global attribute | Often used with CSS to style elements with common properties. |
color |
{{ HTMLElement("font") }}, {{ HTMLElement("hr") }} |
This attribute sets the text color using either a named color or a color specified in the hexadecimal #RRGGBB format. Note: This is a legacy attribute. Please use the CSS {{ Cssxref("color") }} property instead. |
cols
|
{{ HTMLElement("textarea") }} | Defines the number of columns in a textarea. |
colspan
|
{{ HTMLElement("td") }}, {{ HTMLElement("th") }} | The colspan attribute defines the number of columns a cell should span. |
content
|
{{ HTMLElement("meta") }} |
A value associated with http-equiv or
name depending on the context.
|
contenteditable
|
Global attribute | Indicates whether the element's content is editable. |
controls
|
{{ HTMLElement("audio") }}, {{ HTMLElement("video") }} | Indicates whether the browser should show playback controls to the user. |
coords
|
{{ HTMLElement("area") }} | A set of values specifying the coordinates of the hot-spot region. |
crossorigin
|
{{ HTMLElement("audio") }}, {{ HTMLElement("img") }}, {{ HTMLElement("link") }}, {{ HTMLElement("script") }}, {{ HTMLElement("video") }} | How the element handles cross-origin requests |
csp
{{experimental_inline}}
|
{{ HTMLElement("iframe") }} | Specifies the Content Security Policy that an embedded document must agree to enforce upon itself. |
data
|
{{ HTMLElement("object") }} | Specifies the URL of the resource. |
data-*
|
Global attribute | Lets you attach custom attributes to an HTML element. |
datetime
|
{{ HTMLElement("del") }}, {{ HTMLElement("ins") }}, {{ HTMLElement("time") }} | Indicates the date and time associated with the element. |
decoding
|
{{ HTMLElement("img") }} | Indicates the preferred method to decode the image. |
default
|
{{ HTMLElement("track") }} | Indicates that the track should be enabled unless the user's preferences indicate something different. |
defer
|
{{ HTMLElement("script") }} | Indicates that the script should be executed after the page has been parsed. |
dir
|
Global attribute | Defines the text direction. Allowed values are ltr (Left-To-Right) or rtl (Right-To-Left) |
dirname
|
{{ HTMLElement("input") }}, {{ HTMLElement("textarea") }} | |
disabled
|
{{ HTMLElement("button") }}, {{ HTMLElement("fieldset") }}, {{ HTMLElement("input") }}, {{ HTMLElement("optgroup") }}, {{ HTMLElement("option") }}, {{ HTMLElement("select") }}, {{ HTMLElement("textarea") }} | Indicates whether the user can interact with the element. |
download
|
{{ HTMLElement("a") }}, {{ HTMLElement("area") }} | Indicates that the hyperlink is to be used for downloading a resource. |
draggable
|
Global attribute | Defines whether the element can be dragged. |
enctype
|
{{ HTMLElement("form") }} |
Defines the content type of the form data when the
method is POST.
|
enterkeyhint
|
{{ HTMLElement("textarea") }},
contenteditable
|
The enterkeyhint
specifies what action label (or icon) to present for the enter key on
virtual keyboards. The attribute can be used with form controls (such as
the value of textarea elements), or in elements in an
editing host (e.g., using contenteditable attribute).
|
for
|
{{ HTMLElement("label") }}, {{ HTMLElement("output") }} | Describes elements which belongs to this one. |
form
|
{{ HTMLElement("button") }}, {{ HTMLElement("fieldset") }}, {{ HTMLElement("input") }}, {{ HTMLElement("label") }}, {{ HTMLElement("meter") }}, {{ HTMLElement("object") }}, {{ HTMLElement("output") }}, {{ HTMLElement("progress") }}, {{ HTMLElement("select") }}, {{ HTMLElement("textarea") }} | Indicates the form that is the owner of the element. |
formaction
|
{{ HTMLElement("input") }}, {{ HTMLElement("button") }} | Indicates the action of the element, overriding the action defined in the {{ HTMLElement("form") }}. |
formenctype
|
{{ HTMLElement("button") }}, {{ HTMLElement("input") }} |
If the button/input is a {{Glossary("submit button")}} (e.g. type="submit" ),
this attribute sets the encoding type to use during form submission. If
this attribute is specified, it overrides the
enctype attribute of the button's
form owner.
|
formmethod
|
{{ HTMLElement("button") }}, {{ HTMLElement("input") }} |
If the button/input is a {{Glossary("submit button")}} (e.g. type="submit" ),
this attribute sets the submission method to use during form submission
(GET , POST , etc.). If this attribute is
specified, it overrides the method attribute of the
button's form owner.
|
formnovalidate
|
{{ HTMLElement("button") }}, {{ HTMLElement("input") }} |
If the button/input is a {{Glossary("submit button")}} (e.g. type="submit" ),
this boolean attribute specifies that the form is not to be validated
when it is submitted. If this attribute is specified, it overrides the
novalidate attribute of the button's
form owner.
|
formtarget
|
{{ HTMLElement("button") }}, {{ HTMLElement("input") }} |
If the button/input is a {{Glossary("submit button")}} (e.g. type="submit" ),
this attribute specifies the browsing context (for example, tab, window,
or inline frame) in which to display the response that is received after
submitting the form. If this attribute is specified, it overrides the
target attribute of the button's
form owner.
|
headers
|
{{ HTMLElement("td") }}, {{ HTMLElement("th") }} |
IDs of the <th> elements which applies to this
element.
|
height |
{{ HTMLElement("canvas") }}, {{ HTMLElement("embed") }}, {{ HTMLElement("iframe") }}, {{ HTMLElement("img") }}, {{ HTMLElement("input") }}, {{ HTMLElement("object") }}, {{ HTMLElement("video") }} |
Specifies the height of elements listed here. For all other elements, use the CSS {{cssxref("height")}} property. Note: In some instances, such as {{ HTMLElement("div") }}, this is a legacy attribute, in which case the CSS {{ Cssxref("height") }} property should be used instead. |
hidden
|
Global attribute | Prevents rendering of given element, while keeping child elements, e.g. script elements, active. |
high
|
{{ HTMLElement("meter") }} | Indicates the lower bound of the upper range. |
href
|
{{ HTMLElement("a") }}, {{ HTMLElement("area") }}, {{ HTMLElement("base") }}, {{ HTMLElement("link") }} | The URL of a linked resource. |
hreflang
|
{{ HTMLElement("a") }}, {{ HTMLElement("link") }} | Specifies the language of the linked resource. |
http-equiv
|
{{ HTMLElement("meta") }} | Defines a pragma directive. |
id
|
Global attribute | Often used with CSS to style a specific element. The value of this attribute must be unique. |
integrity
|
{{ HTMLElement("link") }}, {{ HTMLElement("script") }} |
Specifies a Subresource Integrity value that allows browsers to verify what they fetch. |
intrinsicsize
{{deprecated_inline}}
|
{{ HTMLElement("img") }} | This attribute tells the browser to ignore the actual intrinsic size of the image and pretend it's the size specified in the attribute. |
inputmode
|
{{ HTMLElement("textarea") }},
contenteditable
|
Provides a hint as to the type of data that might be entered by the user
while editing the element or its contents. The attribute can be used
with form controls (such as the value of
textarea elements), or in elements in an editing host
(e.g., using contenteditable attribute).
|
ismap
|
{{ HTMLElement("img") }} | Indicates that the image is part of a server-side image map. |
itemprop
|
Global attribute | |
kind
|
{{ HTMLElement("track") }} | Specifies the kind of text track. |
label
|
{{ HTMLElement("optgroup") }}, {{ HTMLElement("option") }}, {{ HTMLElement("track") }} | Specifies a user-readable title of the element. |
lang
|
Global attribute | Defines the language used in the element. |
language
{{deprecated_inline}}
|
{{ HTMLElement("script") }} | Defines the script language used in the element. |
loading |
{{ HTMLElement("img") }}, {{ HTMLElement("iframe") }} |
Indicates if the element should be loaded lazily
(loading="lazy" ) or loaded immediately
(loading="eager" ).
|
list
|
{{ HTMLElement("input") }} | Identifies a list of pre-defined options to suggest to the user. |
loop
|
{{ HTMLElement("audio") }}, {{ HTMLElement("marquee") }}, {{ HTMLElement("video") }} | Indicates whether the media should start playing from the start when it's finished. |
low
|
{{ HTMLElement("meter") }} | Indicates the upper bound of the lower range. |
max
|
{{ HTMLElement("input") }}, {{ HTMLElement("meter") }}, {{ HTMLElement("progress") }} | Indicates the maximum value allowed. |
maxlength
|
{{ HTMLElement("input") }}, {{ HTMLElement("textarea") }} | Defines the maximum number of characters allowed in the element. |
minlength
|
{{ HTMLElement("input") }}, {{ HTMLElement("textarea") }} | Defines the minimum number of characters allowed in the element. |
media
|
{{ HTMLElement("a") }}, {{ HTMLElement("area") }}, {{ HTMLElement("link") }}, {{ HTMLElement("source") }}, {{ HTMLElement("style") }} | Specifies a hint of the media for which the linked resource was designed. |
method
|
{{ HTMLElement("form") }} |
Defines which HTTP method to use when
submitting the form. Can be GET (default) or
POST .
|
min
|
{{ HTMLElement("input") }}, {{ HTMLElement("meter") }} | Indicates the minimum value allowed. |
multiple
|
{{ HTMLElement("input") }}, {{ HTMLElement("select") }} |
Indicates whether multiple values can be entered in an input of the type
email or file .
|
muted
|
{{ HTMLElement("audio") }}, {{ HTMLElement("video") }} | Indicates whether the audio will be initially silenced on page load. |
name
|
{{ HTMLElement("button") }}, {{ HTMLElement("form") }}, {{ HTMLElement("fieldset") }}, {{ HTMLElement("iframe") }}, {{ HTMLElement("input") }}, {{ HTMLElement("object") }}, {{ HTMLElement("output") }}, {{ HTMLElement("select") }}, {{ HTMLElement("textarea") }}, {{ HTMLElement("map") }}, {{ HTMLElement("meta") }}, {{ HTMLElement("param") }} | Name of the element. For example used by the server to identify the fields in form submits. |
novalidate
|
{{ HTMLElement("form") }} | This attribute indicates that the form shouldn't be validated when submitted. |
open
|
{{ HTMLElement("details") }}, {{ HTMLElement("dialog") }} |
Indicates whether the contents are currently visible (in the case of
a <details> element) or whether the dialog is active
and can be interacted with (in the case of a
<dialog> element).
|
optimum
|
{{ HTMLElement("meter") }} | Indicates the optimal numeric value. |
pattern
|
{{ HTMLElement("input") }} | Defines a regular expression which the element's value will be validated against. |
ping
|
{{ HTMLElement("a") }}, {{ HTMLElement("area") }} |
The ping attribute specifies a space-separated list of URLs
to be notified if a user follows the hyperlink.
|
placeholder
|
{{ HTMLElement("input") }}, {{ HTMLElement("textarea") }} | Provides a hint to the user of what can be entered in the field. |
playsinline
|
{{ HTMLElement("video") }} | A Boolean attribute indicating that the video is to be played "inline"; that is, within the element's playback area. Note that the absence of this attribute does not imply that the video will always be played in fullscreen. |
poster
|
{{ HTMLElement("video") }} | A URL indicating a poster frame to show until the user plays or seeks. |
preload
|
{{ HTMLElement("audio") }}, {{ HTMLElement("video") }} | Indicates whether the whole resource, parts of it or nothing should be preloaded. |
readonly
|
{{ HTMLElement("input") }}, {{ HTMLElement("textarea") }} | Indicates whether the element can be edited. |
referrerpolicy
|
{{ HTMLElement("a") }}, {{ HTMLElement("area") }}, {{ HTMLElement("iframe") }}, {{ HTMLElement("img") }}, {{ HTMLElement("link") }}, {{ HTMLElement("script") }} | Specifies which referrer is sent when fetching the resource. |
rel
|
{{ HTMLElement("a") }}, {{ HTMLElement("area") }}, {{ HTMLElement("link") }} | Specifies the relationship of the target object to the link object. |
required
|
{{ HTMLElement("input") }}, {{ HTMLElement("select") }}, {{ HTMLElement("textarea") }} | Indicates whether this element is required to fill out or not. |
reversed
|
{{ HTMLElement("ol") }} | Indicates whether the list should be displayed in a descending order instead of an ascending order. |
role
|
Global attribute | Defines an explicit role for an element for use by assistive technologies. |
rows
|
{{ HTMLElement("textarea") }} | Defines the number of rows in a text area. |
rowspan
|
{{ HTMLElement("td") }}, {{ HTMLElement("th") }} | Defines the number of rows a table cell should span over. |
sandbox
|
{{ HTMLElement("iframe") }} | Stops a document loaded in an iframe from using certain features (such as submitting forms or opening new windows). |
scope
|
{{ HTMLElement("th") }} |
Defines the cells that the header test (defined in the
th element) relates to.
|
scoped
{{non-standard_inline}} {{deprecated_inline}}
|
{{ HTMLElement("style") }} | |
selected
|
{{ HTMLElement("option") }} | Defines a value which will be selected on page load. |
shape
|
{{ HTMLElement("a") }}, {{ HTMLElement("area") }} | |
size
|
{{ HTMLElement("input") }}, {{ HTMLElement("select") }} |
Defines the width of the element (in pixels). If the element's
type attribute is text or
password then it's the number of characters.
|
sizes
|
{{ HTMLElement("link") }}, {{ HTMLElement("img") }}, {{ HTMLElement("source") }} | |
slot
|
Global attribute | Assigns a slot in a shadow DOM shadow tree to an element. |
span
|
{{ HTMLElement("col") }}, {{ HTMLElement("colgroup") }} | |
spellcheck
|
Global attribute | Indicates whether spell checking is allowed for the element. |
src
|
{{ HTMLElement("audio") }}, {{ HTMLElement("embed") }}, {{ HTMLElement("iframe") }}, {{ HTMLElement("img") }}, {{ HTMLElement("input") }}, {{ HTMLElement("script") }}, {{ HTMLElement("source") }}, {{ HTMLElement("track") }}, {{ HTMLElement("video") }} | The URL of the embeddable content. |
srcdoc
|
{{ HTMLElement("iframe") }} | |
srclang
|
{{ HTMLElement("track") }} | |
srcset
|
{{ HTMLElement("img") }}, {{ HTMLElement("source") }} | One or more responsive image candidates. |
start
|
{{ HTMLElement("ol") }} | Defines the first number if other than 1. |
step
|
{{ HTMLElement("input") }} | |
style
|
Global attribute | Defines CSS styles which will override styles previously set. |
summary
{{deprecated_inline}}
|
{{ HTMLElement("table") }} | |
tabindex
|
Global attribute | Overrides the browser's default tab order and follows the one specified instead. |
target
|
{{ HTMLElement("a") }}, {{ HTMLElement("area") }}, {{ HTMLElement("base") }}, {{ HTMLElement("form") }} |
Specifies where to open the linked document (in the case of an
<a> element) or where to display the response received
(in the case of a <form> element)
|
title
|
Global attribute | Text to be displayed in a tooltip when hovering over the element. |
translate
|
Global attribute |
Specify whether an element's attribute values and the values of its
Text node
children are to be translated when the page is localized, or whether to
leave them unchanged.
|
type
|
{{ HTMLElement("button") }}, {{ HTMLElement("input") }}, {{ HTMLElement("embed") }}, {{ HTMLElement("object") }}, {{ HTMLElement("ol") }}, {{ HTMLElement("script") }}, {{ HTMLElement("source") }}, {{ HTMLElement("style") }}, {{ HTMLElement("menu") }}, {{ HTMLElement("link") }} | Defines the type of the element. |
usemap
|
{{ HTMLElement("img") }}, {{ HTMLElement("input") }}, {{ HTMLElement("object") }} | |
value
|
{{ HTMLElement("button") }}, {{ HTMLElement("data") }}, {{ HTMLElement("input") }}, {{ HTMLElement("li") }}, {{ HTMLElement("meter") }}, {{ HTMLElement("option") }}, {{ HTMLElement("progress") }}, {{ HTMLElement("param") }} | Defines a default value which will be displayed in the element on page load. |
width
|
{{ HTMLElement("canvas") }}, {{ HTMLElement("embed") }}, {{ HTMLElement("iframe") }}, {{ HTMLElement("img") }}, {{ HTMLElement("input") }}, {{ HTMLElement("object") }}, {{ HTMLElement("video") }} |
For the elements listed here, this establishes the element's width. Note: For all other instances, such as {{ HTMLElement("div") }}, this is a legacy attribute, in which case the CSS {{ Cssxref("width") }} property should be used instead. |
wrap
|
{{ HTMLElement("textarea") }} | Indicates whether the text should be wrapped. |
In HTML, most attributes have two faces: the content attribute and the IDL (Interface Definition Language) attribute.
The content attribute is the attribute as you set it from the content (the HTML code) and you can set it or get it via {{domxref("element.setAttribute()")}} or {{domxref("element.getAttribute()")}}. The content attribute is always a string even when the expected value should be an integer. For example, to set an {{HTMLElement("input")}} element's maxlength
to 42 using the content attribute, you have to call setAttribute("maxlength", "42")
on that element.
The IDL attribute is also known as a JavaScript property. These are the attributes you can read or set using JavaScript properties like element.foo
. The IDL attribute is always going to use (but might transform) the underlying content attribute to return a value when you get it and is going to save something in the content attribute when you set it. In other words, the IDL attributes, in essence, reflect the content attributes.
Most of the time, IDL attributes will return their values as they are really used. For example, the default type
for {{HTMLElement("input")}} elements is "text", so if you set input.type="foobar"
, the <input>
element will be of type text (in the appearance and the behavior) but the "type" content attribute's value will be "foobar". However, the type
IDL attribute will return the string "text".
IDL attributes are not always strings; for example, input.maxlength
is a number (a signed long). When using IDL attributes, you read or set values of the desired type, so input.maxlength
is always going to return a number and when you set input.maxlength
, it wants a number. If you pass another type, it is automatically converted to a number as specified by the standard JavaScript rules for type conversion.
IDL attributes can reflect other types such as unsigned long, URLs, booleans, etc. Unfortunately, there are no clear rules and the way IDL attributes behave in conjunction with their corresponding content attributes depends on the attribute. Most of the time, it will follow the rules laid out in the specification, but sometimes it doesn't. HTML specifications try to make this as developer-friendly as possible, but for various reasons (mostly historical), some attributes behave oddly (select.size
, for example) and you should read the specifications to understand how exactly they behave.
Some content attributes (e.g. required
, readonly
, disabled
) are called boolean attributes. If a boolean attribute is present, its value is true, and if it's absent, its value is false.
HTML defines restrictions on the allowed values of boolean attributes: If the attribute is present, its value must either be the empty string (equivalently, the attribute may have an unassigned value), or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace. The following examples are valid ways to mark up a boolean attribute:
<div itemscope>This is valid HTML but invalid XML.</div>
<div itemscope=itemscope>This is also valid HTML but invalid XML.</div>
<div itemscope="">This is valid HTML and also valid XML.</div>
<div itemscope="itemscope">
This is also valid HTML and XML, but perhaps a bit verbose.
</div>
To be clear, the values "true"
and "false"
are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether. This restriction clears up some common misunderstandings: With checked="false"
for example, the element's checked
attribute would be interpreted as true because the attribute is present.
Warning
The use of event handler content attributes is discouraged. The mix of HTML and JavaScript often produces unmaintainable code, and the execution of event handler attributes may also be blocked by content security policies.
In addition to the attributes listed in the table above, global event handlers — such as onclick
— can also be specified as content attributes on all elements.
All event handler attributes accept a string. The string will be used to synthesize a JavaScript function like function name(/*args*/) {body}
, where name
is the attribute's name, and body
is the attribute's value. The handler receives the same parameters as its JavaScript event handler counterpart — most handlers receive only one event
parameter, while onerror
receives five: event
, source
, lineno
, colno
, error
. This means you can, in general, use the event
variable within the attribute.
<div onclick="console.log(event)">Click me!</div>
<!-- The synthesized handler has a name; you can reference itself -->
<div onclick="console.log(onclick)">Click me!</div>