HTML 属性参考

HTML 中的元素具有属性;这些是附加值,用于配置元素或以各种方式调整其行为以满足用户想要的标准。

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

属性名称 元素 描述
accept <form>, <input> 服务器接受的类型列表,通常是文件类型。
accept-charset <form> 支持的字符集列表。
accesskey 全局属性 用于激活元素或将焦点添加到元素的键盘快捷键。
action <form> 处理通过表单提交的信息的程序的 URI。
align Deprecated <caption>, <col>, <colgroup>, <hr>, <iframe>, <img>, <table>, <tbody>, <td>, <tfoot>, <th>, <thead>, <tr> 指定元素的水平对齐方式。
allow <iframe> 指定 iframe 的功能策略。
alt <area>, <img>, <input> 无法显示图片时的替代文本。
as <link> 指定链接正在加载的内容的类型。
async <script> 异步执行脚本。
autocapitalize 全局属性 设置用户输入时是否自动大写
autocomplete <form>, <input>, <select>, <textarea> 指示此表单中的控件默认情况下是否可以由浏览器自动补齐其值。
autoplay <audio><video> 音频或视频应尽快播放。
background <body>, <table>, <td>, <th> 指定图片文件的 URL。

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

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

元素的背景颜色。

注意:这是一个旧属性。请改用 CSS background-color 属性。

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

边框宽度。

注意:这是一个旧属性。请改用 CSS border 属性。

capture <input> 媒体捕捉规范,指定可以捕获的新文件。
charset <meta> 声明页面或脚本的字符编码。
checked <input> 指示是否应在页面加载时检查元素。
cite <blockquote>, <del>, <ins>, <q> 包含指向引用或更改来源的 URI。
class 全局属性 通常与 CSS 一起使用来设置具有公共属性的元素的样式。
color <font>, <hr>

此属性使用命名颜色或以十六进制 #RRGGBB 格式指定的颜色来设置文本颜色。

注意:这是一个旧属性。请改用 CSS color 属性。

cols <textarea> 定义文本区域中的列数。
colspan <td>, <th> colspan 属性定义单元格应跨越的列数。
content <meta> http-equivname 关联的值,具体取决于上下文。
contenteditable 全局属性 指示元素的内容是否可编辑。
controls <audio><video> 指示浏览器是否应向用户显示播放控件。
coords <area> 指定热点区域坐标的一组值。
crossorigin <audio><img><link><script><video> 元素如何处理跨源请求
csp Experimental <iframe> 指定嵌入文档必须同意对其自身执行的内容安全策略。
data <object> 指定资源的 URL。
data-* 全局属性 允许你将自定义属性附加到 HTML 元素。
datetime <del>, <ins>, <time> 指示与元素关联的日期和时间。
decoding <img> 指示解码图片的首选方法。
default <track> 指示应启用该轨道,除非用户的首选项表明有不同的情况。
defer <script> 指示应在解析页面后执行脚本。
dir 全局属性 定义文本方向。允许的值为 ltr(从左到右)或 rtl(从右到左)
dirname <input><textarea>
disabled <button>, <fieldset>, <input>, <optgroup>, <option>, <select>, <textarea> 指示用户是否可以与元素交互。
download <a>, <area> 指示超链接将用于下载资源。
draggable 全局属性 定义元素是否可以拖动。
enctype <form> 定义 method 为 POST 时表单数据的内容类型。
enterkeyhint Experimental <textarea>, contenteditable enterkeyhint 指定为虚拟键盘上的 Enter 键呈现什么操作标签(或图标)。该属性可以与表单控件(例如 textarea 元素的值)一起使用,也可以在编辑主机中的元素中使用(例如,使用 contenteditable 属性)。
for <label><output> 描述属于该元素的元素。
form <button>, <fieldset>, <input>, <label>, <meter>, <object>, <output>, <progress>, <select>, <textarea> 指示作为元素所有者的表单。
formaction <input><button> 指示元素的操作,覆盖 <form> 中定义的操作。
formenctype <button><input> 如果按钮/输入是 submit button(例如 type="submit"),则此属性设置表单提交期间要使用的编码类型。如果指定此属性,它将覆盖按钮的 form 所有者的 enctype 属性。
formmethod <button><input> 如果按钮/输入是 submit button(例如 type="submit"),则此属性设置表单提交期间要使用的提交方法(GETPOST 等)。如果指定此属性,它将覆盖按钮的 form 所有者的 method 属性。
formnovalidate <button><input> 如果按钮/输入是 submit button(例如 type="submit"),则此布尔属性指定表单在提交时不进行验证。如果指定此属性,它将覆盖按钮的 form 所有者的 novalidate 属性。
formtarget <button><input> 如果按钮/输入是 submit button(例如 type="submit"),则此属性指定浏览上下文(例如选项卡、窗口或内联框架),在其中显示提交表单后收到的响应。如果指定此属性,它将覆盖按钮的 form 所有者的 target 属性。
headers <td>, <th> 适用于该元素的 <th> 元素的 ID。
height <canvas>, <embed>, <iframe>, <img>, <input>, <object>, <video>

指定此处列出的元素的高度。对于所有其他元素,请使用 CSS height 属性。

注意:在某些情况下,例如 <div>,这是一个旧属性,在这种情况下,应使用 CSS height 属性。

hidden 全局属性 防止渲染给定元素,同时保留子元素,例如 脚本元素,活动。
high <meter> 表示上限范围的下限。
href <a>, <area>, <base>, <link> 链接资源的 URL。
hreflang <a>, <link> 指定链接资源的语言。
http-equiv <meta> 定义一个杂注指令。
id 全局属性 通常与 CSS 一起使用来设置特定元素的样式。该属性的值必须是唯一的。
integrity <link>, <script>

指定一个 子资源完整性 值,允许浏览器验证它们获取的内容。

intrinsicsize Deprecated <img> 此属性告诉浏览器忽略图片的实际固有大小并假装它是属性中指定的大小。
inputmode <textarea>, contenteditable 提供有关用户在编辑元素或其内容时可能输入的数据类型的提示。该属性可以与表单控件(例如 textarea 元素的值)一起使用,也可以在编辑主机中的元素中使用(例如,使用 contenteditable 属性)。
ismap <img> 指示图片是服务器端图片映射的一部分。
itemprop 全局属性
kind <track> 指定文本轨道的类型。
label <optgroup>, <option>, <track> 指定元素的用户可读标题。
lang 全局属性 定义元素中使用的语言。
language Deprecated <script> 定义元素中使用的脚本语言。
loading Experimental <img>, <iframe> 指示元素是应延迟加载 (loading="lazy") 还是立即加载 (loading="eager")。
list <input> 标识要向用户建议的预定义选项列表。
loop <audio>, <marquee>, <video> 指示媒体结束后是否应从头开始播放。
low <meter> 表示下限范围的上限。
manifest Deprecated <html> 指定文档的缓存清单的 URL。

注意:此属性已过时,请使用 <link rel="manifest"> 代替。

max <input>, <meter>, <progress> 表示允许的最大值。
maxlength <input><textarea> 定义元素中允许的最大字符数。
minlength <input><textarea> 定义元素中允许的最小字符数。
media <a><area><link><source><style> 指定为其设计链接资源的媒体的提示。
method <form> 定义提交表单时使用哪种 HTTP 方法。可以是 GET(默认)或 POST
min <input><meter> 表示允许的最小值。
multiple <input><select> 指示是否可以在 emailfile 类型的输入中输入多个值。
muted <audio><video> 指示页面加载时音频是否最初静音。
name <button>, <form>, <fieldset>, <iframe>, <input>, <object>, <output>, <select>, <textarea>, <map>, <meta>, <param> 元素的名称。例如,服务器使用它来识别表单提交中的字段。
novalidate <form> 此属性指示表单在提交时不应进行验证。
open <details><dialog> 指示内容当前是否可见(对于 <details> 元素)或者对话框是否处于活动状态并且可以与之交互(对于 <dialog> 元素)。
optimum <meter> 表示最佳数值。
pattern <input> 定义一个正则表达式,将根据该正则表达式验证元素的值。
ping <a>, <area> ping 属性指定一个以空格分隔的 URL 列表,当用户点击超链接时将收到通知。
placeholder <input><textarea> 向用户提供可以在字段中输入的内容的提示。
playsinline <video> 布尔属性,表示视频要播放 "inline";即在元素的播放区域内。请注意,缺少此属性并不意味着视频将始终以全屏播放。
poster <video> 指示在用户播放或搜索之前显示的海报框架的 URL。
preload <audio><video> 指示是否应预加载整个资源、部分资源或不加载任何内容。
readonly <input><textarea> 指示元素是否可以编辑。
referrerpolicy <a>, <area>, <iframe>, <img>, <link>, <script> 指定获取资源时发送哪个引荐来源网址。
rel <a>, <area>, <link> 指定目标对象与链接对象的关系。
required <input>, <select>, <textarea> 指示该元素是否需要填写。
reversed <ol> 指示列表是否应按降序而不是升序显示。
role 全局属性 为辅助技术使用的元素定义明确的角色。
rows <textarea> 定义文本区域中的行数。
rowspan <td>, <th> 定义表格单元格应跨越的行数。
sandbox <iframe> 阻止 iframe 中加载的文档使用某些功能(例如提交表单或打开新窗口)。
scope <th> 定义与标头测试(在 th 元素中定义)相关的单元格。
scoped Non-standard Deprecated <style>
selected <option> 定义将在页面加载时选择的值。
shape <a>, <area>
size <input><select> 定义元素的宽度(以像素为单位)。如果元素的 type 属性是 textpassword,则它是字符数。
sizes <link>, <img>, <source>
slot 全局属性 将影子 DOM 影子树中的一个槽分配给一个元素。
span <col><colgroup>
spellcheck 全局属性 指示元素是否允许进行拼写检查。
src <audio>, <embed>, <iframe>, <img>, <input>, <script>, <source>, <track>, <video> 可嵌入内容的 URL。
srcdoc <iframe>
srclang <track>
srcset <img>, <source> 一张或多张响应式候选图片。
start <ol> 定义第一个数字(如果不是 1)。
step <input>
style 全局属性 定义将覆盖先前设置的样式的 CSS 样式。
summary Deprecated <table>
tabindex 全局属性 覆盖浏览器的默认 Tab 键顺序并遵循指定的顺序。
target <a>, <area>, <base>, <form> 指定在何处打开链接文档(对于 <a> 元素)或在何处显示收到的响应(对于 <form> 元素)
title 全局属性 将鼠标悬停在元素上时在工具提示中显示的文本。
translate 全局属性 指定在本地化页面时是否要翻译元素的属性值及其 Text 子节点的值,或者是否保持它们不变。
type <button>, <input>, <embed>, <object>, <ol>, <script>, <source>, <style>, <menu>, <link> 定义元素的类型。
usemap <img>, <input>, <object>
value <button>, <data>, <input>, <li>, <meter>, <option>, <progress>, <param> 定义将在页面加载时显示在元素中的默认值。
width <canvas>, <embed>, <iframe>, <img>, <input>, <object>, <video>

对于此处列出的元素,这确定了元素的宽度。

注意:对于所有其他实例,例如 <div>,这是一个旧属性,在这种情况下,应使用 CSS width 属性。

wrap <textarea> 指示文本是否应换行。

内容与 IDL 属性

¥Content versus IDL attributes

