CSS Font Shorthand Property: Complete Guide with Syntax, Examples, and Practical Uses
The CSS font shorthand property is a powerful feature that allows web developers to define multiple font-related properties in a single declaration. Instead of writing separate CSS rules for font style, font variant, font weight, font size, line height, and font family, you can combine them into one compact and easy-to-maintain statement.
Typography plays an important role in website design. The right font style, size, weight, and spacing improve readability, create visual consistency, and make web pages more attractive. Understanding the CSS font shorthand property helps developers write cleaner CSS, reduce repetitive code, and manage typography more efficiently.
In this guide, you will learn what the CSS font shorthand property is, how its syntax works, which properties it controls, and how to use it with practical examples.
What Is the CSS Font Shorthand Property?
The CSS font property is a shorthand property used to specify several font-related CSS properties in one declaration.
Normally, you might write separate declarations for different font settings:
p {
font-style: italic;
font-variant: small-caps;
font-weight: bold;
font-size: 18px;
line-height: 1.6;
font-family: Arial, sans-serif;
}Using the font shorthand property, you can combine these declarations into a single rule:
p {
font: italic small-caps bold 18px/1.6 Arial, sans-serif;
}Both examples specify the same font settings.
The shorthand property makes CSS code shorter and easier to organize, especially when working with large websites or projects that contain multiple typography styles.
CSS Font Shorthand Property Syntax
The basic syntax of the CSS font shorthand property is:
selector {
font: font-style font-variant font-weight font-stretch font-size/line-height font-family;
}Each value controls a different aspect of typography.
Here is an example:
h2 {
font: italic small-caps bold 24px/1.5 Georgia, serif;
}In this example:
italicdefines the font style.small-capsspecifies the font variant.boldsets the font weight.24pxdefines the font size.1.5sets the line height.Georgia, serifspecifies the font family.
The font-stretch value can also be included in the shorthand when needed.
Important: The font size and font family are required in the shorthand declaration. Other font-related values are optional.
Properties Included in the CSS Font Shorthand
The CSS font shorthand property can control several individual CSS properties.
1. font-style
The font-style property defines the style of text. It is commonly used to display normal, italic, or oblique text.
Possible values include:
normal– Displays regular text.italic– Displays italic text.oblique– Displays slanted text.
Example:
p {
font: italic 18px Arial, sans-serif;
}This example displays paragraph text in an italic style with a font size of 18 pixels.
2. font-variant
The font-variant property controls variations in the appearance of characters. Its commonly used shorthand-compatible value is small-caps.
Example:
h3 {
font: small-caps 22px Georgia, serif;
}This declaration displays the heading using small-cap typography when supported by the selected font.
3. font-weight
The font-weight property controls the thickness or boldness of text.
Common values include:
normal– Standard font thickness.bold– Bold text.lighter– A lighter weight relative to the inherited weight.bolder– A heavier weight relative to the inherited weight.100to900– Numeric font weights where supported by the font.
Example:
h1 {
font: 700 32px Arial, sans-serif;
}The heading appears with a bold font weight of 700.
4. font-stretch
The font-stretch property controls the width of characters by selecting a condensed or expanded font face when available.
Common values include:
normalultra-condensedextra-condensedcondensedsemi-condensedsemi-expandedexpandedextra-expandedultra-expanded
Percentage values can also be used with supported fonts.
Example:
p {
font: condensed 18px Arial, sans-serif;
}This requests a condensed font face if the selected font family provides one. It does not simply compress every font horizontally.
5. font-size
The font-size property defines the size of the text.
Common units include:
px– Pixels.em– Relative to the parent element’s font size.rem– Relative to the root element’s font size.%– A percentage of the inherited font size.vw– Relative to the viewport width.
Example:
p {
font: 18px Arial, sans-serif;
}This displays paragraph text at 18 pixels.
The font size is a required part of the font shorthand declaration.
6. line-height
The line-height property controls the vertical spacing between lines of text.
It is included after the font size using a forward slash (/).
Example:
p {
font: 16px/1.6 Arial, sans-serif;
}Here:
16pxis the font size.1.6is the line-height multiplier.
A line height of 1.6 provides comfortable spacing for many paragraphs and improves readability.
You can also specify a length:
p {
font: 16px/26px Arial, sans-serif;
}7. font-family
The font-family property specifies which typeface should be used to display text.
It is required in the CSS font shorthand property.
Example:
body {
font: 16px Arial, Helvetica, sans-serif;
}The browser first attempts to use Arial. If Arial is unavailable, it tries Helvetica and then a generic sans-serif font.
It is good practice to include fallback fonts to ensure that text remains readable across different devices and operating systems.
Practical Examples of the CSS Font Shorthand Property
The following examples demonstrate how to use the CSS font property in different situations.
Example 1: Basic Font Shorthand
p {
font: 16px Arial, sans-serif;
}This example sets the paragraph font size to 16 pixels and uses Arial as the primary font.
Example 2: Bold Font
h1 {
font: bold 32px Arial, sans-serif;
}This declaration creates a bold heading with a font size of 32 pixels.
Example 3: Italic Font
p {
font: italic 18px Georgia, serif;
}This displays paragraph text in italic style using Georgia.
Example 4: Font Weight and Line Height
h2 {
font: 600 24px/1.5 Arial, sans-serif;
}This sets the heading to a font weight of 600, a font size of 24 pixels, and a line height of 1.5.
Example 5: Combining Multiple Font Values
article p {
font: italic small-caps bold 17px/1.7 Georgia, serif;
}This declaration combines font style, font variant, font weight, font size, line height, and font family.
Example 6: Using Font Stretch
h3 {
font: condensed 22px Arial, sans-serif;
}This requests a condensed version of Arial if that font face is available.
Example 7: Using Different Font Sizes
body {
font: 16px/1.6 Arial, sans-serif;
}
h1 {
font: bold 36px/1.2 Georgia, serif;
}
h2 {
font: 600 28px/1.3 Arial, sans-serif;
}
h3 {
font: bold 22px/1.4 Arial, sans-serif;
}This example establishes a simple typography system for a website using different font sizes, weights, and line heights.
Understanding the Order of CSS Font Shorthand Values
The order of values is important when using the CSS font shorthand property.
The general order is:
- Font style.
- Font variant.
- Font weight.
- Font stretch.
- Font size.
- Line height.
- Font family.
For example:
p {
font: italic small-caps 700 condensed 18px/1.5 Arial, sans-serif;
}The browser interprets the values according to the shorthand syntax.
However, the optional values do not need to be included if they are not required.
A simpler declaration is also valid:
p {
font: 18px Arial, sans-serif;
}Remember that font size and font family must always be included.
CSS Font Shorthand vs Individual Font Properties
There are two common ways to define typography in CSS: using individual properties or using the shorthand property.
Consider the following comparison:
| Individual CSS Properties | CSS Font Shorthand |
|---|---|
font-style: italic; | italic |
font-variant: small-caps; | small-caps |
font-weight: bold; | bold |
font-size: 18px; | 18px |
line-height: 1.6; | /1.6 |
font-family: Arial, sans-serif; | Arial, sans-serif |
Individual properties example:
p {
font-style: italic;
font-weight: bold;
font-size: 18px;
line-height: 1.6;
font-family: Arial, sans-serif;
}Shorthand property example:
p {
font: italic bold 18px/1.6 Arial, sans-serif;
}The shorthand approach is more compact, while individual properties can make it easier to understand and update specific typography settings.
Important Rules When Using the CSS Font Shorthand Property
Although the font shorthand property makes CSS easier to write, there are some important rules to understand.
1. Font Size Is Required
You must specify a font size when using the shorthand property.
Valid example:
p {
font: 18px Arial, sans-serif;
}2. Font Family Is Required
The font family must also be included.
Valid example:
p {
font: bold 18px Arial, sans-serif;
}3. Line Height Uses a Forward Slash
If you want to specify line height, place it immediately after font size using /.
p {
font: 16px/1.5 Arial, sans-serif;
}4. Optional Values Must Follow the Correct Order
Optional values such as font style, font variant, font weight, and font stretch must appear before the font size.
p {
font: italic bold 18px/1.6 Arial, sans-serif;
}5. Unspecified Font Subproperties Are Reset
One important behavior of shorthand properties is that they reset omitted subproperties to their initial values.
For example:
p {
font: 18px Arial, sans-serif;
}This does not preserve previously specified font weight, style, or other omitted font subproperties. They are reset according to the shorthand rules.
Therefore, avoid using the shorthand property when you need to preserve certain existing font settings unless you explicitly include them.
6. The Font Shorthand Can Use System Fonts
CSS also provides system font keywords that allow an element to use the platform’s default interface typography.
Examples include:
button {
font: caption;
}
input {
font: menu;
}
body {
font: message-box;
}Other system font keywords include icon, small-caption, and status-bar.
These keywords represent system-defined font settings rather than a specific font family and size combination.
Common Mistakes to Avoid
Beginners sometimes make mistakes when writing the CSS font shorthand property. Understanding these mistakes can help you create valid and reliable stylesheets.
Mistake 1: Forgetting the font family
Incorrect:
p {
font: 18px;
}Correct:
p {
font: 18px Arial, sans-serif;
}Mistake 2: Forgetting the forward slash before line height
Incorrect:
p {
font: 16px 1.5 Arial, sans-serif;
}Correct:
p {
font: 16px/1.5 Arial, sans-serif;
}Mistake 3: Placing font weight after font size
Incorrect:
p {
font: 18px bold Arial, sans-serif;
}Correct:
p {
font: bold 18px Arial, sans-serif;
}Mistake 4: Expecting omitted properties to remain unchanged
Incorrect assumption:
p {
font-weight: bold;
font: 18px Arial, sans-serif;
}The second declaration resets the font weight to its initial value because bold was omitted.
Correct:
p {
font: bold 18px Arial, sans-serif;
}Mistake 5: Using a font that is not available
A font may not be installed on every user’s device. Always provide suitable fallback fonts.
body {
font: 16px Arial, Helvetica, sans-serif;
}Advantages of the CSS Font Shorthand Property
The CSS font shorthand property offers several benefits for website development.
1. Reduces CSS Code: It combines multiple font declarations into one statement, helping reduce repetitive code.
2. Improves Code Organization: Related typography settings can be maintained together, making stylesheets easier to read.
3. Saves Development Time: Developers can define several font settings with a single declaration.
4. Makes Typography Consistent: Reusable shorthand declarations can help maintain consistent typography throughout a website.
5. Simplifies Maintenance: When a complete font configuration needs to change, developers can update a single declaration.
6. Supports Responsive Design: Font shorthand can be used inside media queries to adjust typography for different screen sizes.
Example:
body {
font: 16px/1.6 Arial, sans-serif;
}
@media (max-width: 768px) {
body {
font: 15px/1.5 Arial, sans-serif;
}
}This example adjusts the font size and line height for smaller screens.
CSS Font Shorthand Property: Complete HTML and CSS Example
The following example demonstrates how to use the CSS font shorthand property in a simple webpage.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Font Shorthand Example</title>
<style>
body {
font: 16px/1.6 Arial, sans-serif;
margin: 30px;
}
h1 {
font: bold 36px/1.2 Georgia, serif;
}
h2 {
font: 600 26px/1.4 Arial, sans-serif;
}
p {
font: normal 17px/1.7 Arial, sans-serif;
}
.highlight {
font: italic bold 18px/1.5 Georgia, serif;
}
.small-text {
font: 14px/1.5 Verdana, sans-serif;
}
</style>
</head>
<body>
<h1>CSS Font Shorthand Property</h1>
<h2>Understanding CSS Typography</h2>
<p>
The CSS font shorthand property allows developers
to define multiple font-related settings in one declaration.
</p>
<p class="highlight">
This paragraph uses bold and italic typography.
</p>
<p class="small-text">
This paragraph uses a smaller font size.
</p>
</body>
</html>In this example, different elements use different font shorthand declarations. The body, headings, paragraphs, and highlighted text each have their own typography settings.
You can copy this code into an HTML file and open it in a web browser to see how the CSS font shorthand property works.
Best Practices for Using CSS Font Shorthand
Follow these recommendations when working with the CSS font shorthand property:
- Use readable font sizes for body text and headings.
- Include appropriate fallback fonts in every font-family list.
- Choose a suitable line height to improve paragraph readability.
- Use consistent font weights throughout your website.
- Avoid unnecessary font variations that make text difficult to read.
- Use
remunits when you want typography to scale relative to the root font size. - Be careful when combining shorthand and individual font declarations.
- Use media queries when typography needs to change on smaller screens.
- Choose font families that support the characters and languages used by your website.
- Use individual font properties when you need to modify only one setting without resetting other font subproperties.
Conclusion
The CSS font shorthand property is an essential tool for managing typography efficiently in modern web development. It allows developers to combine font style, font variant, font weight, font stretch, font size, line height, and font family into a single declaration.
By understanding its syntax, value order, required properties, and reset behavior, you can write cleaner CSS and create consistent typography across your website.
Whether you are building a personal blog, an educational website, a business website, or a responsive web application, learning the CSS font shorthand property will help you manage text styles more effectively.
Practice the examples in this guide to understand how different font values work together and how you can use them to create attractive, readable, and well-organized web pages.
Frequently Asked Questions (FAQs)
1. What is the CSS font shorthand property?
The CSS font shorthand property allows developers to combine multiple font-related properties, including font style, weight, size, line height, and family, into one declaration.
2. What is the syntax of the CSS font shorthand property?
The general syntax is:
font: font-style font-variant font-weight font-stretch font-size/line-height font-family;Font size and font family are required, while the other values are optional.
3. Which properties are included in the CSS font shorthand?
The CSS font shorthand can set font-style, font-variant, font-weight, font-stretch, font-size, line-height, and font-family.
4. Is font size mandatory in CSS font shorthand?
Yes. The font size is required when using the CSS font shorthand property, except when using a system font keyword.
5. Is font family required in the CSS font shorthand property?
Yes. A font-family value must be included in a normal font shorthand declaration.
6. How do you set line height using CSS font shorthand?
Use a forward slash after the font size.
p {
font: 16px/1.5 Arial, sans-serif;
}7. Can I use bold and italic together in CSS font shorthand?
Yes. You can combine font weight and font style in the same declaration.
p {
font: italic bold 18px Arial, sans-serif;
}8. What happens to omitted font properties in shorthand?
Omitted font subproperties are reset to their initial values. Therefore, previously defined font settings may be lost when you use the shorthand property.
9. What is the difference between CSS font and font-family?
The font-family property specifies only the typeface, while the font shorthand property can define several typography settings together.
10. Why should developers use the CSS font shorthand property?
It reduces repetitive CSS declarations, improves code organization, saves development time, and makes typography easier to manage.