HTML 属性参考

翻译不完整。 请帮助我们翻译这篇文章!



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

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

async <script> 表示该脚本应该异步执行。
autocapitalize Global attribute 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> Specifies the URL of an image file.

Note: Although browsers and email clients may still support this attribute, it is obsolete. Use CSS background-image instead.

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



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


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

buffered <audio>, <video> 包含已缓存媒体的时间范围。
capture <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 Global attribute 经常和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 Global attribute 指出该元素的内容是否可以被编辑。
contextmenu Global attribute Defines the ID of a <menu> element which will serve as the element's context menu.
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  <iframe> Specifies the Content Security Policy that an embedded document must agree to enforce upon itself.





Global attribute


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 Global attribute 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 Global attribute Defines whether the element can be dragged.
dropzone Global attribute 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 <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 Global attribute 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 Global attribute 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 Global attribute
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 Global attribute 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 Global attribute 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 Global attribute 定义CSS样式,这些样式会覆盖之前设置的样式。
summary <table>
tabindex Global attribute Overrides the browser's default tab order and follows the one specified instead.
target <a>, <area>, <base>, <form>
title Global attribute 当鼠标悬停在元素上面时,提示框显示的文本。
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),你应该阅读规范以理解它们实际上到底如何表现。


一些内容属性(例如 requiredreadonlydisabled)是布尔值属性。如果一个布尔值属性存在,则其值是 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,因为这个属性出现了。