Chapter 5. Text Manipulation

In Chapter 4, you learned how certain properties in CSS are inherited and how the cascade determines which style rules are the most important. In this and subsequent chapters, I begin an in-depth look at the individual properties of CSS and how these come together to style a document.

In this chapter, I look specifically at properties that manipulate the presentation of text. You can manipulate text in a variety of ways, from the length of space between letters in words of text, to the length of space between the words of a sentence, to the spacing between sentences in a paragraph, to how much space is used to indent the text contained in a paragraph.

I cover the various CSS text-manipulation properties:

  • The letter-spacing property and how it is used to add or subtract space between the letters that make up a word

  • The word-spacing property and how it is used to add or subtract space between the words of a sentence

  • The text-indent property and how it is used to indent the text of a paragraph

  • The text-align property and how it is used to align the text of a document

  • The text-decoration property and how it is used to underline, overline, and strikethrough text

  • The text-transform property and how it is used to capitalize text or convert text to uppercase or lowercase letters

  • The white-space property and how it is used to control the flow and formatting of text

The text manipulation properties of CSS allow you to design the layout of a document in much the same way as you use a word processing application.

The letter-spacing Property

The letter-spacing property, as I have demonstrated briefly in previous chapters, controls the amount of space between the letters. The following table shows its allowable values.

Property

Value

letter-spacing

normal | <length>

Initial value: normal

The letter-spacing property is a simple property that accepts a length as its value. A<length> value is any length value supported by CSS, as I discussed in Chapter 2. A normal value is the default value, and is determined by the font that's being used. This value is equal to a zero length value.

Figure 5-1a shows an example of the letter-spacing property.

Figure 5-1a

Figure 5-1a. Figure 5-1a

In Figure 5-1a, you see how the letter-spacing property would be specified; Figure 5-1b shows the corresponding markup.

Figure 5-1c shows the rendered output of the CSS in Figure 5-1a and the markup in Figure 5-1b in the Safari browser.

The letter-spacing property may have either a positive or negative value. When given a negative value, letters are rendered closer together. Figure 5-2a shows an example of this.

Figure 5-1b

Figure 5-1b. Figure 5-1b

Figure 5-1c

Figure 5-1c. Figure 5-1c

Figure 5-2a

Figure 5-2a. Figure 5-2a

The CSS in Figure 5-2a is combined with the markup in Figure 5-2b.

Figure 5-2b

Figure 5-2b. Figure 5-2b

Figure 5-2c shows the rendered output of Figures 5-2a and 5-2b.

Figure 5-2c

Figure 5-2c. Figure 5-2c

As you can see in Figure 5-2c, the letters of the paragraph are condensed together because the value of the letter-spacing property is a negative value.

You can use the letter-spacing property to add or subtract space between letters. In the following example, you try the letter-spacing property out for yourself.

In the next section, I present a property similar to the letter-spacing property, the word-spacing property.

The word-spacing Property

The word-spacing property, in essence, functions identically to the letter-spacing property. However, (of course) instead of controlling the space between letters, the word-spacing property controls the space between words. The following table shows its allowable values.

Property

Value

word-spacing

normal | <length>

Initial value: normal

To demonstrate the effect of the word-spacing property, consider the style sheet rule in Figure 5-4a.

Figure 5-4a

Figure 5-4a. Figure 5-4a

The style sheet in Figure 5-4a is coupled with the markup in Figure 5-4b.

Figure 5-4b

Figure 5-4b. Figure 5-4b

Figure 5-4a and Figure 5-4b together result in the output shown in Figure 5-4c; 25 pixels of space now separate each word of the <h4> element.

Figure 5-4c

Figure 5-4c. Figure 5-4c

Additionally, like the letter-spacing property, the word-spacing property can contain a negative value. If given a negative value, the effects are less space between each word. This is demonstrated in CSS in Figure 5-5a.

Figure 5-5a

Figure 5-5a. Figure 5-5a

Again, the CSS in Figure 5-5a is combined with the markup in Figure 5-5b.

Figure 5-5b

Figure 5-5b. Figure 5-5b

The CSS in Figure 5-5a and the markup in Figure 5-5b result in the output depicted in Figure 5-5c.

Figure 5-5c

Figure 5-5c. Figure 5-5c

As you did with the letter-spacing property in Example 5-1, in the following Try It Out you experiment with the word-spacing property for yourself.

Now that you have seen how to control the space between letters and words, the next section describes how to indent text within a paragraph.

Indenting Paragraph Text Using text-indent

Indenting text in CSS is done using the text-indent property. The text-indent property identifies the first line of text of a paragraph and inserts the specified length before the first line of text, thus indenting the text. The following table shows this property's allowed values.

Property

Value

text-indent

<length> | <percentage>

 

Initial value: 0

The text-indent property accepts either a normal length value or a percentage value. Figure 5-7a demonstrates the text-indent property with a normal length value in pixels applied.

Figure 5-7a

