On this page9 sections
Lecture 041: MathML
| Category | 7: Interactive Elements, Graphics & Media |
| Lecture | 041 of 048 |
| Estimated video length | 8 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
Fractions, powers and square roots are hard to show with plain text. MathML is a markup language for maths that works right inside HTML, and screen readers can read it aloud properly.
Learning Objectives
- Write maths with
<math>and its main elements - Show numbers, variables, operators, powers, fractions and roots
- Choose between inline and block (display) formulas
1. The Building Blocks
| Element | Meaning | Example |
|---|---|---|
<mi> | Identifier (a variable) | x |
<mn> | Number | 2 |
<mo> | Operator | + = ( ) |
<mrow> | A group | (keeps parts together) |
<msup> | Superscript | x² |
<msub> | Subscript | x₁ |
<mfrac> | Fraction | ½ |
<msqrt> | Square root | √x |
2. A First Formula
Example
<p>
The area of a circle is
<math>
<mi>A</mi><mo>=</mo><mi>π</mi><msup><mi>r</mi><mn>2</mn></msup>
</math>.
</p><msup> takes two children: the base (r) and the power (2).
3. Fractions and Roots
Example
<p>
<math display="block">
<mi>x</mi><mo>=</mo>
<mfrac>
<mrow><mo>−</mo><mi>b</mi><mo>±</mo>
<msqrt><msup><mi>b</mi><mn>2</mn></msup><mo>−</mo><mn>4</mn><mi>a</mi><mi>c</mi></msqrt>
</mrow>
<mrow><mn>2</mn><mi>a</mi></mrow>
</mfrac>
</math>
</p><mfrac>takes a numerator, then a denominator.display="block"centres the formula on its own line, like in a textbook.
4. Subscripts
Example
<p>Water is <math><msub><mi>H</mi><mn>2</mn></msub><mi>O</mi></math>, and the first term is <math><msub><mi>a</mi><mn>1</mn></msub></math>.</p>For simple chemistry in text, <sub> (HTML Lecture 007) is often enough; MathML shines for real formulas.
Common Beginner Mistakes
1. Wrong number of children. <mfrac> and <msup> need exactly two children. Wrap groups in <mrow>.
2. Typing operators as plain text. Put +, = and brackets in <mo> so spacing is correct.
3. Images of formulas. They can't be read aloud, searched or zoomed. Use MathML.
Practice Questions
- Which element wraps all MathML?
- Which elements are for variables, numbers and operators?
- Write x² in MathML.
- Write the fraction 3 over 4.
- What does
display="block"do?
Practice Answers
<math><mi>,<mn>and<mo>.<math><msup><mi>x</mi><mn>2</mn></msup></math><math><mfrac><mn>3</mn><mn>4</mn></mfrac></math>- It shows the formula on its own centred line instead of inside the text.
Key Takeaways
<math>holds maths;<mi>,<mn>,<mo>are variables, numbers, operators.<msup>,<msub>,<mfrac>,<msqrt>build powers, subscripts, fractions and roots.<mrow>groups parts;display="block"gives a textbook-style formula.- MathML is accessible, unlike images of formulas.