|
1 | | -<ComingSoon /> |
| 1 | +--- |
| 2 | +title: The font Shorthand Property |
| 3 | +description: Learn how to use the font shorthand property to set multiple typography properties (style, weight, size, height, and family) in a single, concise CSS declaration. |
| 4 | +keywords: [CSS font shorthand, typography shorthand, concise CSS, font property order, font-weight, font-size, line-height, CodeHarborHub] |
| 5 | +tags: [CSS font shorthand, typography shorthand, concise CSS, font property order, font-weight, font-size, line-height, CodeHarborHub] |
| 6 | +sidebar_label: font Shorthand |
| 7 | +--- |
| 8 | + |
| 9 | +The **`font`** property is the ultimate shorthand for typography. It allows you to set up to six related font properties in a single, efficient CSS declaration. Using the shorthand dramatically reduces the length of your stylesheet and improves readability. |
| 10 | + |
| 11 | +<AdsComponent /> |
| 12 | +<br /> |
| 13 | + |
| 14 | +## The Full `font` Syntax |
| 15 | + |
| 16 | +When using the `font` shorthand, you must follow a very strict order for the values, and some properties are optional while others are **required**. |
| 17 | + |
| 18 | +### Required and Optional Order |
| 19 | + |
| 20 | +The simplified, most common syntax looks like this: |
| 21 | + |
| 22 | +```css title="font Shorthand Syntax" |
| 23 | +font: <font-style> <font-weight> <font-size> / <line-height> <font-family>; |
| 24 | +``` |
| 25 | + |
| 26 | +| Property | Required / Optional | Notes | |
| 27 | +| :--- | :--- | :--- | |
| 28 | +| **`font-style`** | Optional | E.g., `italic`, `normal`. Must come before `font-weight`. | |
| 29 | +| **`font-weight`** | Optional | E.g., `bold`, `400`. Must come before `font-size`. | |
| 30 | +| **`font-size`** | **Required** | Must be the third value, followed immediately by the line height. | |
| 31 | +| **`/ line-height`** | Optional | The `/` is required if you include `line-height`. | |
| 32 | +| **`font-family`** | **Required** | Must be the final value. | |
| 33 | + |
| 34 | +### The Critical Requirement |
| 35 | + |
| 36 | +The two **required** values, **`font-size`** and **`font-family`**, must always be the last two items in the list (with `font-family` being the absolute last). |
| 37 | + |
| 38 | +## Practical Examples |
| 39 | + |
| 40 | +### 1. Simple Body Text |
| 41 | + |
| 42 | +This example sets the size and the font stack. All other properties default to `normal`. |
| 43 | + |
| 44 | +```css title="styles.css" |
| 45 | +/* Sets size to 1rem and uses the sans-serif font stack */ |
| 46 | +body { |
| 47 | + font: 1rem sans-serif; |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +**This is shorthand for:** |
| 52 | +`font-size: 1rem;` |
| 53 | +`font-family: sans-serif;` |
| 54 | +`font-style: normal;` |
| 55 | +`font-weight: normal;` |
| 56 | + |
| 57 | +### 2. Heading with Style and Weight |
| 58 | + |
| 59 | +This example includes `font-weight` and `font-style` before the required properties. |
| 60 | + |
| 61 | +```css title="style.css" |
| 62 | +/* Italic, Bold, 2.5rem size, using a serif font */ |
| 63 | +h1 { |
| 64 | + font: italic bold 2.5rem serif; |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### 3. Including `line-height` |
| 69 | + |
| 70 | +To specify `line-height`, you must insert a **forward slash (`/`)** immediately after the `font-size` value. |
| 71 | + |
| 72 | +```css title="styles.css" |
| 73 | +/* Bold, 18px size, line height of 1.6, using a sans-serif stack */ |
| 74 | +.article-text { |
| 75 | + font: bold 18px / 1.6 "Roboto", Arial, sans-serif; |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +<AdsComponent /> |
| 80 | +<br /> |
| 81 | + |
| 82 | +## A Note on Unspecified Values |
| 83 | + |
| 84 | +When you use the `font` shorthand, any omitted optional properties (like `font-style` or `font-weight`) are **reset to their initial default values** (`normal` or `400`). |
| 85 | + |
| 86 | +This is important because if you had set a custom `font-weight: 700;` earlier in your stylesheet, and then later used `font: 1rem sans-serif;` (omitting `bold`), the weight would be **reset to `normal` (400)**. |
| 87 | + |
| 88 | +This is a key difference between shorthand and individual properties. |
| 89 | + |
| 90 | +## Interactive `font` Shorthand Demo |
| 91 | + |
| 92 | +Use the live editor to test different combinations. See how omitting `line-height` or `font-style` affects the text, and observe the strict order requirement. |
| 93 | + |
| 94 | +<CodePenEmbed |
| 95 | + title="Interactive font Shorthand Demo" |
| 96 | + penId="YPqQZde" |
| 97 | +/> |
| 98 | + |
| 99 | +**Challenge:** Remove the `/ 1.8` from the CSS line. The line spacing will instantly snap back to the browser's default, demonstrating that the `line-height` property was successfully reset. |
0 commit comments