🎁 Exclusive offer: Get EXTRA Bits and Celebrate Bybit's 6th Birthday With $2.2M Prize Pool. Act Now!

Code has been added to clipboard!

The CSS Font Property

Reading time 2 min
Published Sep 8, 2017
Updated Jan 23, 2020

How to change font in CSS

By using the CSS font property, you can define a set of value for the text font:

Example
p.arial-font {
    font: 14px arial, sans-serif;
}      
p.georgia-font {
    font: italic bold 16px/32px Georgia, serif;
}

This property is actually a shorthand for seven subproperties:

The syntax for CSS text styling

To use the CSS fontshorthand, you need to define the subproperties in a certain order:

font: strech style variant weight size/line-height family;

The only values you must specify to make CSS set font properties are size and family. Other values are optional: if you skip them, the default values will be used instead.

Value Default value Explanation
stretch normal Defines if the text is condensed or stretched
style normal Defines if the text is italic or oblique
variant normal Defines if the text is in small caps
weight normal Defines the thickness of the characters
size medium Defines the height of the characters
line-height normal Defines the height of empty spaces between the text lines
family Depends on the browser Defines the font for the characters

Note: the line height must be defined after the font size with a forward slash between these two values (e.g., 20px/20px).

DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

Using keywords for CSS fonts

Instead of defining each subproperty individually, you can also use keywords to make CSS set font styles:

Example
<ul>
    <li style="font: caption;">Captioned Control Font.</li>
    <li style="font: icon;">Icon Label Font.</li>
    <li style="font: menu;">Dropdown Menu Font.</li>
    <li style="font: message-box;">Dialogue Box Font.</li>
    <li style="font: small-caption;">Smaller Caption Font.</li>
    <li style="font: status-bar;">Status Bar Font.</li>
</ul>

Find all the available values in the table below:

Value Description
caption CSS fonts used for captioned controls such as controls and buttons
small-caption A smaller version of the caption font
icon CSS fonts used for icon labels
menu CSS fonts used for various menus
message-box CSS fonts used for dialog boxes
status-bar CSS fonts used for window status bars

Browser support

Browser image
Chrome
1+
Browser image
Edge
12+
Browser image
Firefox
1+
Browser image
IE
5.5+
Browser image
Opera
7+
Browser image
Safari
1+

Mobile browser support

Browser image
Chrome
18+
Browser image
Firefox
4+
Browser image
Opera
10.1+
Browser image
Safari
1+