HTML 属性参考

HTML 中的元素拥有属性(attribute);这些额外的属性值可以配置元素或者以各种方式来调整元素的行为,进而满足用户所需的标准。


属性名 元素 描述
accept (en-US) <form>, <input> 服务器接受内容或文件类型的列表。
accept-charset <form> 支持的字符集列表。
accesskey 全局属性 定义键盘快捷键来激活或者聚焦元素
action <form> 表单信息提交的 url 地址,指向进行处理的程序。
align <applet>, <caption>, <col>, <colgroup>, <hr>, <iframe>, <img>, <table>, <tbody>, <td>, <tfoot> , <th>, <thead>, <tr> 设置元素的水平对齐。
allow <iframe> Specifies a Permissions-Policy for the iframe.

<applet>, <area>, <img>, <input>

async <script> 表示该脚本应该异步执行。
autocapitalize 全局属性 Sets whether input is automatically capitalized when entered by user
autocomplete <form>, <input>, <select>, <textarea> 表示该表单中是否可以由浏览器自动完成填值。
autofocus <button>, <input>, <keygen>, <select>, <textarea> 在网页加载后该元素应该自动聚焦。
autoplay <audio>, <video> audio 或 video 应该自动播放
background <body>, <table>, <td>, <th> 指定图像文件的 URL。

注意:虽然浏览器和电子邮件客户端可能仍然支持此属性,但它已过时。改用 cssback-image

bgcolor <body>, <col>, <colgroup>, <marquee>, <table>, <tbody>, <tfoot>, <td>, <th>, <tr>


