This page looks at some of the CSS properties that allow us to add style to fonts. We discuss the following properties: font-family, font-size, font-weight, font-style, and color.
The font-family allows us to specify a font along with a set of back-up fonts that we would like the browser to use. The fonts that we use everyday on the web can be categorized into families. Two popular font-families are serif and sans-serif. The serif fonts are fonts with small lines (or strokes) trailing from the edges of the text (letters or symbols). The other type, sans-serif do not have such edges -- in fact, the word "sans" means without and hence sans-serif literally means a font without sans! Due to lack of such lines at the edges of the font, some readers find sans-serif fonts to be cleaner and hence possibly, more readable. Besides sans-serif and serif families, the other popular font-families are cursive, mono-space (fixed-width fonts), and fantasy.
For a given font from a font-family, we can use additional design styles like font-weight (bold, bolder, lighter, normal), font-size (how big is the each letter in the font), and font-style (italics, oblique, or normal). Before we go ahead, let us take a look at these properties in some more detail.
You might be wondering, how come the property name for font color is just "color" and
not "font-color". Well, one reason for that could be the fact that the "color" property may apply
to other non-font elements as well.
Now that we are familiar with various CSS properties for fonts, let us see a handful of examples.
Our first example focuses on the font-family property. The HTML file for this example consists of five simple paragraph elements. We use different IDs to each one of them so that we can use that to assign different font-family property values to each paragraph.
<!doctype html> <html> <head> <title> Learning CSS: Making Fonts More Stylish! </title> <link type="text/css" rel="stylesheet" href="font_family.css"> </head> <body> <p id="id_sans_serif"> [Sans-Serif] Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> <p id="id_serif"> [Serif] Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> <p id="id_monospace"> [Monospace] Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> <p id="id_cursive"> [Cursive] Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> <p id="id_fantasy"> [Fantasy] Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> </body> </html>
The above HTML file includes "font_family.css" CSS file. This file adds different font types to different paragraphs. For each paragraph, we assign a different font-family type -- the value consists of a representative font (or fonts) and the base font type for that family.
#id_sans_serif { font-family: Helvetica, sans-serif; } #id_serif { font-family: "Times New Roman", serif; } #id_monospace { font-family: Courier, monospace; } #id_cursive { font-family: "Apple Chancery", "URW Chancery L", cursive; } #id_fantasy { font-family: Papyrus, fantasy; }
Let us load the above HTML and see the output (we add it below). We would find that different paragraphs have different font types. The following output was generated using Firefox running in Linux -- if you load the example on a different browser or on a different operating system, you might find the output a little different. This is because different browsers/operating-systems might vary in terms of the fonts that they have.
With our second example, we shift the focus to the font-size property. As the name suggests, we can use it to assign various font sizes to a text element. We reuse the earlier HTML file. For the sake of readability, we give different IDs to each paragraph element and the accompanying CSS file assigns different sizes to fonts of these paragraph elements. Here is the HTML file.
<!doctype html> <html> <head> <title> Learning CSS: Making Fonts More Stylish! </title> <link type="text/css" rel="stylesheet" href="font_size.css"> </head> <body> <p id="id_x_small"> Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> <p id="id_small"> Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> <p id="id_medium"> Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> <p id="id_large"> Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> <p id="id_x_large"> Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> </body> </html>
The "font_size.css" file assigns different font-sizes to different paragraphs. We keep the default font-family for all paragraphs as sans-serif.
p { font-family: Helvetica, sans-serif; } #id_x_small { font-size: x-small; } #id_small { font-size: small; } #id_medium { font-size: medium; } #id_large { font-size: large; } #id_x_large { font-size: x-large; }
Let us load the above HTML file. As expected, the output would show that different paragraphs have fonts that are varying in sizes.
Our next example looks at the font-weight property. We continue to reuse the earlier HTML and once again, for readability, rename the ID values for each paragraphs. The idea is that we assign different font-weight to different paragraphs. Here is the HTML file.
<!doctype html> <html> <head> <title> Learning CSS: Making Fonts More Stylish! </title> <link type="text/css" rel="stylesheet" href="font_weight.css"> </head> <body> <p id="id_lighter"> [Weight: Lighter] Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> <p id="id_normal"> [Weight: Normal] Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> <p id="id_bold"> [Weight: Bold] Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> <p id="id_bolder"> [Weight: Bolder] Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> </body> </html>
Here is the accompanying CSS file ("font_weight.css"). Once again, we keep the default font-family for all paragraphs as sans-serif.
p { font-family: Helvetica, sans-serif; } #id_lighter { font-weight: lighter; } #id_normal { font-weight: normal; } #id_bold { font-weight: bold; } #id_bolder { font-weight: bolder; }
Let us load the above HTML page and see the output (provided below). It is okay, if you find the output a little surprising. You might find it surprising for that different font-weight values, we still see similar boldness. Thus, the values "bold" and "bolder" look like, while the values "lighter" and "normal" look the same. Well, for some of the common fonts, the browser typically have only two values, one light (the one with the "normal" weight) and the other one bold. So, when we specify a value for the font-weight, the browser would pick the nearest available font and substitute it. One mystery solved!
Our last example looks at the last remaining properties: font-style. We continue to reuse the earlier HTML and once again, for readability, rename the ID values for each paragraphs. The only thing that we change is the font-style property.
<!doctype html> <html> <head> <title> Learning CSS: Making Fonts More Stylish! </title> <link type="text/css" rel="stylesheet" href="font_style.css"> </head> <body> <p id="id_normal"> [Style: Normal] Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> <p id="id_italic"> [Style: Italic] Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> <p id="id_oblique"> [Style: Oblique] Evolution of dinosaurs began during the Triassic period, they became dominant animals during the Jurassic period, and alas, became extinct during Cretaceous period. </p> </body> </html>
Here is the accompanying CSS file ("font_style.css"). Once again, we keep the default font-family for all paragraphs as sans-serif.
p { font-family: Helvetica, sans-serif; } #id_normal { font-style: normal; } #id_italic { font-style: italic; } #id_oblique { font-style: oblique; }
Let us load the above HTML page and see the output (provided below). For the last two paragraph, we have styles as italic and oblique, but they look identical! Well, this can happen, if the browser does not have two different font-styles for the current font (Helvetica, in this case). If it does not, then it can substitute one for the other.
Like some of the other properties, CSS provides a single property to specify all the font properties, in one shot: font. This property allows us to specify 7 individual font-related properties in one shot. While we have seen most of these properties (font-style, font-weight, font-size, and font-size) above, some of these properties are new as well: font_stretch, font_variant, and line_height.
Here are some details about this property.