Figure 5-7a. Figure 5-7a

Figure 5-7a is combined with the markup in Figure 5-7b.

Figure 5-7b

Figure 5-7b. Figure 5-7b

Figure 5-7c shows the result of the preceding rule and markup.

Figure 5-7c

Figure 5-7c. Figure 5-7c

Figure 5-7 demonstrates the most common use of the text-indent property, with a normal length value, used to indent the text of the target element. The text-indent property can also accept a percentage width. This is demonstrated in the rule in Figure 5-8a.

Figure 5-8a is combined with the markup from Figure 5-7b, to get the output you see in Figure 5-8b.

The percentage width assigned by the text-indent property depends on the width of the <p> element's parent element. In this example, the parent element is the <body> element. By default, the <body> element's width expands horizontally, filling the entire browser window.

Figure 5-8a

Figure 5-8a. Figure 5-8a

Figure 5-8b

Figure 5-8b. Figure 5-8b

For instance, if the <p> element were to be assigned a fixed width of 200 pixels, since the indentation for the <p> element is based on the width of the <body> element, which is more than 200 pixels, let's say for this example it's 800 pixels wide. Given a 10% indention, the indention of the first line of the <p> element would be 80 pixels, rather than 20 pixels, since 10% of 800 is 80.

Like the letter-spacing and word-spacing properties, the text-indent property can also accept a negative value. Figure 5-9a shows an example of the text-indent property with a negative value.

Figure 5-9a

Figure 5-9a. Figure 5-9a

The CSS rule in Figure 5-9a is combined with the markup from Figure 5-7b. Safari (or your browser of choice) gives you the output in Figure 5-9b.

Figure 5-9b shows that the text is shifted the other way.

Figure 5-9b

Figure 5-9b. Figure 5-9b

Now that you've seen some examples of the text-indent property, in Example 5-3, you experiment with it for yourself.

In the next section, I discuss the text-align property.

Aligning Text with the text-align Property

The purpose of the text-align property is simple: It aligns text! The following table outlines each of the possible values for the text-align property.

Property

Value

text-align

left | right | center | justify

Initial value: left

The text-align property should be fairly straightforward and obvious in its purpose. Figures 5-11a, 5-11b, and 5-11c demonstrate what the different keyword values of the text-align property do.

Figure 5-11a

Figure 5-11a. Figure 5-11a

The CSS in Figure 5-11a is combined with the markup in Figure 5-11b.

Figure 5-11b

Figure 5-11b. Figure 5-11b

The CSS and markup from Figures 5-11a and 5-11b result in the output observed in Figure 5-11c.

In Figure 5-11c, there are no surprises; left aligns text left, right, to the right, and center to the middle. You may not, however, be familiar with the justify keyword. In Figure 5-11c, you can see that the text is lined up on the left and on the right; spacing between words on the line is adjusted automatically so that both the beginning and the end of each line are lined up. To put this in perspective, Figure 5-12 shows the same code, but with the text-align: justify; declaration removed.

Figure 5-11c

Figure 5-11c. Figure 5-11c

Figure 5-12

Figure 5-12. Figure 5-12

In Figure 5-12 you can see that the ends of each line are no longer lined up.

The text-decoration Property

The text-decoration property applies underlining, overlining, and strikethrough to text. The following table outlines the text-decoration property and the values it allows.

Property

Value

text-decoration

