<var>: The Variable element #
::: section-content
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 #
::: section-content ::: iframe ::: {.output-header .border-rounded-top}
HTML Demo: <var> #
Reset :::
::: {#warning-no-script .warning-container} ::: warning The interactive example cannot be shown because JavaScript is disabled. ::: :::
::: {#warning-mathml-not-supported .warning-container .hidden} ::: warning The interactive example cannot be shown because MathML is not supported by your browser. ::: :::
::: {#editor-container .editor-container .tabbed-shorter .hidden .border-rounded-bottom editor-type=“tabbed”} ::: {#tab-container .section .tabs} ::: {#tablist .tab-list role=“tablist”} HTML
CSS
JavaScript :::
::: {#html-panel .section .hidden tabindex=“0” role=“tabpanel” aria-labelledby=“html” aria-hidden=“true”} ::: {#html-editor}
The volume of a box is l × w × h, where l represents the length, w the width and h the height of the box.
::: :::::: {#css-panel .section .hidden tabindex=“0” role=“tabpanel” aria-labelledby=“css” aria-hidden=“true”} ::: {#css-editor} var { font-weight: bold; } ::: :::
::: {#js-panel .section .hidden tabindex=“0” role=“tabpanel” aria-labelledby=“js” aria-hidden=“true”} ::: {#js-editor} ::: ::: :::
::: {#output .output-container}
Output #
::: :::
::: {.section .console-container .hidden aria-hidden=“true”}
Console Output #
![] clear console
::: {#console .console} ::: :::
::: {#html-output .output .editor-tabbed} %html-content% ::: ::: :::
Attributes #
::: section-content This element only includes the global attributes. :::
Usage notes #
Related elements #
::: section-content
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 #
::: section-content
Most browsers apply
font-style
to "italic"
when rendering <var>
. This can be overridden in CSS,
like this:
::: code-example [css]{.language-name}
var {
font-style: normal;
}
::: :::
Examples #
Basic example #
::: section-content
Here's a simple example, using <var>
to denote variable names in a
mathematical equation.
::: code-example [html]{.language-name}
<p>A simple equation: <var>x</var> = <var>y</var> + 2</p>
:::
Result #
::: {#sect1 .code-example} ::: iframe ::: ::: :::
Overriding the default style #
::: section-content
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 #
::: code-example [css]{.language-name}
var {
font:
bold 15px "Courier",
"Courier New",
monospace;
}
:::
HTML #
::: code-example [html]{.language-name}
<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 #
::: {#sect2 .code-example} ::: iframe ::: ::: :::
Technical summary #
::: section-content
Content categories | Flow content, phrasing content, palpable content. |
---|---|
Permitted content | Phrasing content. |
Tag omission | None, both the starting and ending tag are mandatory. |
Permitted parents | Any element that accepts phrasing content. |
Implicit ARIA role | No corresponding role |
Permitted ARIA roles | Any |
DOM interface | HTMLElement |
Specifications #
::: _table #
Specification #
HTML Standard
[#
the-var-element]{.small}
:::
Browser compatibility #
::: _table Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
var
1 12 1 Yes 15 ≤4 4.4 18 4 14 ≤3.2 1.0
:::
::: _attribution
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5
or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/var{._attribution-link}
:::