Contents
How to change font in CSS
By using the CSS font
property, you can define a set of value for the text font:
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:
- font-stretch
- font-style
font-variant
- font-weight
- font-size
- line-height
- font-family
The syntax for CSS text styling
To use the CSS font
shorthand, 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
).
Using keywords for CSS fonts
Instead of defining each subproperty individually, you can also use keywords to make CSS set font styles:
<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 |