The Font Properties

fontsThe CSS font properties define the font family, boldness, size, and the style of a text.

CSS gives you great control over the way your text is displayed.
You can change the text size, color, style, and more. You probably already knew how to make text bold or underlined, but did you know you could resize your font using percentages?

 

 

All CSS Font Properties

PropertyDescription
font Sets all the font properties in one declaration
font-family Specifies the font family for text
font-size Specifies the font size of text
font-style Specifies the font style for text
font-variant Specifies whether or not a text should be displayed in a small-caps font
font-weight Specifies the weight of a font

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")
  • font family - a specific font family (like "Times New Roman" or "Arial")
Generic familyFont familyDescription
Serif Times New Roman
Georgia
Serif fonts have small lines at the ends on some characters
Sans-serif Arial
Verdana
"Sans" means without - these fonts do not have the lines at the ends of characters
Monospace Courier New
Lucida Console
All monospace characters have the same width

Note It's worth mentioning that on computer screens, sans-serif fonts are considered easier to read than serif fonts .

 

Font Family

The font family of a text is set with the font-family property.

The font-family property should hold several font names as a "fallback" system. If the browser does not support the first font, it tries the next font.

Start with the font you want, and end with a generic family, to let the browser pick a similar font in the generic family, if no other fonts are available.

Note: If the name of a font family is more than one word, it must be in quotation marks, like: "Times New Roman".

More than one font family is specified in a comma-separated list:

body {font-family: arial, "Times New Roman", Times, serif;}

Using the body tag in the example above sets the font for the whole page unless the font family is changed in another tag (selector).

 


Font Style

The font-style property is mostly used to specify 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" (oblique is similar to italic, but less supported)
p. {font-style:normal;}
p. {font-style:italic;}
p. {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.

Always use the proper HTML tags, like <h1> - <h6> for headings and <p> for 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:

h1 {font-size:48px;}
h2 {font-size:32px;}
p {font-size:12px;}

The example above allows browsers to resize the text.

Note: The example above does not work in IE, prior version 9.

The text can be resized in all browsers using the zoom tool, however this resizes the entire page not just the text.

The size of the font
Text such as headings should not just be a paragraph (p) in a large font - you should still use headings tags (h1, h2,h3 etc.) In practice, you could make the font-size of a paragraph larger than that of a heading, but this doesn't help with HTML semantics and SEO (Search engine optimization).

Set Font Size With Em

To avoid the resizing problem with older versions of Internet Explorer, many developers use em instead of pixels.

The em size unit is recommended by the W3C.

1em is equal to the browser font size (If you look at your browsers setting you will see the font size). The default text size in most browsers is 16px. So, the default size of 1em is 16px.

The size can be calculated from pixels to em using this formula: pixels/16=em

h1 {font-size:3.0em;}  (equivalent to 48px )
h2 {font-size:1.875em;} (equivalent to 30px )
p {font-size:0.875em;} (equivalent to 12px )

In the example above, the text size in em is the same as the previous example in pixels.
However, with the em size, it is possible to adjust the text size in all browsers.

All the font properties in one declaration

This example demonstrates how to use the shorthand property for setting all of the font properties in one declaration.

h1 {font: normal small-caps bold 36px Georgia, sans-serif; }

This is just a small example of what you can do with the font declaration. With the new CSS3 specifications  greater things are possible.

 

Leave your comments

Post comment as a guest

0 / 300 Character restriction
Your text should be in between 10-300 characters
terms and condition.
  • No comments found

Get The Latest Browsers

firefoxLogo

google

News Letter

Sign up for news letter