<var>: The Variable element - HTML: HyperText Markup Language 编辑

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

Content categoriesFlow content, phrasing content, palpable content.
Permitted contentPhrasing content.
Tag omissionNone, both the starting and ending tag are mandatory.
Permitted parentsAny element that accepts phrasing content.
Implicit ARIA roleNo corresponding role
Permitted ARIA rolesAny
DOM interfaceHTMLElement

Attributes

This element only includes the global attributes.

Usage notes

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:

Default style

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

Examples

Basic example

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:

Overriding the default style

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.

CSS

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

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>

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

Result

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of '<var>' in that specification.
Living Standard
HTML5
The definition of '<var>' in that specification.
Recommendation

Browser compatibility

BCD tables only load in the browser

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:75 次

字数:7332

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文