LEARNING OBJECTIVES โต
- Understand the WHATWG specification for
<var>(Variables and Placeholders). - Differentiate between
<var>,<i>(idiomatic text), and<em>(emphasis). - Represent mathematical formulas and physics equations using
<var>,<sup>, and<sub>. - Style variables for both mathematical notation (serif italics) and programmatic syntax (monospace).
๐ The Mental Model & Story (Intuitive Foundation)
Imagine reading a high school physics textbook:
"The kinetic energy $E_k$ of an object is equal to $\frac{1}{2} m v^2$, where $m$ is mass and $v$ is velocity."
In that equation, $m$ and $v$ are not literal letters of the alphabet; they are placeholders representing numbers that can vary depending on the experiment. If a 1,000 kg car travels at 20 m/s, you replace $m$ with 1,000 and $v$ with 20.
In HTML, the <var> element represents a variable in a mathematical expression, a physical formula, or a programmatic context (such as function parameters or template placeholders). It alerts browsers and assistive technology that the enclosed character is a variable rather than standard reading text.
Mathematical Formula:
<var>E</var> = <var>m</var><var>c</var><sup>2</sup>
| | |
Energy Mass Speed of Light (Constants & Variables)
Technical Deep Dive & Specifications
WHATWG Specification & Semantic Definition
According to the WHATWG HTML Living Standard:
- The
<var>element represents a variable. - This could be an actual variable in a mathematical expression or programming context, an identifier representing a constant, a symbol identifying a physical quantity, a function parameter, or simply a placeholder in prose.
Default Browser Rendering
By default, browsers render <var> with:
var {
font-style: italic;
}
Semantic Differentiation: <var> vs <i> vs <em>
| Element | Primary Semantic Purpose | Example | User-Agent Styling |
|---|---|---|---|
<var> |
A variable, mathematical parameter, or placeholder | <var>x</var> + <var>y</var> = <var>z</var> |
Italics |
<em> |
Stress emphasis that changes the meaning of a sentence | "I love this framework" | Italics |
<i> |
Idiomatic text, technical terms, taxonomic names, thoughts | Homo sapiens, Schadenfreude | Italics |
Math Typography vs Programmatic Variable Styling
In mathematical documents, variables are traditionally rendered in serif italics (such as Computer Modern, Times New Roman, or Latin Modern Math):
/* Mathematical Variable Styling */
.math var {
font-family: "Cambria Math", "Latin Modern Math", "Times New Roman", serif;
font-style: italic;
font-size: 1.1em;
padding: 0 0.1em;
}
In programming documentation, variables inside code snippets are often monospace tokens styled with distinct variable syntax colors:
/* Programmatic Variable Styling */
code var {
font-family: ui-monospace, Menlo, Consolas, monospace;
font-style: italic;
color: #f59e0b; /* Amber variable highlight */
}
๐ป Interactive Code Playground
Starter Code
Line-by-Line Code Breakdown
- Line 26โ34: Formats
.math-displayusing classical serif math fonts (Cambria Math,Latin Modern Math) and styles<var>in deep ocean blue. - Line 36โ42: Targets
pre code varso programmatic variables inherit the monospace typeface while keeping italics and an amber color token. - Line 57โ59: Combines
<var>,<sub>0</sub>(subscript for initial velocity), and<sup>2</sup>(superscript for squared time) to build a clean mathematical equation. - Line 65โ67: Nests
<var>inside a JavaScript<code>block to mark function parameters (userName,emailAddress,userRole) as variable placeholders.
Expected Browser Render Output
- A centered mathematical card showing $d = v_0t + \frac{1}{2}at^2$ with elegant italic blue serif variables.
- A dark-themed code block where function arguments
userName,emailAddress, anduserRolestand out in italicized amber monospace font.
๐๏ธ Hands-On Exercise
๐ฏ The Challenge: Pythagorean Theorem & Euclidean Distance
Instructions:
- Create a geometry documentation snippet defining the Distance Formula between two 2D points: $P_1(x_1, y_1)$ and $P_2(x_2, y_2)$.
- Use
<var>for all variable placeholders ($d$, $x_1$, $x_2$, $y_1$, $y_2$). - Use
<sub>for point subscripts and<sup>for squared exponents. - Provide an explanation sentence describing what each variable represents.
๐ Starter Code Sandbox
โ ๏ธ Common Pitfalls
- Using
<i>for Mathematical Variables: While<i>renders text in italics, it lacks the semantic meaning of a variable or mathematical placeholder. - Using
<var>for Emphasis: Never use<var>when you want to emphasize a word in a sentence (use<em>). - Applying Monospace Globally to
<var>:<var>is widely used in math formulas. If your global stylesheet setsvar { font-family: monospace; }, mathematical equations will look distorted. Only apply monospace when scoped insidecode varorpre var.
๐ก Pro Tips
- MathML Integration: For complex mathematical publications (integrals, fractions, matrices), pair
<var>with modern HTML5<math>(MathML) elements:<math display="block"> <mi>E</mi><mo>=</mo><mi>m</mi><msup><mi>c</mi><mn>2</mn></msup> </math> - Template Documentation Replacement: In CLI documentation, use
<var>to denote arguments that users must replace with their own values:<code>git checkout -b <var>branch-name</var></code>.
๐ Key Takeaways
- The
<var>element represents a variable in a mathematical equation, physics formula, or programmatic context. - Browsers render
<var>as italicized text by default (font-style: italic). - Differentiate
<var>(variables) from<em>(stress emphasis) and<i>(idiomatic phrasing). - Combine
<var>with<sup>(superscripts) and<sub>(subscripts) for mathematical expressions. - Nest
<var>inside<code>or<pre>to identify function parameters and command placeholders. - --