The font properties in CSS control how text looks on a webpage: its typeface (family), size, weight, and style. Good font choices improve readability and overall design aesthetics.
font-family to choose fonts with proper fallbacks.font-size, font-weight, and font-style to adjust the look.font shorthand to set multiple font-related properties in one line."Arial", "Georgia").sans-serif).16px, 1rem).400, bold).The basic way to set a font for an element is using font-family with one or more fallbacks:
/* Basic font-family with fallback fonts */
selector {
font-family: "Font Name", fallback1, fallback2;
}
The font shorthand lets you define several font properties in one declaration:
/* font: style variant weight size/line-height family */
.text-sample {
font: italic small-caps 16px/1.5 "Georgia", serif;
}
Here are some common font-related declarations you will use frequently:
/* Basic font family for paragraphs */
p {
font-family: "Arial", sans-serif;
}
/* Larger, bold heading text */
h1 {
font-size: 32px;
font-weight: bold;
}
/* Using shorthand to set multiple font properties */
.highlight-text {
font: italic small-caps 18px/1.6 "Georgia", serif;
}
This text uses the Georgia font, in italic, with a slightly larger size and relaxed line spacing.
In this example:
font-family: "Georgia", serif; chooses Georgia, then any serif font as fallback.font-size: 18px; makes the text bigger than the default 16px.font-style: italic; tilts the text to emphasize it.line-height: 1.5; adds breathing space between lines for better readability.Some important font-related properties and sample values are listed below:
| Property | Description | Example Value |
|---|---|---|
font-family |
Sets the font type (with fallbacks). | "Arial", sans-serif |
font-size |
Sets the size of the font. | 16px, 1em, 1rem |
font-style |
Sets normal, italic, or oblique style. | italic |
font-weight |
Controls how thick or thin characters appear. | normal, bold, 400, 700 |
font |
Shorthand to set style, variant, weight, size/line-height, and family. | italic small-caps bold 16px/1.5 Arial, sans-serif |
font-family (e.g., "Roboto", Arial, sans-serif).em or rem for scalable, responsive text.font-family values. Observe how the mood of the text changes.font-size, font-weight, and font-style on headings vs body text.font shorthand to set style, weight, size, line-height, and family in a single line.