<var>:变量元素

<var> HTML 元素表示数学表达式或编程上下文中变量的名称。它通常使用当前字体的斜体版本呈现,尽管该行为取决于浏览器。

¥The <var> HTML element 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.

Try it

属性

¥Attributes

该元素仅包含 全局属性

¥This element only includes the global attributes.

使用说明

¥Usage notes

相关元素

¥Related elements

<var> 常用的上下文中使用的其他元素包括:

¥Other elements that are used in contexts in which <var> is commonly used include:

  • <code>:HTML 代码元素
  • <kbd>:HTML 键盘输入元素
  • <samp>:HTML 示例输出元素

如果你遇到错误地将 <var> 用于样式目的而不是语义目的的代码,你应该使用具有适当 CSS 的 <span>,或者使用以下元素中的适当语义元素:

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

默认样式

¥Default style

大多数浏览器在渲染 <var> 时都会应用 font-style"italic"。这可以在 CSS 中重写,如下所示:

¥Most browsers apply font-style to "italic" when rendering <var>. This can be overridden in CSS, like this:

css
var {
  font-style: normal;
}

示例

¥Examples

基本示例

¥Basic example

这是一个简单的示例,使用 <var> 表示数学方程中的变量名称。

¥Here's a simple example, using <var> to denote variable names in a mathematical equation.

html
<p>A simple equation: <var>x</var> = <var>y</var> + 2</p>

结果

¥Result

覆盖默认样式

¥Overriding the default style

使用 CSS,你可以覆盖 <var> 元素的默认样式。在此示例中,变量名称以粗体呈现,如果可用,则使用 Courier,否则它将回退到默认的等宽字体。

¥Using CSS, you can override the default style for the <var> element. In this example, variable names are rendered in bold, using Courier if it's available, otherwise it falls back to the default monospace font.

CSS

css
var {
  font:
    bold 15px "Courier",
    "Courier New",
    monospace;
}

HTML

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

此 HTML 使用 <var> 括住两个变量的名称。

¥This HTML uses <var> to enclose the names of two variables.

结果

¥Result

技术总结

¥Technical summary

内容类别 流量内容措辞内容,可触及的内容。
允许的内容 措辞内容.
标签遗漏 无,开始和结束标记都是强制性的。
允许的父级 任何接受 措辞内容 的元素。
隐式 ARIA 角色 没有对应的角色
允许的 ARIA 角色 任何
DOM 接口 HTMLElement

规范

Specification
HTML Standard
# the-var-element

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility