In the early days of the web, font choices were limited. However, with the advancements in CSS, you now have the ability to load and use any font on your web pages. CSS provides a number of properties related to fonts, including the shorthand font
property, font-family
, font-weight
, font-stretch
, font-style
, and font-size
.
The font-family
property allows you to specify the font you want to use for an element. Fonts are actually composed of sub-fonts that provide different styles such as bold, italic, and light. You can use specific fonts, such as “Helvetica”, or define multiple font options, with the second option being used as a fallback if the first is not available.
Web Safe Fonts, which are pre-installed on various operating systems, can also be used. These fonts are divided into Serif, Sans-Serif, and Monospace categories. However, there is no guarantee that these fonts will be available on every system. In such cases, you can use generic names, like “sans-serif” or “monospace”, as fallback options.
The font-weight
property sets the width of a font. Predefined values like “normal” and “bold” can be used or numeric keywords ranging from 100 to 900. These numeric values may not map to a specific font, in which case CSS will make the font at least as bold as the preceding value.
With the font-stretch
property, you can choose a narrow or wide face of the font if available. The allowed values range from “ultra-condensed” to “ultra-expanded”.
font-style
allows you to apply italic or oblique styles to a font. This property also supports the value “normal”.
The font-size
property determines the size of the font. You can specify the size using length values (e.g., pixels or em) or choose from predefined keywords like “small” or “large”.
Additionally, the font-variant
property, originally used to change text to small caps, has three valid values: “normal”, “inherit”, and “small-caps”.
To simplify the font declaration, you can use the font
property to define multiple font properties in a single line, including font-stretch
, font-style
, font-variant
, font-weight
, font-size
, and font-family
. The order of these properties must be maintained.
To load custom fonts, you can use the @font-face
rule, which enables you to add new font families to your webpage by linking font files. This allows you to use any font you want by including the src
property to specify the URI(s) of the font file(s). Fonts can be in formats like woff
, woff2
, eot
, otf
, or ttf
.
It’s important to consider the performance implications of loading custom fonts on your webpage. Ensuring optimized font loading can greatly enhance the user experience.
Tags: CSS, web typography, font properties, custom fonts, @font-face
, font-family
, font-weight
, font-stretch
, font-style
, font-size