CSS Text

Formatting Text With CSS

Formatting and adding style to text is a key issue for any web designer. In this lesson you will be introduced to the amazing opportunities CSS gives you to add layout to text. The following properties will be explained:

text-indent

text-align

text-decoration

letter-spacing

text-transform

text-indent

The property text-indent allows you to add an elegant touch to text paragraphs by applying an indent to the first line of the paragraph. In the example below a 30px is applied to all text paragraphs marked with <p>:

The above CSS code will result in all paragraphs being effected like this in the browser...

This is a paragraph of text which has been indented. This is a paragraph of text which has been indented.

text-align

The CSS property text-align corresponds to the attribute align used in old versions of HTML. Text can either be aligned to the left, to the right or centred or you can justify the text, which will stretch each line so that both the right and left margins are straight. In the examples below the text in table headings <th> is aligned to the right while the table data <td> are centred. In addition, normal text paragraphs are justified.

text-decoration

The property text-decoration makes it is possible to add different "decorations" to text.

For example, you can underline the text, have a line through or above the text.

In the following example, <h1> are underlined headlines, <h2> are headlines with a line above the text and <h3> are headlines with a line though the text.

letter-spacing

The spacing between text characters can be specified using the property letter-spacing. The value of the property is simply the desired width. For example, if you want a spacing of 3px between the letters in a text paragraph <p> and 6px between letters in headlines <h1> the code below could be used.

text-transform

The text-transform property controls the capitalization of a text. You can choose to capitalize, use uppercase or lowercase regardless of how the original text is looks in the HTML code. There are four possible values for text-transform...

Capitalize: Capitalizes the first letter of each word. For example: "john doe" will be "John Doe".

Uppercase: Converts all letters to uppercase. For example: "john doe" will be "JOHN DOE"

Lowercase: Converts all letters to lowercase. For example: "JOHN DOE" will be "john doe"

None: No transformations - the text is presented as it appears in the HTML code.

As an example, we will use a list of names. The names are all marked with <li> (list-item). Let's say that we want names to be capitalized and headlines to be presented in uppercase letters.