NOTE:这是一个遗留属性,请使用 css{“background-color":}代替。

border <img>, <object>, <table>


Note:这是遗留属性,请使用 CSS border 属性代替。

buffered <audio>, <video> 包含已缓存媒体的时间范围。
capture (en-US) <input> From the HTML Media Capture
The definition of 'media capture' in that specification.
spec, specifies a new file can be captured.
challenge <keygen> 与公钥一起提交的挑战字符。
charset <meta>, <script> 申明该页面或脚本的字符编码。
checked <command>, <input> 指出该元素在页面加载后是否处于选中状态。
cite <blockquote>, <del>, <ins>, <q> 包含一个 URI,用来指明引用或修改的源地址。
class 全局属性 经常和 CSS 一起配合使用来修饰元素。
code <applet> 标明被加载和执行的 applet 类文件的 URL。
codebase <applet> 指出存放 applet 类文件基本 URL 地址 (绝对或相对),这些文件由 code 属性标明。
color <basefont>, <font>, <hr>


1. 以`#`号开头的 RGB 十六进制数,如#112233,其中 11 表示的是 R(红),22 表示的是 G 绿 (),33 表示的是 B(蓝)

2. 颜色的英文单词名,如`red`表示红色

cols <textarea> 定义一个 textarea 中包含多少列。
colspan <td>, <th> 该属性一般用在 td 标签中,用于指定一个单元格占用多少列,例如:<td colspan='2'>表示占用一个单元格占 2 列。
content <meta> A value associated with http-equiv or name depending on the context.
contenteditable 全局属性 指出该元素的内容是否可以被编辑。
contextmenu 全局属性 定义将用作元素上下文菜单的<menu>元素的 ID。
controls <audio>, <video> 指出浏览器是否对用户显示播放控制台 (前进或回退)
coords <area> A set of values specifying the coordinates of the hot-spot region.
crossorigin <audio>, <img>, <link>, <script>, <video> How the element handles cross-origin requests
csp (en-US) 实验性 <iframe> Specifies the Content Security Policy that an embedded document must agree to enforce upon itself.



资源 URL 地址



允许你对于一个 HTML 元素绑定自定义的属性。

datetime <del>, <ins>, <time> Indicates the date and time associated with the element.
decoding <img> Indicates the preferred method to decode the image.
default <track> Indicates that the track should be enabled unless the user's preferences indicate something different.
defer <script> Indicates that the script should be executed after the page has been parsed.
dir 全局属性 Defines the text direction. Allowed values are ltr (Left-To-Right) or rtl (Right-To-Left)
dirname <input>, <textarea>
disabled <button>, <command>, <fieldset>, <input>, <keygen>, <optgroup>, <option>, <select>, <textarea> Indicates whether the user can interact with the element.
download <a>, <area> Indicates that the hyperlink is to be used for downloading a resource.
draggable 全局属性 Defines whether the element can be dragged.
dropzone 全局属性 Indicates that the element accept the dropping of content on it.
enctype <form> Defines the content type of the form date when the method is POST.
enterkeyhint 实验性 <input>, <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 (en-US) <label>, <output> 描述与当前元素绑定的元素。
form <button>, <fieldset>, <input>, <keygen>, <label>, <meter>, <object>, <output>, <progress>, <select>, <textarea> Indicates the form that is the owner of the element.
formaction <input>, <button> Indicates the action of the element, overriding the action defined in the <form>.
headers <td>, <th> IDs of the <th> elements which applies to this element.
height <canvas>, <embed>, <iframe>, <img>, <input>, <object>, <video> 注意:在某些情况下,例如<div>,这是一个遗留属性,在这种情况下应该使用 CSS height 属性。 在其他情况下,例如<canvas>,必须使用此属性指定高度。
hidden 全局属性 Indicates the relevance of an element.
high <meter> Indicates the lower bound of the upper range.
href <a>, <area>, <base>, <link> 关联资源的 URL。
hreflang <a>, <area>, <link> Specifies the language of the linked resource.
http-equiv <meta>
icon <command> Specifies a picture which represents the command.
id 全局属性 Often used with CSS to style a specific element. The value of this attribute must be unique.
ismap <img> Indicatesthat the image is part of a server-side image map.
itemprop 全局属性
keytype <keygen> Specifies the type of key generated.
kind <track> Specifies the kind of text track.
label <track> Specifies a user-readable title of the text track.
lang 全局属性 Defines the language used in the element.
language <script> Defines the script language used in the element.
list <input> Identifies a list of pre-defined options to suggest to the user.
loop <audio>, <bgsound>, <marquee>, <video> Indicates whether the media should start playing from the start when it's finished.
low <meter> Indicates the upper bound of the lower range.
manifest <html> Specifies the URL of the document's cache manifest.
max <input>, <meter>, <progress> Indicates the maximum value allowed.
maxlength <input>, <textarea> Defines the maximum number of characters allowed in the element.
media <a>, <area>, <link>, <source>, <style> Specifies a hint of the media for which the linked resource was designed.
method <form> Defines which HTTP method to use when submitting the form. Can be GET (default) or POST.
min <input>, <meter> Indicates the minimum value allowed.
multiple <input>, <select> Indicates whether multiple values can be entered in an input of the type email or file.
name <button>, <form>, <fieldset>, <iframe>, <input>, <keygen>, <object>, <output>, <select>, <textarea>, <map>, <meta>, <param> Name of the element. For example used by the server to identify the fields in form submits.
novalidate <form> This attribute indicates that the form shouldn't be validated when submitted.
open <details> Indicates whether the details will be shown on page load.
optimum <meter> Indicates the optimal numeric value.
pattern <input> Defines a regular expression which the element's value will be validated against.
ping <a>, <area>
placeholder <input>, <textarea> Provides a hint to the user of what can be entered in the field.
poster <video> A URL indicating a poster frame to show until the user plays or seeks.
preload <audio>, <video> Indicates whether the whole resource, parts of it or nothing should be preloaded.
pubdate <time> Indicates whether this date and time is the date of the nearest <article> ancestor element.
radiogroup <command>
readonly <input>, <textarea> Indicates whether the element can be edited.
rel <a>, <area>, <link> Specifies the relationship of the target object to the link object.
required <input>, <select>, <textarea> Indicates whether this element is required to fill out or not.
reversed <ol> Indicates whether the list should be displayed in a descending order instead of a ascending.
rows <textarea> Defines the number of rows in a textarea.
rowspan <td>, <th> Defines the number of rows a table cell should span over.
sandbox <iframe>
spellcheck 全局属性 Indicates whether spell checking is allowed for the element.
scope <th>
scoped <style>
seamless <iframe>
selected <option> Defines a value which will be selected on page load.
shape <a>, <area>
size <input>, <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 <link>
span <col>, <colgroup>
src <audio>, <embed>, <iframe>, <img>, <input>, <script>, <source>, <track>, <video> The URL of the embeddable content.
srcdoc <iframe>
srclang <track>
start <ol> Defines the first number if other than 1.
step <input>
style 全局属性 定义 CSS 样式,这些样式会覆盖之前设置的样式。
summary <table>
tabindex 全局属性 Overrides the browser's default tab order and follows the one specified instead.
target <a>, <area>, <base>, <form>
title 全局属性 当鼠标悬停在元素上面时,提示框显示的文本。
type <button>, <input>, <command>, <embed>, <object>, <script>, <source>, <style>, <menu> 定义元素的类型。
usemap <img>, <input>, <object>
value <button>, <option>, <input>, <li>, <meter>, <progress>, <param> 定义页面加载时,在元素内显示的默认值。
width <canvas>, <embed>, <iframe>, <img>, <input>, <object>, <video> Note: In some instances, such as <div>, this is a legacy attribute, in which case the CSS width property should be used instead. In other cases, such as <canvas>, the width must be specified with this attribute.
wrap <textarea> Indicates whether the text should be wrapped.

内容属性和 IDL 属性

在 HTML 中,大多数属性都有两副面孔:内容属性IDL(接口描述语言)属性

内容属性需要你在内容(HTML 代码)中设置,而且可以通过 element.setAttribute()element.getAttribute()来设置。内容属性常常是一个字符串,即使里面的值是一个整数。例如,要将 <input> 元素的 maxlength 设置为 42,你需要在元素中调用 setAttribute("maxlength", "42")

IDL 属性(attribute)也就是一个 JavaScript 属性(property)。你可以使用 JavaScript 属性如 来设置这些属性。当你需要获取 IDL 属性的值时,IDL 属性总会使用隐含的内容属性的值(可能先经过转换)来返回一个值。同样地,当你设置这个值时,这个值会保存在内容属性中。换句话说,IDL 属性本质上反映了内容属性。

大多数时候,IDL 属性会返回元素确实在用的值。例如,<input> 的默认 type 是 "text",所以如果你设置 input.type="foobar"<input> 元素仍然会是文本类型(在外观上和表现上),但 "type" 内容属性的值是 "foobar"。然而,type IDL 属性依旧会返回字符串 "text"。

IDL attributes 并不总是字符串;例如 input.maxlength 是一个数字(一个有符号的 long 类型)。使用 IDL 属性时,你读取或设置值的类型都是要求的类型。所以 input.maxlength 总会返回一个数字,而如果你要设置 input.maxlength,也需要使用数字。如果你传入了别的类型,则会根据标准 JavaScript 的类型转换规则被转换为数字。

IDL 属性可以 反映其他类型,如 unsigned long, URLs, booleans 等。不幸的是,并没有清晰的规则规定一个属性的 IDL 属性与相关的内容属性如何关联。大多数时候,这会遵守 规范中列出的规则,但有时并不遵守。HTML 规范尝试将这件事尽可能变得对开发者友好,但由于很多原因(大多是历史原因),一些属性表现得很奇怪(例如 select.size),你就需要阅读规范以理解它们实际上到底如何表现。


一些内容属性(例如 required, readonly, disabled)是布尔值属性。如果一个布尔值属性存在,则其值是 true,如果不存在,其值是 false

HTML5 定义了布尔值属性允许的取值:如果属性存在,其值必须是一个空字符串(即该属性的值未分配),或者是一个大小写无关的 ASCII 字符串,该字符串与属性名严格相同,前后都没有空格。下述例子是为一个布尔值属性取值的几个有效方式。

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

再明确一点,布尔值属性不能取值为 "true" 和 "false"。如果需要表示 false 值,布尔值属性需要整个忽略不写。这个限制澄清了一些常见误会:比如在元素中设置 checked="false" ,元素的 checked 属性会被解读为 true,因为这个属性出现了。