Configuring Fonts
The typeface of text on a web page, and its particular characteristics such as size and weight are defined using font and its related properties.
font-family
The font-family property accepts a comma-separated list of font family names. If the browser can't find the first font on the list, it tries to find the next font, and so on, until it finds a font or uses default.If the font name contains spaces, enclose the name with single or double quotation marks.
At the end of the list of font choices, you should insert a generic font family. CSS offers five generic font family values:
- serif - A typeface style typically denoted by flourishes or flared ends on each character; ("Times New Roman", Garamond)
- sans-serif - A typeface design with plain or straight features; (Arial, Verdana, Helvetica, Tahoma)
- cursive - A cursive or handwriting-like typeface ("Lucida Handwriting", "Zapf-Chancery")
- monospace - A fixed-width font, commonly used for displaying code (Courier, "MS Courier New", Prestige)
- fantasy - A highly stylized typeface ("Comic Sans", Whimsy, Critter, Cottonwood")
h2 { font-family: Verdana, Arial, Helvetica, sans-serif; }
p { font-family: "Comic Sans", Whimsy, Critter, fantasy; }
Font Embedding
If you want to use a particular font in your web page, other than "web-safe" fonts, you can include it with @font-face rule.The @font-face rule allows for defining a custom font family and linking that family to a resource where the font file data resides.
To use this CSS rule, you need to set two properties: font-family (the family name for the custom font) and src (the font source URI).
Include a font file somewhere on your server, and refer to it with CSS, as you can see in the fallowing example:
@font-face {
font-family: 'Sansation_Light';
src: url('Sansation_Light.ttf');
}
/* Once the new font is included, you can use it */
h1 {
font-family: 'Sansation_Light', sans-serif;
}
- If the font file is located at a different domain, use a full URL:src: url('http://coursesweb.net/Sansation_Light.ttf');
- If a browser did not support the @font-face rule (or could not find or did not understand the particular format of the font), it would ignore the font name when it tried to follow the font-family rule and render the element using the browser-defined sans-serif font.
The problem with @font-face is that the browsers not recognise the same file font format.
The IE browser supports only the Embedded OpenType Font format (.eot). Firefox, Chrome, Safari, and Opera support fonts of type OpenType Face (.otf) and TrueType Format (.ttf).
The Font Squirrel @font-face Generator (www.fontsquirrel.com) takes any uploaded font and converts it into all the formats mentioned earlier as well as offers sample CSS embedding code.
Google Font Directory offers an embeddable API and tool that wraps the @font-face declarations, and it has collected a set of open source fonts, free to use: Google Font Directory.
font-size
The font-size property controls the size of the text in the element and may be defined as a fixed size or a size relative to the font size of the parent element.It can take on different values and several units:
- Length units - Pixels (px), Em (em), Inches (in), Centimeters (cm), Millimeters (mm), Points (pt), Picas (pc).
A pixel is the smallest dot that can be made on a computer screen. Em units refer to the default font size of the user’s browser.
A point, in terms of the CSS specification, is equal to 1/72 of an inch, and a pica is equal to 12 points. - Relative units - larger, smaller ; Values relative to the parent element’s size; typically representing a step up or down the previous scale.
- Fixed-size - xx-small, x-small, small, medium, large, x-large si xx-largexx-small, x-small, small, medium, large, x-large si xx-large
- percentage - A percentage value (%), which indicate the size of text in relation to the size of the parent element’s font-size.
- selector { font-size: value; }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>font-size</title>
<style type="text/css"><!--
#idp1 { font-size: 16px; }
#idp2 { font-size: 125%; }
--></style>
</head>
<body>
<p id="idp1">Web site: http://CoursesWeb.net</p>
<p id="idp2">RO Web site: www.MarPlo.net</p>
</body>
</html>
- Result:
Web site: http://CoursesWeb.net
RO Web site: www.MarPlo.net
font-weight
The font-weight property controls the weight, or thickness, of the characters in a font.It can take on different values:
- normal , bold - Text representing normal weight type (default) and bolded type.
- bolder , lighter - Sets a weight relative to the weight inherited from the parent element.
- 100 to 900 - A nine-step scale, in increments of 100, ranging from thin (100) to black (900).
- selector { font-weight: value; }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>font-weight</title>
<style type="text/css"><!--
#idp1 { font-weight: bold; }
#idp2 { font-weight: 500; }
--></style>
</head>
<body>
<p id="idp1">CSS course.</p>
<p id="idp2">Font lesson.</p>
</body>
</html>
- Result:
CSS course.
Font lesson.
font-variant
The font-variant specifies whether or not a text should be displayed in a small-caps font.- normal - Selects the normal variant of a font face (default).
- small-caps - All lowercase letters are converted to uppercase letters. The converted uppercase letters appears in a smaller font size than the original uppercase letters in the text.
- selector { font-variant: value; }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>font-variant</title>
<style type="text/css"><!--
#idp1 { font-variant: small-caps; }
--></style>
</head>
<body>
<p id="idp1">Free CSS course.</p>
</body>
</html>
- Result:
Free CSS course.
font-style
The font-style sets the style of the font.- normal - Normal type (default).
- italic - Italicized type.
- oblique - Oblique type.
- selector { font-style: value; }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>font-style</title>
<style type="text/css"><!--
#idp1 { font-style: italic; }
--></style>
</head>
<body>
<p id="idp1">Have a good life.</p>
</body>
</html>
- Result:
Have a good life.
Multiple values for font property
Using the font property, you can set the individual font properties (font-style, font-variant, font-weight, font-size, font-family) in the same definition, separated by space.- Syntax:
- selector { font: font-style_val font-variant_val font-weight_val font-size_val font-family_val; }
Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>font</title>
<style type="text/css"><!--
#idp1 { font: italic small-caps bold 14px Georgia, serif; }
--></style>
</head>
<body>
<p id="idp1">Have a good life.</p>
</body>
</html>
- Result:
Have a good life.
Creating style sheets <<-- Previous ----------- Next -->> Configuring Text