Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 36 ยท Components and Props

Typography

Use the Material UI Typography component well: variants such as h1 and body1, the component prop for semantic HTML, color, alignment, noWrap and responsive sizes.

Text is most of a user interface. Material UI's Typography component renders text using a consistent type scale from the theme, so headings, body copy and captions look related without you setting font sizes by hand. This lesson explains the variants, how they map to HTML elements, and the common props.

The type scale

Typography has a variant prop. Each variant is a named style in theme.typography:

Variant Typical use Default HTML tag
h1 to h6 headings <h1> to <h6>
subtitle1, subtitle2 secondary headings <h6>
body1 (default) main body text <p>
body2 denser body text <p>
button button labels <span>
caption small notes <span>
overline small uppercase labels <span>
inherit copy parent's style <p>

Real MUI code (read-only):

JSX
import Typography from '@mui/material/Typography';

<Typography variant="h3" component="h1" gutterBottom>
  Dashboard
</Typography>
<Typography variant="subtitle1" color="text.secondary">
  Overview of this week
</Typography>
<Typography variant="body1">
  Body text uses the theme's body1 size, weight and line height.
</Typography>

gutterBottom adds a small bottom margin. Without it, Typography has no margin at all, which is why MUI pages use Stack or Box for spacing.

Style and semantics are separate

A key idea: the variant controls how text looks, and the element controls what it means. Search engines and screen readers rely on heading levels (h1 to h6) to understand structure, but you may want an h2 that looks small or an h4-sized title that is semantically h1. Use component to set the tag and variant to set the look:

JSX
<Typography variant="h4" component="h1">Page title that is an h1</Typography>
<Typography variant="h6" component="h2">Section title that is an h2</Typography>

You can also change the default mapping for the whole app with the variantMapping prop (or in the theme's component defaults) if you want, say, subtitle1 to be a <p>.

Do not skip heading levels or pick h3 only because it looks right. Choose the semantically correct tag with component and pick the look with variant. Each page should have exactly one h1.

Color, alignment and wrapping props

JSX
<Typography color="primary">Theme color</Typography>
<Typography color="text.secondary">Muted text</Typography>
<Typography color="error">Error text</Typography>
<Typography align="center">Centered</Typography>
<Typography noWrap>Long text is cut off with an ellipsis instead of wrapping</Typography>
<Typography sx={{ fontWeight: 'bold', fontStyle: 'italic' }}>Custom weight</Typography>
  • color takes palette paths ('primary', 'text.secondary', 'error.main').
  • align is left, center, right or justify.
  • noWrap truncates a single line; the element needs a constrained width to see it.
  • paragraph adds bottom margin (older API; gutterBottom or spacing from a parent is preferred).
  • Any sx styles work as usual.

Stand-in: a type scale and semantic tags

The first runnable example is a stand-in Typography with a scale close to MUI's defaults and a component prop. It shows that variant and tag are independent: the console line prints the real tag each row renders.

Stand-in: Typography variants and the component prop

Open your browser's element inspector on the preview to confirm the real tags (data-tag shows them).

Fonts, weights and the theme

The scale comes from theme.typography. Set fontFamily once and all variants follow; override a single variant to adjust it:

JSX
const theme = createTheme({
  typography: {
    fontFamily: '"Inter", "Roboto", sans-serif',
    h1: { fontWeight: 700, fontSize: '3rem' },
    button: { textTransform: 'none' },
  },
});

Defaults use rem units, so users who raise the browser's base font size get larger text. That is good for accessibility; avoid replacing the scale with fixed px values. For larger headings on big screens only, use responsive font sizes: either an sx object (fontSize: { xs: '2rem', md: '3rem' }) or the responsiveFontSizes(theme) helper, which scales variants between breakpoints.

Putting a text block together

The following second stand-in builds a small article card and counts words with a plain function. It shows a good pattern: a semantic structure (heading, meta line, body) wrapped around one Typography-like component.

Stand-in: an article block built from typography

Common mistakes

  • Using Typography variant="h1" several times on one page. Use component to keep one h1.
  • Hard-coding fontSize: 14 in pixels everywhere instead of using a variant, so the type scale drifts.
  • Forgetting that Typography has no margin; wrap groups in Stack (see the layout chapter) instead of adding margin to every line.
  • Putting block Typography (a <p>) inside another <p>. Use component="span" for inline text inside paragraphs, otherwise React warns about invalid DOM nesting.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the variant prop of Typography control?

    variant selects a style (size, weight, line height). The tag is chosen by the default mapping or the component prop.
  2. 2. You need a page title that is an h1 but looks like an h4. What do you write?

    variant sets the look, component sets the element, so they can differ.
  3. 3. Which prop truncates one line of text with an ellipsis?

    noWrap applies overflow hidden, text-overflow ellipsis and no wrapping (the element needs a limited width).
  4. 4. Why are theme typography sizes in rem?

    rem units follow the root font size, which supports user preferences and accessibility.

Summary

  • Typography renders text from the theme's type scale via variant (h1 to h6, subtitle1/2, body1/2, caption, overline).
  • Separate look from meaning: variant for style, component for the HTML tag.
  • Useful props: color, align, gutterBottom, noWrap and sx.
  • Configure fontFamily and variant overrides in the theme; keep rem units for accessibility.

Next: Paper, Card and Chip