none | [ underline | | overline | | line-through | | blink

Initial value: none

Safari and IE do not support the blink keyword.

Because this property is a little more complicated than those covered previously, a simple explanation of its use is warranted.

To demonstrate the various styles available using this property, consider the example in Figure 5-13a.

The CSS in Figure 5-13a is combined with the markup in Figure 5-13b.

Figure 5-13c shows the various effects provided by the text-decoration property as specified by the preceding code.

However, this is not all that is possible with the text-decoration property. This notation

[ underline || overline || line-through || blink ]

means that the text-decoration property can accept one or more of these values. To specify more than one value, each value is separated by a single space. Take for example the code in Figure 5-14a.

Figure 5-13a

Figure 5-13a. Figure 5-13a

Figure 5-13b

Figure 5-13b. Figure 5-13b

Figure 5-13c

Figure 5-13c. Figure 5-13c

Figure 5-14a

Figure 5-14a. Figure 5-14a

The CSS in Figure 5-14a is combined with the markup in Figure 5-14b.

Figure 5-14b

Figure 5-14b. Figure 5-14b

The code in Figures 5-14a and 5-14b result in the output shown in Figure 5-14c.

Figure 5-14c

Figure 5-14c. Figure 5-14c

The notation for the text-decoration property indicates that it can accept up to four values. Those values can be any combination of underline, overline, line-through, and blink. The values none or inherit can be used instead of any of those four values; so if either the value none or inherit is used, only that lone value may appear.

In the next section, I discuss the text-transform property, which allows you to control the case of text via CSS.

The text-transform Property

The text-transform property exists purely to manipulate the case of text, for instance, to capitalize or make all characters uppercase or all characters lowercase. The following table shows the text-transform property and its possible values.

Property

Value

text-transform

capitalize | uppercase | lowercase| none

Initial value: none

Consider the CSS in Figure 5-16a.

Figure 5-16a

Figure 5-16a. Figure 5-16a

This CSS is combined with the markup in Figure 5-16b.

Figure 5-16b

Figure 5-16b. Figure 5-16b

Figure 5-16c shows that the text-transform property overrides the case of the text, no matter how it appears in the source code.

Figure 5-16c

Figure 5-16c. Figure 5-16c

In the first paragraph, even though in the source the sentence appears in all lowercase, if you apply the text-transform: capitalize; declaration, each word of the sentence is capitalized. Likewise, in the next paragraph, even though the source code contains all lowercase letters, with the addition of the text-transform: uppercase; declaration, each word of the sentence appears in all uppercase letters in the rendered output. In the last paragraph, each word appears in uppercase in the markup source code, but with the addition of the text-transform: lowercase; declaration, each word of the sentence appears in all lowercase in the actual output rendered by the browser.

Now that you've seen an example of what the text-transform property does, in the following example you try out the text-transform property for yourself.

In the next section, I present CSS's white-space property, which controls whether or not spaces and line breaks in the source code are recognized, and whether or not text wraps automatically.

The white-space Property

The white-space property allows you to control text formatting in the source code of the web document. The following table outlines the possible keyword values of the white-space property as of CSS 2.

Property

Value

white-space

normal | pre | nowrap

 

Initial value: normal

IE 6 and IE 7 support white-space: pre; only in standards rendering mode. For more information on rendering modes, see Chapter 7, "The Box Model."

Figure 5-18a is an example of the white-space: pre; declaration.

I've specified a monospace font for clarity. The CSS in Figure 5-18a is combined with the markup in Figure 5-18b.

Figure 5-18a

Figure 5-18a. Figure 5-18a

Figure 5-18b

Figure 5-18b. Figure 5-18b

The result looks like Figure 5-18c.

Figure 5-18c

Figure 5-18c. Figure 5-18c

In the source code for the output shown in Figure 5-18c, I've added spaces before each line and line breaks. With the white-space: pre; declaration, those spaces and line breaks are preserved in the browser's rendered output.

By default, the browser will collapse the extra spaces between words and ignore the line breaks, which is the behavior of the white-space: normal; declaration. The white-space: pre; declaration preserves that extra space and keeps the line breaks where they appear in the source code. Under normal circumstances, if there is too much text to appear on a single line, the extra text overflows onto the following line or lines. The white-space: nowrap; declaration prevents that overflow from happening and forces the text to stay on one line, unless an HTML line break <br /> element is encountered. That forces a line break. Figure 5-19a is an example of this.

Figure 5-19a

Figure 5-19a. Figure 5-19a

The CSS in Figure 5-19a is combined with the markup in Figure 5-19b.

Figure 5-19c shows that the text has flowed off the screen to the right because there is more text than can fit on the screen.

Compare the output in Figures 5-18c and 5-19c to that in Figure 5-20 where no white-space property is applied. That is, applying the white-space: normal; declaration is the same as applying no white-space property, because normal is the initial value of the white-space property.

Figure 5-19b

Figure 5-19b. Figure 5-19b

Figure 5-19c

Figure 5-19c. Figure 5-19c

Figure 5-20

Figure 5-20. Figure 5-20

Now that you've had an overview of what the white-space property is, the following Try It Out gives you an opportunity to test the white-space property for yourself.

Summary

In this chapter, I discussed a variety of CSS text-manipulation properties, which include the following:

  • The letter-spacing property, which is used to specify the length of space between letters

  • The word-spacing property, which is used to specify the length of space between words

  • The text-indent property, which is used to indent text

  • The text-align property, which is used to align the text of a document

  • The text-decoration property, which is used to apply decorative styling to text, such as underlining, overlining, strikethrough, or blinking text

  • The text-transform property, which is used to control the case of text regardless of what case is used in the document's source code

  • The white-space property, which is used to control text formatting as it relates to how the text appears in the document's source code

Chapter 6 continues along the same vein of text manipulation, with a discussion of the font properties in CSS.

Exercises

  1. If you wanted to reduce the spacing between letters, how would it be done? Provide an example declaration.

  2. How would you produce the output you see in Figure 5-22? Provide the declaration.

    Figure 5-22

    Figure 5-22. Figure 5-22

  3. When indenting text in a paragraph, how is a percentage value calculated?

  4. What are the keywords that CSS offers for changing the case of text within an element?

  5. If you wanted to preserve line breaks and spacing as formatted in the source code, what would the CSS declaration be?

  6. What browsers do not support the annoying blink keyword?

  7. If you wanted to put a line over a section of text, rather than underlining it, what property and keyword would you use?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.45.137