CSS FONTS

The CSS font properties define allows authors to specify online fonts to display text on their web pages.

CSS FONT FAMILIES

In CSS, there are two types of font family names. Generic family – a group of font families with a similar look like “Serif” or “Monospace” and font-family – a specific font family like “Times New Roman” or “Arial”.

FONT-FAMILY

The font family of a text is set with the font-family property. It should hold several font names as a “fallback” system. If the browser does not support the first font, it tries the next font, and so on.

Start with the font you want, and end with a generic family, to let the browser pick a related font in the generic family if no other fonts are available. More than one font family is defined in a comma-separated list.

EXAMPLE

p {

font-family: “Times New Roman”, Times, serif;

FONT STYLE

The font-style property is often used to specify an italic text. This property has three values.

normal – The text is shown normally

italic – The text is shown in italics

oblique – The text is “leaning” (obliquely is very similar to italic, but less supported)

EXAMPLE

p.normal {

font-style: normal;

}

 

p.italic {

font-style: italic;

}

 

p.oblique {

font-style: oblique;

}

FONT SIZE

The font-size property sets the size of the text. Being able to manage the text size is important in web design. However, you should not use font size adjustments to make paragraphs look like headings, or headings look like paragraphs.

The font-size value can be an absolute or relative size.

ABSOLUTE SIZE

Sets the text to a specified size

Does not allow a user to change the text size in all browsers (bad for accessibility reasons)

Absolute size is useful when the physical size of the output is known

RELATIVE SIZE

Sets the size relative to surrounding elements

Allows a user to change the text size in browsers

SET FONT SIZE WITH PIXELS

Setting the text size with pixels gives you full control over the text size.

EXAMPLE

h1 {

font-size: 40px;

}

 

h2 {

font-size: 30px;

}

 

p {

font-size: 14px;

}

SET FONT SIZE WITH EM

To allow users to resize the text (in the browser menu), many developers use em instead of pixels. Unfortunately, there is still a problem with older versions of IE. The text becomes larger than it should when made larger, and smaller than it should when made smaller.

EXAMPLE

h1 {

font-size: 2.5em; /* 40px/16=2.5em */

}

 

h2 {

font-size: 1.875em; /* 30px/16=1.875em */

}

 

p {

font-size: 0.875em; /* 14px/16=0.875em */

}

USE A COMBINATION OF PERCENT AND EM

The solution that works in all browsers is to set a default font-size in percent for the <body> element. Our code now works great! It shows the same text size in all browsers and allows all browsers to zoom or resize the text.

EXAMPLE

body {

font-size: 100%;

}

 

h1 {

font-size: 2.5em;

}

 

h2 {

font-size: 1.875em;

}

 

p {

font-size: 0.875em;

}

FONT-WEIGHT

The font-weight property specifies the weight of a font.

Example

p.normal {

font-weight: normal;

}

 

p.thick {

font-weight: bold;

}

FONT VARIANT

The font-variant property specifies whether or not a text should be displayed in a small-caps font.

In a small-caps font, all lowercase letters are converted to uppercase letters. However, the converted uppercase letters appear in a smaller font size than the original uppercase letters in the text.

EXAMPLE

p.normal {

font-variant: normal;

}

 

p.small {

font-variant: small-caps;

}