Cascading Style Sheets (CSS): Text
- Cascading Style Sheets (CSS): Text
- Text-Related Properties
- The SPAN Tag
Cascading Style Sheets (CSS): Text
In the previous article ("Cascading Style Sheets (CSS): Basics"), you learned how to add a style sheet to a document. Now, let's take a step back and look at that style sheet in more detail.
1. <STYLE TYPE="text/css"> <!-- H1 { font-family: Verdana } --> </STYLE>
1. The STYLE element is a container that goes in the HEAD section of your HTML document. The TYPE attribute specifies the type of style sheet being used, which in this case is CSS. The STYLE element can contain any number of different styles. This style element contains one style; it applies to the H1 tag, changing its font to Verdana (see Figure 1).
Figure 1 A look at how the style itself is constructed.
The first part of the style is called the selector. The selector specifies where the style should be applied. In this case, the selector is H1, indicating that this style should be applied to the text in all H1 tags in this document.
The curly braces ("{"and"}") enclose the body of the style. Within the body of the style are lines with properties and values. The word on the left side of the colon is called a property, and the right side is the value assigned to the property. In this example, the font-family property is given the value Verdana, which effectively tells the browser to use the Verdana font for H1 elements in this document.
You can specify more than one property in a single style, and they needn't be in any particular order. If you need to specify more than one property in a style, you must end each one with a semicolon (;) character. Figure 2 shows this style in use.
H1 { font-family: Verdana; font-size: 24px; }
Figure 2 css3.html: Both font-family and font-size properties.
You can also specify more than one style in a single style sheet. This style sheet (see Figure 3) has styles for both H1 and P elements:
H1 { font-family: Verdana; font-size: 24px; } P { font-family: Georgia; font-size: 18px; }
Figure 3 css4.html: Styles for both H1 and P in the same style sheet.
Now that you know the basic format for adding a style sheet to a document, instead of showing you the whole STYLE section for each change we make, the rest of our examples will just concentrate on the individual style that we're working on.
Type and Measurements
Before we discuss CSS measurement units for text, it's important to understand the terminology used in measuring type.
Back in the days when "setting type" meant a physical activity handled by skilled professionals with leaded type and wooden typecases, the body size of a type referred to the actual size of the metal block on which the letter was formed. This is a size somewhat larger than any of the measurable dimensions of the printed letters, though not by any specific ratio. There is no analogous measurement in electronic type, so when we refer to a "twelve-point" typeface, a certain amount of ambiguity cannot be resolved. Figure 4 shows a type sample with some common names used for parts of type.
Figure 4 Type measurement terminology.
Unit Measurements
With that in mind, let's look at some measuring units. The table that follows lists the English name, the CSS name, and a short description for each measurement unit.
English |
CSS |
Description |
Pixel |
px |
A pixel is the distance from one dot to the next on a computer screen (pixel literally means picture element). |
Point |
pt |
A point is 1/72 inch. The number of pixels that is depends on the resolution of your screen, but it is one pixel on 72 dpi systems. |
Pica |
pc |
12 points. |
Em |
em |
The "body size" of the font. That is, if the font size is 12 points, one em is 12 points. |
En |
en |
1/2 em. |
x-height |
ex |
The height of a lower-case "x". |
Inch |
in |
The length of the thumb of King Henry VIII. |
Centimeter |
cm |
1/100 meter. |
Millimeter |
mm |
1/1,000 meter |
Let's look at an example. If you want to display the word Quack in three-inch type (see Figure 5), you can use this:
<HTML> <HEAD> <STYLE TYPE="text/css"> P { font-size: 3in; font-family: Garamond; font-style: italic; } </STYLE> </HEAD> <BODY BGCOLOR=WHITE> <P ALIGN=CENTER> Quack </BODY> </HTML>
Figure 5 quack.html: Quack in three-inch letters.
Because most Web-based graphics are a fixed number of pixels wide and a fixed number of pixels high, we recommend that you use pixel measurements most of the time. That way, you can always be sure that your page will look more-or-less uniform, relative to the size of the graphics on the page.