16 Text

Contents

The properties defined in the following sections affect the visual presentation of characters, spaces, words, and paragraphs.

16.1 Indentation: the 'text-indent' property

'text-indent'
Value:  <length> | <percentage> | inherit
Initial:  0
Applies to:  block-level elements
Inherited:  yes
Percentages:  refer to width of containing block
Media:  visual

This property specifies the indentation of the first line of text in a block. More precisely, it specifies the indentation of the first box that flows into the block's first line box. The box is indented with respect to the left (or right, for right-to-left layout) edge of the line box. User agents should render this indentation as blank space.

Values have the following meanings:

<length>
The indentation is a fixed length.
<percentage>
The indentation is a percentage of the containing block width.

The value of 'text-indent' may be negative, but there may be implementation-specific limits.

The following example causes a 3em text indent.

  P { text-indent: 3em }

16.2 Alignment: the 'text-align' property

'text-align'
Value:  left | right | center | justify | <string> | inherit
Initial:  depends on user agent and writing direction
Applies to:  block-level elements
Inherited:  yes
Percentages:  N/A
Media:  visual

This property describes how a block of text is aligned. Values have the following meanings:

left, right, center, and justify
Left, right, center, and double justify text, respectively.
<string>
Specifies a string on which cells in a table column will align (see the section on horizontal alignment in a column for details and an example). This value applies only to table columns.

A block of text is a stack of line boxes. In all cases except for 'justify', this property specifies how the inline boxes within each line box align with respect to the line box's left and right sides; alignment is not with respect to the viewport. In the case of 'justify', the UA may stretch the inline boxes in addition to adjusting their positions.

In this example, note that since 'text-align' is inherited, all block-level elements inside the DIV element with 'class=center' will be centered.

  DIV.center { text-align: center }

Note. The actual justification algorithm used is user-agent and written language dependent.

Conforming HTML user agents may interpret the value 'justify' as 'left' or 'right', depending on whether the element's default writing direction is left-to-right or right-to-left, respectively.

16.3 Decoration

16.3.1 Underlining, overlining, striking, and blinking: the 'text-decoration' property

'text-decoration'
Value:  none | [ underline || overline || line-through || blink ] | inherit
Initial:  none
Applies to:  all elements
Inherited:  no (see prose)
Percentages:  N/A
Media:  visual

This property describes decorations that are added to the text of an element. If the property is specified for a block-level element, it affects all inline-level descendants of the element. If it is specified for (or affects) an inline-level element, it affects all boxes generated by the element. If the element has no content or no text content (e.g., the IMG element in HTML), user agents must ignore this property.

Values have the following meanings:

none
Produces no text decoration.
underline
Each line of text is underlined.
overline
Each line of text has a line above it.
line-through
Each line of text has a line through the middle
blink
Text blinks (alternates between visible and invisible). Conforming user agents are not required to support this value.

The color(s) required for the text decoration should be derived from the 'color' property value.

This property is not inherited, but descendant boxes of a block box should be rendered with the same decoration (e.g., they should all be underlined). The color of decorations should remain the same even if descendant elements have different 'color' values.

In the following example for HTML, the text content of all A elements acting as hyperlinks will be underlined:

  A[href] { text-decoration: underline }

16.3.2 Text shadows: the 'text-shadow' property

'text-shadow'
Value:  none | [<color> || <length> <length> <length>? ,]* [<color> || <length> <length> <length>?] | inherit
Initial:  none
Applies to:  all
Inherited:  no (see prose)
Percentages:  N/A
Media:  visual

This property accepts a comma-separated list of shadow effects to be applied to the text of the element. The shadow effects are applied in the order specified and may thus overlay each other, but they will never overlay the text itself. Shadow effects do not alter the size of a box, but may extend beyond its boundaries. The stack level of the shadow effects is the same as for the element itself.

Each shadow effect must specify a shadow offset and may optionally specify a blur radius and a shadow color.

A shadow offset is specified with two <length> values that indicate the distance from the text. The first length value specifies the horizontal distance to the right of the text. A negative horizontal length value places the shadow to the left of the text. The second length value specifies the vertical distance below the text. A negative vertical length value places the shadow above the text.

A blur radius may optionally be specified after the shadow offset. The blur radius is a length value that indicates the boundaries of the blur effect. The exact algorithm for computing the blur effect is not specified.

A color value may optionally be specified before or after the length values of the shadow effect. The color value will be used as the basis for the shadow effect. If no color is specified, the value of the 'color' property will be used instead.

Text shadows may be used with the :first-letter and :first-line pseudo-elements.

The example below will set a text shadow to the right and below the element's text. Since no color has been specified, the shadow will have the same color as the element itself, and since no blur radius is specified, the text shadow will not be blurred:

  H1 { text-shadow: 0.2em 0.2em }

The next example will place a shadow to the right and below the element's text. The shadow will have a 5px blur radius and will be red.

  H2 { text-shadow: 3px 3px 5px red }

