font

Term side of the Card.
Topic: Text Handling
Unit title: font
Categories: Property
Explanation side of the Card

Explanation: The shorthand property that sets several font-related properties in one declaration, including `font-style`, `font-variant`, `font-weight`, `font-size`, `line-height` and `font-family`. Values that are not specified are reset to their initial values.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.
  • The safari browser supports this feature starting from version 1.
Availability: 

Example

Wrongcss
.title {
  line-height: 1.2;
  font: 700 2rem;
}
Rightcss
.title {
  font: 700 2rem/1.2 system-ui, sans-serif;
}

Size and family are required, and putting line height after the slash avoids the shorthand resetting it.

Common mistakes

  • The font shorthand is ignored because the family or size is missing.
  • A later font shorthand resets line-height and font-weight set earlier.

Key points

  • The shorthand includes both font-size and font-family, the two required values.
  • Values follow the order style, weight, then size with optional /line-height, then family.
  • Form controls get font: inherit so they match the page font.

Interview questions

Why do form inputs not use my page font?

Inputs and buttons don't inherit font by default. Add button, input, select, textarea { font: inherit; }.

Does the font shorthand reset other properties?

Yes. Any sub-property you leave out, such as line-height, is reset to its initial value.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy