HTML
Variable element
(
<var>
) represents the name of a variable in a mathematical expression or a programming context.
It's typically presented using an italicized version of the current typeface, although that behavior is browser-dependent.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
| 内容类别 | 流内容 , 措词内容 ,可触及内容。 |
|---|---|
| 准许内容 | 措词内容 . |
| Tag omission | None, both the starting and ending tag are mandatory. |
| Permitted parents | Any element that accepts 措词内容 . |
| Implicit ARIA role | 无对应角色 |
| Permitted ARIA roles | 任何 |
| DOM 接口 |
HTMLElement
|
此元素只包括 全局属性 .
Other elements that are used in contexts in which
<var>
is commonly used include:
<code>
: The HTML Code element
<kbd>
: The HTML Keyboard input element
<samp>
: The HTML Sample Output element
If you encounter code that is mistakenly using
<var>
for style purposes rather than semantic purposes, you should either use a
<span>
with appropriate CSS or, an appropriate semantic element among the following:
Most browsers apply
font-style
to
"italic"
when rendering
<var>
. This can be overridden in CSS, like this:
var {
font: bold 15px "Courier", "Courier New", monospace;
}
Here's a simple example, using
<var>
to denote variable names in a mathematical equation.
<p>A simple equation: <var>x</var> = <var>y</var> + 2 </p>
The output:
Using CSS, you can override the default style for the
<var>
element. In this example, variable names are rendered using bold Courier if it's available, otherwise it falls back to the default monospace font.
var {
font: bold 15px "Courier", "Courier New", monospace;
}
<p>The variables <var>minSpeed</var> and <var>maxSpeed</var> control the minimum and maximum speed of the apparatus in revolutions per minute (RPM).</p>
This HTML uses
<var>
to enclose the names of two variables.
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of '<var>' in that specification. |
实时标准 | |
|
HTML5
The definition of '<var>' in that specification. |
推荐 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
var
|
Chrome 完整支持 Yes | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 Yes | Opera 完整支持 Yes | Safari 完整支持 Yes | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 4 | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
完整支持