Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > CSS

  • Print
  • + Share This
This chapter is from the book

Text Properties

Now that you’ve looked at how to style font properties, it’s time to look at how to style text properties. If you want to indent a paragraph, create a superscript such as the 6 in 106, create more space between each letter of a headline, and many other type formatting tasks, you will use the CSS text properties.

Here are the most useful text-related CSS properties:

  • text-indent
  • letter-spacing
  • word-spacing
  • text-decoration
  • text-align
  • line-height
  • text-transform
  • vertical-align

Text-Indent Property

Values: any length value (positive or negative)

Example: p {text-indent:3em;}

This property sets the start position of the text box in relation to the containing element. By default, that is the top-left corner of the container.

If you set a positive value to the text-indent, then the text moves to the right, creating an indented paragraph (Figure 4.5, example 1).

Figure 4.5

Figure 4.5. These four examples illustrate the text-indent property.

However, if you set a negative value for text-indent, the first line hangs out to the left of the containing element, so make sure that there is a place for it to go. If there’s an element to the left, the hanging text can overlap it, or if it’s close to the edge of the browser window, it is clipped (Figure 4.5, example 2). The way to avoid this problem is always to specify a positive left margin value greater than the specified negative indent. In Figure 4.5 example 2, the negative indent is –1.5 ems, but in Figure 4.5, example 3, there is also a left margin value of 2 ems. Here is how this is written.

Indents can help give text a professionally-styled look and also give the reader clear visual entry points into the text blocks. Remember to set indents and related margins in ems, as I have done here, so that the indent remains proportional to the line length if the user (or you) changes the font size.

Letter-Spacing Property

Values: any length values (positive or negative)

Example: p {letter-spacing:.2em;}

Positive letter-spacing values increase the overall space between letters, while negative values decrease it. Always use relative values such as ems for letter spacing, even if you are setting the font size in pixels, so that the spacing remains proportional if the font size changes. Examples are shown in Figure 4.7. letter-spacing controls tracking, which is the typographical term for the letter spacing applied to all characters in a block of text. This contrasts with kerning, which is the term for adjusting the space between two specific characters.

The default letter spacing of a font appears looser as the text gets larger, so tightening the letter spacing of a headline adds refinement to your Web page. Note the text and headline I tightened in Figure 4.7 only have .05 em (a twentieth of an em) of letter spacing removed from between each character; much more and the letters would start to merge into each other.

Figure 4.7

Figure 4.7. You can see how changing the letter-spacing value changes the look of your text.

Word-Spacing Property

Values: any length values (positive or negative)

Example: p {word-spacing:.2em;}

Word spacing is very similar to letter spacing except, as you might imagine, the space changes between each word rather than between each letter. CSS treats any character or group of characters with white space around them as a word. Second, even more than letter spacing, word spacing can easily be overdone and result in some very hard-to-read text (Figure 4.8).

Figure 4.8

Figure 4.8. Paragraphs and headlines with normal, negative, and positive word spacing.

Text-Decoration Property

Values: underline, overline, line-through, blink, none

Example: .retailprice {text-decoration:line-through;}

These values, with the exception of blink, are displayed in Figure 4.9. blink, which makes text flash on and off, is truly annoying, and should be used sparingly, or better yet, not at all.

Figure 4.9

Figure 4.9. These are the various text-decoration values, but the most useful application is the control of underlining on links.

The primary application of text decoration is controlling the underlining of links. Here’s an example that removes the underlining of links in a navigation bar, where the text is obviously clickable and underlining is just clutter, but adds it back when the user rolls over a link, providing a little tactile feedback.


Text-Align Property

Values: left, right, center, justify

Example: p {text-align:right;}

There are only four values for this property: left, right, center, and justify. The text aligns horizontally within the element. Note that center will also center a smaller fixed-width element or image horizontally within a larger element. Figure 4.10 shows the four possible text-align values in action.

Figure 4.10

Figure 4.10. The four text-align values.

Line-Height Property

Values: any numerical value (value type is optional)

Example: p {line-height:1.5;}

line-height is the CSS equivalent of leading (pronounced like the metal) in the world of print. Leading creates space between the lines of a block of text.

Line height is distributed above and below the text. For example, if you have a font size of 12 pixels and you set the line height to 20 pixels, the browser adds 4 pixels of space above and 4 pixels of space below to achieve the 20 pixel line height.

On a single line of text like a headline, line-height acts like another margin, and large headlines (h1 and h2, for example) have a significant amount of default line height. This is worth remembering, because sometimes you will find that even after removing margins and padding, you still can’t eliminate all the space above and below a headline. To do this you need to reduce the line height also, sometimes to a height less than that of the text, i.e., less than 1.

As shown in Figure 4.11, the simplest way to change this default line height is to use the font shorthand property and write a compound value for both font-size and line-height in one. For example:

Figure 4.11

Figure 4.11. A variation of the standard line height is a simple way to give a distinctive look to your pages.

In this case, the leading is 1.4 times the font size of 1.2 ems. Note that you don’t need any units, such as ems or pixels, specified for the line-height part of the value, just a number. In this case, CSS simply takes the calculated size of whatever number of onscreen pixels 1.2 ems works out to be and multiplies it by 1.4 to arrive at the line height. If you later increase the font size to 1.5 ems, the line height (leading) is still 1.4 times the calculated amount of 1.5 ems. Note if you specify a line height in a fixed unit, such as pixels, and you increase the font size, then the lines of text may start to overlap one another.

Text-Transform Property

Values: none, uppercase, lowercase, capitalize

Example: p {text-transform:capitalize;}

text-transform changes the capitalization of text within an element. You can force a line of text to have initial letters capitalized, all text uppercase, or all text lowercase. Figure 4.12 illustrates these options.

Figure 4.12

Figure 4.12. text-transform lets you add newspaper-style headline formatting to text.

capitalize capitalizes the first letter of every word. This emulates the style of many headlines in ads, newspapers, and magazines, except that a human applying such styling tends to leave the capitalization off minor words such as “of,” “as,” and “and,” as in “Tom and Jerry Go to Vegas.” CSS capitalization simply produces “Tom And Jerry Go To Vegas.”

Vertical-Align Property

Values: any length value, sub, sup, top, middle, bottom

Example: span {vertical-align:60%;}

vertical-align moves text up or down with respect to the baseline, but note that it only affects inline elements. If you want to vertically align a block-level element, you must also set its display property to inline. One of the most common uses is for superscript and subscript numbers in formulas and mathematical expressions, such as x4–y-5 or N3O. It’s also the correct way to style asterisks and other markers within text to indicate footnotes. I don’t like the way most browsers style sub- and superscripts by default—the font size is too large and too high (or low, for subscript) for my liking. A little styling can render better, and more consistent cross-browser, proportions.

Here’s the HTML for this example

and the CSS

While the HTML tags sup and sub create superscript or subscript text automatically, it’s worth using vertical-align and font-size in combination to produce a more pleasing result (Figure 4.13). This covers the font and text properties of CSS. Now let’s look at how fonts can be downloaded into your Web pages.

Figure 4.13

Figure 4.13. Superscripting and subscripting vary the vertical position and size of text.

  • + Share This
  • 🔖 Save To Your Account