在 HTML 中,大多数属性都有两个面孔:内容属性和 IDL(接口定义语言)属性。

¥In HTML, most attributes have two faces: the content attribute and the IDL (Interface Definition Language) attribute.

内容属性是你从内容(HTML 代码)中设置的属性,你可以通过 element.setAttribute()element.getAttribute() 设置或获取它。即使预期值应该是整数,内容属性也始终是字符串。例如,要使用内容属性将 <input> 元素的 maxlength 设置为 42,你必须在该元素上调用 setAttribute("maxlength", "42")

¥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 element.setAttribute() or element.getAttribute(). The content attribute is always a string even when the expected value should be an integer. For example, to set an <input> element's maxlength to 42 using the content attribute, you have to call setAttribute("maxlength", "42") on that element.

IDL 属性也称为 JavaScript 属性。这些是你可以使用 JavaScript 属性(例如 element.foo)读取或设置的属性。当你获取它时,IDL 属性始终会使用(但可能会转换)底层内容属性来返回一个值,并且当你设置它时,它会在内容属性中保存一些内容。换句话说,IDL 属性本质上反映了内容属性。

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

大多数时候,IDL 属性会返回实际使用时的值。例如,<input> 元素的默认 type 是 "text",因此如果设置 input.type="foobar"<input> 元素将是文本类型(在外观和行为上),但 "type" 内容属性的值将为 "foobar"。但是,type IDL 属性将返回字符串 "text"。

¥Most of the time, IDL attributes will return their values as they are really used. For example, the default type for <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 属性并不总是字符串;例如,input.maxlength 是一个数字(有符号长整型)。使用 IDL 属性时,你读取或设置所需类型的值,因此 input.maxlength 始终会返回一个数字,而当你设置 input.maxlength 时,它需要一个数字。如果传递其他类型,它会自动转换为标准 JavaScript 类型转换规则指定的数字。

¥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 属性可以是 反映其他类型,例如无符号长整型、URL、布尔值等。不幸的是,没有明确的规则,并且 IDL 属性与其相应的内容属性结合使用的方式取决于属性。大多数时候,它会跟随 规范中规定的规则,但有时却不会。HTML 规范试图使其尽可能对开发者友好,但由于各种原因(主要是历史原因),某些属性的行为很奇怪(例如 select.size),你应该阅读规范以了解它们的行为到底如何。

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

布尔属性

¥Boolean Attributes

某些内容属性(例如 requiredreadonlydisabled)称为 布尔属性。如果存在布尔属性,则其值为 true,如果不存在,则其值为 false。

¥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 定义了对布尔属性允许值的限制:如果该属性存在,则其值必须是空字符串(等效地,该属性可能具有未分配的值),或者是与该属性的规范名称匹配的 ASCII 不区分大小写的值,且没有前导或尾随空格。以下示例是标记布尔属性的有效方法:

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

html
<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”。要表示假值,必须完全省略该属性。这一限制消除了一些常见的误解:以 checked="false" 为例,元素的 checked 属性将被解释为 true,因为该属性存在。

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

事件处理程序属性

¥Event handler attributes

警告:不鼓励使用事件处理程序内容属性。HTML 和 JavaScript 的混合通常会产生不可维护的代码,并且事件处理程序属性的执行也可能被内容安全策略阻止。

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

除了上表中列出的属性之外,全局 事件处理程序(例如 onclick)也可以在所有元素上指定为 内容属性

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

所有事件处理程序属性都接受字符串。该字符串将用于合成 JavaScript 函数,如 function name(/*args*/) {body},其中 name 是属性的名称,body 是属性的值。该处理程序接收与其对应的 JavaScript 事件处理程序相同的参数 — 大多数处理程序仅接收一个 event 参数,而 onerror 接收五个参数:event, source, lineno, colno, error.这意味着你通常可以在属性中使用 event 变量。

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

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

也可以看看

¥See also