Setting font and text attributes

Font properties

Font families

To choose a font family for a Web page element use the style

font-family:font1, font2, . . .

where font1, font2, . . . are generic or specific values. Generic font names must be serif, sans-serif, monospace, cursive, or fantasy.

Here is how the generic fonts look installed on your computer look:

Generic names

This is SERIF serif

This is SANS-SERIF sans-serif

This is MONOSPACE monospace

This is CURSIVE cursive

This is FANTASY fantasy

Specific fonts can be listed being separated by commas. The browser will scan these fonts in order and use whatever font is available.

Example:

<p style="font-family:Arial, Helvetica, sans-serif">
this is a test paragraph</p>

this is a test paragraph

Font size

To specify a font size for an element, use the style

font-size: size

The size can be measured in absolute units:

Absolute unitsComments
font-size : 0.5in
font-size : 37pt
font-size : 3pc
font-size : 16px
size in inches
points (1 inch = 72 points)
picas  (1 pica = 12 points)
pixels (size depends on screen resolution)

. . . or in relative units:

Relative unitsComments
font-size : 3em
font-size : 2ex
font-size : 150%
em unit (width of letter M)
ex unit (height of letter x)
percentage of the current font size

. . . or absolute-size key words:

Relative unitsComments
font-size : xx-small
font-size : x-small
font-size : small
font-size : medium
font-size : large
font-size : x-large
font-size : xx-large
xx-small
x-small
small
medium
large
x-large
xx-large

The font size increases in a factor of 1.5 by default. Setup for the default font size for the body is small (it is medium in most IE browsers).

. . . or relative-size key words:

smaller larger

Each application of these key words moves the font size to the next category in order (if it exists).

Font style and weight

These style elements specify the appearance of your font and its weight:

font-style : normal or italic or oblique
font-weight : weight

where weight is either a value from 100 (lightest) to 900 (heaviest) in intervals of 100, or one of the keywords normal, bold, lighter, or heavier.

Example (styling the <p> tag):

font-style : normalfont-style : italic font-style : oblique

font style

font style

font style

font-weight : 100font-weight : 200 font-weight : 300font-weight : 400 font-weight : 500font-weight : 600 font-weight : 700font-weight : 800font-weight : 900

font weight

font weight

font weight

font weight

font weight

font weight

font weight

font weight

font weight


Text properties

Specifying word, letter, and line spacing

These style elements specify the horizontal space between individual letters and words, and vertical space between lines of text. To set these values use the following style:

letter-spacing : size
word-spacing : size
line-height : size

Example (styling the <p> tag):

line-height : 3exletter-spacing : 2em

line
spacing

letter spacing

Text alignment

To align text horizontally, use the text-align attribute:

text-align : left or center or right

Example (styling the <td> tag):

text-align:centertext-align:right
text align text align

To align text vertically, use the vertical-align attribute:

vertical-align : alignment

The following table lists several examples for various values of the alignment attribute of the aligned text within the <i> tag:

AlignmentResult
vertical-align : baseline Text aligned text
vertical-align : middle Text aligned text
vertical-align : super Text aligned text
vertical-align : sub Text aligned text
vertical-align : bottom Text aligned text
vertical-align : text-bottom Text aligned text
vertical-align : top Text aligned text
vertical-align : text-top Text aligned text

Text decoration and transformation

These text properties are supported by the following CSS properties:

text-decoration : none or underline or overline or line-through

Example:

StyleResult
text-decoration : none

no decoration

text-decoration : underline

underline

text-decoration : overline

overline

text-decoration : line-through

line-through

The next set of properties support the transformation that will be applied to the text characters:

text-transform : capitalize or uppercase or lowercase

Examples:

StyleResult
<p style="text-transform:capitalize">capitalize first letter of each word</p>

capitalize first letter of each word

<p style="text-transform:uppercase">display paragraph in upper-case</p>

display paragraph in upper-case

<p style="text-transform:lowercase">DISPLAY ALL IN LOWER-CASE</p>

DISPLAY ALL IN LOWER-CASE