In this tutorial we're going to talk about fonts... Fonts are the most amazing thing ever invented on the face of the Earth! Note: all fonts that are mentioned in this tutorial are acceptable fonts to be used on a web page. Use of arbitrary fonts that some people might not have is not a good thing. That means when the browser renders the site and they don't have the font, they cannot view it properly or as you intend them to. Please see this Wiki article on the core fonts to use on a web page for more information.

Font Property

I'm sure we have all opened a text editor such as textpad, notepad, or textedit. In fact, we've probably all opened something like Microsoft Office or OpenOffice that gives us the ability to choose different fonts, adjust the margins of a page, zoom into a page, change the font size, color, weight, and other variations of that font. In CSS, there is a font property that allows us to specify fonts to be used on a web page. We can do basic formatting of the font itself using this font property.

Differences of Fonts

This CSS font-family property allows us to choose a font family that will be used on our web page. There are two different types of font families in CSS.

  1. Generic Families
  2. Font Families

A Generic Family is a group of fonts that have the same look. Monospace type font is an example.
A Font Family is a specific group of fonts such as Arial or Times New Roman.

There are three things about fonts we need to be aware of:

  1. Serif Fonts
  2. Sans-Serif Fonts
  3. Monospace Fonts

Serif Fonts have small lines at the end of some characters. Most notably is the character of a lower-case "g" or upper-case "A". An example of a Serif type font is "Times New Roman", "Georgia", and "Trebuchet MS". Here are font examples:

  • Times New Roman
  • Georgia
  • Trebuchet MS

Sans-Serif literally means "without serif". So there are no small lines at the end of any characters. An example of a Sans-Serif type font is "Arial", "Comic Sans MS", and "Verdana". Here are font examples:

  • Arial
  • Comic Sans MS
  • Verdana
  • Impact

Monospace means all character have the same width. So a capital "B" and lower-case "p" will have the same width and all spaces between character will be the same. Here are font examples:

  • Courier New
  • Lucida Console

font-family

Great! Now that we know the differences between fonts and such we can finally get to some CSS! So this so-called font-family property allows us to call a series of fonts. An example of this would look like the following:

body {font-family:font-family:Georgia, "Times New Roman", 
Times, serif;}
p.fancy {font-family:Tahoma, Geneva, sans-serif;}

Note the use of multiple fonts. This is known as a fallback system for the browser... If someone doesn't have the font "Georgia" for some wild reason then the browser will change the font to the next which is "Times New Roman". And let us say that they didn't have Times New Roman for some crazy reason then the browser would change the font to "Times" and so forth. Start with a font you want and then choose more generic fonts such as plain old "serif".

font-style

The font-style property can be used in various ways. You can have three possible values.

  1. Italic
  2. Normal
  3. Oblique

Consider the following:

p {font-family:"Times New Roman", Georgia, Times, serif;}
p.italic {font-style:italic;}
p.normal {font-style:normal;}
p.oblique {font-style:oblique;}

All text within the p element will be of the font family "Times New Roman" because of the font-family property. From there we have created three separate classes for p elements for the three values we can have for the font-style property. Our result will look like so:

Italic text, Normal text (what a surprise), Oblique text (what's the difference?).
Using the italic value we get our italic text. Using the normal value we get out normal text — what a surprise there, normal text. Last but not least, using our oblique value we get oblique text.
Wait... What? It looks the same as the italic text?! Believe it or not, oblique text is actually different from italic text. Oblique text is slightly slanted to the right. Unlike italic text, Oblique text does NOT use a different glyph shape for each character. So for "Times New Roman" it will use the "Times New Roman" glyph but only slightly distorted. You can read all about the differences in this Wiki article on Oblique text if it interests you.

font-size

Font size is important on a web page! You wouldn't want a font where users have to get three inches away from their screen to read some text and you don't want the text to be in-your-face huge. Depending on the font and the size of the glyph, text on a web page is usually anywhere from 11pm to 14px. Now... without confusing you or myself we'll examine the possible values of this property.

  1. larger
  2. smaller
  3. xx-large
  4. x-large
  5. large
  6. medium
  7. small
  8. x-small
  9. xx-small
  10. percentage denoted by %
  11. pixel denoted by px
  12. em unites denoted by em

Ok, we'll take this one step at a time... Consider the following:

p.xx-small {font-size:xx-small;}
p.x-small {font-size:x-small;}
p.small{font-size:small;}
p.normal {font-size:medium;}
h3 {font-size:large;}
h2 {font-size:x-large;}
h1 {font-size:xx-large;}
p.custom-font {font-size: 2em;}
p.pixel-font {font-size:18px;}

Ok, now let's see some examples! xx-small text, x-small text, small text, medium text, large text, x-large text, xx-large text, 2em text, 18px text.

Excellent, now let us decode this all. I'm sure we can all agree on font sizes xx-small — xx-large, just be sure they're used appropriately... We should really concentrate on the last two. The "em" and "px" units.

The Pixel

If you've ever created an html table that had "width=53" then you have used the unit called the pixel before. What is a pixel? A pixel is the smallest element of information of an image. This holds true for a digital graphic file and the very screen you're looking at right now. The smallest unit is called a pixel. So let's try something here... How about text that is 1px text? (1 pixel text is what that says). See how small that is? If you want to learn more about the pixel, check out this Wiki article for more details... a lot more details.

The Em unit

Em units are proportional to the size of the current font. 1 em = the current font size. 2 em would be double the current font size. If we have a font that is originally 10pt then 1 em would equal 10pt font. If we wanted that font to be twice the size we would use 2em which would equal 20pt font and so on.

1 em font
10pt font
2 em font
20pt font

If this em unit interests you please check out this Wiki article to read more about the em unit.

Percent with fonts

Percentages have increasingly become more commonplace over the past few years. Sometimes percentages are used to define widths or heights but they can also be used to define the size of fonts. Here's an example:

body {font-size:100%;}
p.half {font-size:50%;}

So in the body, the font is 100% of the size and in the class "half" the font is 50% of the size. So we have..
100% size text and 50% size text. Not surprisingly, our 100% size text is well... 100%. Our 50% text, however, is quite small and probably not something we would use very often.

The rest of this tutorial will be published on wednesday! Stay posted!!

Be Sociable, Share!