1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
<header><h1><var>: The Variable element</h1></header><div class="section-content"><p>The <code><var></code> <a href="../index">HTML</a> 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.</p></div>
<h2 id="try_it">Try it</h2>
<div class="section-content"><iframe class="interactive is-tabbed-shorter-height" height="200" src="https://interactive-examples.mdn.mozilla.net/pages/tabbed/var.html" title="MDN Web Docs Interactive Example" loading="lazy"></iframe></div>
<h2 id="attributes">Attributes</h2>
<div class="section-content"><p>This element only includes the <a href="../global_attributes">global attributes</a>.</p></div>
<h2 id="usage_notes">Usage notes</h2>
<h3 id="related_elements">Related elements</h3>
<div class="section-content">
<p>Other elements that are used in contexts in which <code><var></code> is commonly used include:</p> <ul> <li>
<a href="code"><code><code></code></a>: The HTML Code element</li> <li>
<a href="kbd"><code><kbd></code></a>: The HTML Keyboard input element</li> <li>
<a href="samp"><code><samp></code></a>: The HTML Sample Output element</li> </ul> <p>If you encounter code that is mistakenly using <code><var></code> for style purposes rather than semantic purposes, you should either use a <a href="span"><code><span></code></a> with appropriate CSS or, an appropriate semantic element among the following:</p> <ul> <li><a href="em"><code><em></code></a></li> <li><a href="i"><code><i></code></a></li> <li><a href="q"><code><q></code></a></li> </ul>
</div>
<h3 id="default_style">Default style</h3>
<div class="section-content">
<p>Most browsers apply <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/font-style"><code>font-style</code></a> to <code>"italic"</code> when rendering <code><var></code>. This can be overridden in CSS, like this:</p> <div class="code-example">
<p class="example-header"><span class="language-name">css</span></p>
<pre data-signature="ooDCnAfPzSn74y40bqmjyc49gE3Ng9SrzwFS01bQorU=" data-language="css"><span class="token selector">var</span> <span class="token punctuation">{</span>
<span class="token property">font-style</span><span class="token punctuation">:</span> normal<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</pre>
</div>
</div>
<h2 id="examples">Examples</h2>
<h3 id="basic_example">Basic example</h3>
<div class="section-content">
<p>Here's a simple example, using <code><var></code> to denote variable names in a mathematical equation.</p> <div class="code-example">
<p class="example-header"><span class="language-name">html</span></p>
<pre data-signature="SPY8TiR6LQslieJgDUA+lAn/aZlX4aH+zNwIAY20Nic=" data-language="html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>A simple equation: <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>var</span><span class="token punctuation">></span></span>x<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>var</span><span class="token punctuation">></span></span> = <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>var</span><span class="token punctuation">></span></span>y<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>var</span><span class="token punctuation">></span></span> + 2<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span>
</pre>
</div> <h4 id="result">Result</h4>
<div class="code-example" id="sect1">
<iframe class="sample-code-frame" title="Basic example sample" id="frame_basic_example" width="650" height="80" src="https://live.mdnplay.dev/en-US/docs/Web/HTML/Element/var/runner.html?id=basic_example" loading="lazy"></iframe>
</div>
</div>
<h3 id="overriding_the_default_style">Overriding the default style</h3>
<div class="section-content">
<p>Using CSS, you can override the default style for the <code><var></code> element. In this example, variable names are rendered using bold Courier if it's available, otherwise it falls back to the default monospace font.</p> <h4 id="css">CSS</h4> <div class="code-example">
<p class="example-header"><span class="language-name">css</span></p>
<pre data-signature="eMzwureY2MmO3vFSVx7d6aZJFB1YB0AdLWORvivgdd8=" data-language="css"><span class="token selector">var</span> <span class="token punctuation">{</span>
<span class="token property">font</span><span class="token punctuation">:</span>
bold 15px <span class="token string">"Courier"</span><span class="token punctuation">,</span>
<span class="token string">"Courier New"</span><span class="token punctuation">,</span>
monospace<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</pre>
</div> <h4 id="html">HTML</h4> <div class="code-example">
<p class="example-header"><span class="language-name">html</span></p>
<pre data-signature="RedQ7E9Z7ECjbf9ggcbiAUSJKZSFMtKTwq+CdNFjSAs=" data-language="html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>
The variables <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>var</span><span class="token punctuation">></span></span>minSpeed<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>var</span><span class="token punctuation">></span></span> and <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>var</span><span class="token punctuation">></span></span>maxSpeed<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>var</span><span class="token punctuation">></span></span> control the minimum
and maximum speed of the apparatus in revolutions per minute (RPM).
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span>
</pre>
</div> <p>This HTML uses <code><var></code> to enclose the names of two variables.</p> <h4 id="result_2">Result</h4>
<div class="code-example" id="sect2">
<iframe class="sample-code-frame" title="Overriding the default style sample" id="frame_overriding_the_default_style" width="650" height="140" src="https://live.mdnplay.dev/en-US/docs/Web/HTML/Element/var/runner.html?id=overriding_the_default_style" loading="lazy"></iframe>
</div>
</div>
<h2 id="technical_summary">Technical summary</h2>
<div class="section-content"><figure class="table-container"><div class="_table"><table class="properties"> <tbody> <tr> <th scope="row"><a href="../content_categories">Content categories</a></th> <td> <a href="../content_categories#flow_content">Flow content</a>, <a href="../content_categories#phrasing_content">phrasing content</a>, palpable content. </td> </tr> <tr> <th scope="row">Permitted content</th> <td>
<a href="../content_categories#phrasing_content">Phrasing content</a>.</td> </tr> <tr> <th scope="row">Tag omission</th> <td>None, both the starting and ending tag are mandatory.</td> </tr> <tr> <th scope="row">Permitted parents</th> <td> Any element that accepts <a href="../content_categories#phrasing_content">phrasing content</a>. </td> </tr> <tr> <th scope="row">Implicit ARIA role</th> <td><a href="https://www.w3.org/TR/html-aria/#dfn-no-corresponding-role" target="_blank">No corresponding role</a></td> </tr> <tr> <th scope="row">Permitted ARIA roles</th> <td>Any</td> </tr> <tr> <th scope="row">DOM interface</th> <td><a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement"><code>HTMLElement</code></a></td> </tr> </tbody> </table></div></figure></div>
<h2 id="specifications">Specifications</h2>
<div class="_table"><table class="standard-table">
<thead><tr><th scope="col">Specification</th></tr></thead>
<tbody><tr><td><a href="https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-var-element">HTML Standard <br><small># the-var-element</small></a></td></tr></tbody>
</table></div>
<h2 id="browser_compatibility">Browser compatibility</h2>
<div class="_table"><table>
<thead>
<tr id="bct-browser-type">
<th></th>
<th colspan="6">Desktop</th>
<th colspan="6">Mobile</th>
</tr>
<tr id="bct-browsers">
<th></th>
<th>Chrome</th>
<th>Edge</th>
<th>Firefox</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
<th>WebView Android</th>
<th>Chrome Android</th>
<th>Firefox for Android</th>
<th>Opera Android</th>
<th>Safari on IOS</th>
<th>Samsung Internet</th>
</tr>
</thead>
<tbody><tr>
<th><code>var</code></th>
<td class="bc-supports-yes">1</td>
<td class="bc-supports-yes">12</td>
<td class="bc-supports-yes">1</td>
<td class="bc-supports-yes">Yes</td>
<td class="bc-supports-yes">15</td>
<td class="bc-supports-yes">≤4</td>
<td class="bc-supports-yes">4.4</td>
<td class="bc-supports-yes">18</td>
<td class="bc-supports-yes">4</td>
<td class="bc-supports-yes">14</td>
<td class="bc-supports-yes">≤3.2</td>
<td class="bc-supports-yes">1.0</td>
</tr></tbody>
</table></div><div class="_attribution">
<p class="_attribution-p">
© 2005–2023 MDN contributors.<br>Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.<br>
<a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/var" class="_attribution-link">https://developer.mozilla.org/en-US/docs/Web/HTML/Element/var</a>
</p>
</div>
|