The next example specifies a list of shadow effects. The first shadow will be to the right and below the element's text and will be red with no blurring. The second shadow will overlay the first shadow effect, and it will be yellow, blurred, and placed to the left and below the text. The third shadow effect will be placed to the right and above the text. Since no shadow color is specified for the third shadow effect, the value of the element's 'color' property will be used:

  H2 { text-shadow: 3px 3px red, yellow -3px 3px 2px, 3px -3px }

Consider this example:

  SPAN.glow { 
    background: white; 
    color: white;
    text-shadow: black 0px 0px 5px;
  }

Here, the 'background' and 'color' properties have the same value and the 'text-shadow' property is used to create a "solar eclipse" effect:

Solar eclipse effect

Note. This property is not defined in CSS1. Some shadow effects may render text invisible in UAs that only support CSS1.

16.4 Letter and word spacing: the 'letter-spacing' and 'word-spacing' properties

'letter-spacing'
Value:  normal | <length> | inherit
Initial:  normal
Applies to:  all elements
Inherited:  yes
Percentages:  N/A
Media:  visual

This property specifies spacing behavior between text characters. Values have the following meanings:

normal
This value allows the user agent to alter the space between characters in order to justify text.
<length>
This value indicates inter-character space in addition to the default space between characters. Values may be negative, but there may be implementation-specific limits.

Character spacing algorithms are user agent-dependent. Character spacing may also be influenced by justification (see the 'text-align' property).

In this example, the space between characters in BLOCKQUOTE elements is increased by '0.1em'.

  BLOCKQUOTE { letter-spacing: 0.1em }

In the following example, the user agent is not permitted to alter inter-character space, nor to add space:

  BLOCKQUOTE { letter-spacing: 0cm }   /* Same as '0' */

When the resultant space between two characters is not the same as the default space, user agents should not use ligatures.

Conforming HTML user agents may consider the value of the 'letter-spacing' property to be 'normal'.

'word-spacing'
Value:  normal | <length> | inherit
Initial:  normal
Applies to:  all elements
Inherited:  yes
Percentages:  N/A
Media:  visual

This property specifies spacing behavior between words. Values have the following meanings:

normal
This value allows the user agent to alter the space between words in order to justify text.
<length>
This value indicates inter-word space in addition to the default space between words. Values may be negative, but there may be implementation-specific limits.

Word spacing algorithms are user agent-dependent. Word spacing may also be influenced by justification (see the 'text-align' property).

In this example, the word-spacing between each word in H1 elements is increased by '1em'.

  H1 { word-spacing: 1em }

Conforming HTML user agents may consider the value of the 'word-spacing' property to be 'normal'.

16.5 Case

16.5.1 Capitalization: the 'text-transform' property

'text-transform'
Value:  capitalize | uppercase | lowercase | none | inherit
Initial:  none
Applies to:  all elements
Inherited:  yes
Percentages:  N/A
Media:  visual

This property controls capitalization effects of an element's text. Values have the following meanings:

capitalize
Puts the first character of each word in uppercase.
uppercase
Puts all characters of each word in uppercase.
lowercase
Puts all characters of each word in lowercase.
none
No capitalization effects.

The actual transformation in each case is written language dependent. See RFC 2070 ([RFC2070]) for ways to find the language of an element.

Conforming HTML user agents may consider the value of 'text-transform' to be 'none' for characters that are not from the Latin-1 repertoire and for elements in languages for which the transformation is different from that specified by the case-conversion tables of ISO 10646 ([ISO10646]).

In this example, all text in an H1 element is transformed to uppercase text.

  H1 { text-transform: uppercase }

16.5.2 Special first letter/first line

Please consult the sections on first line and first letter for information on specially formatting the first letter or line of a paragraph.

16.6 White space: the 'white-space' property

'white-space'
Value:  normal | pre | nowrap | inherit
Initial:  normal
Applies to:  block-level elements
Inherited:  yes
Percentages:  N/A
Media:  visual

This property declares how whitespace inside the element is handled. Values have the following meanings:

normal
This value allows user agents to collapse sequences of white space. Line breaks may be created by characters or elements (e.g., the BR element in HTML).
pre
This value prevents user agents from collapsing sequences of white space.
nowrap
This value suppresses line breaks within text except for those created by other elements (e.g., the BR element in HTML).

The following examples show what whitespace behavior is expected from the PRE and P elements in HTML.

  PRE { white-space: pre }
  P   { white-space: normal }

Conforming HTML user agents may ignore the 'white-space' property in author and user style sheets but must specify a value for it in the default style sheet.

16.6.1 Line breaks

To force a line break with CSS2, authors may specify one with the ':before' or ':after' pseudo-elements. Thus, the BR element in HTML may be described as follows:

  BR:before { content: "\A"}

See "Generated content and automatic numbering" for explanations of the 'content' property and the use of the "\A" escape sequence.