CSS Text Formatting


CSS offers many options to style text.


text formatting

This text looks a certain way because of some special text rules. The title is centered, changed to uppercase, and given a color.The paragraph is shifted to the right, centered, and the gap between letters is set. The colored line under the text is taken away "Try it Yourself" link.


Text Color

The color property changes the text's color. You can specify the color using this property.

  • a color name - like "red"
  • a HEX value - like "#ff0000"
  • an RGB value - like "rgb(255,0,0)"

Example

body {
  color: blue;
}

h1 {
  color: green;
}
Try it Yourself »

Text Color and Background Color

In this example given below, we have used both the background-color property and the color property:

Example

body {
  background-color: lightgrey;
  color: blue;
}

h1 {
  background-color: black;
  color: white;
}

div {
  background-color: blue;
  color: white;
}
Try it Yourself »

Text Alignment and Text Direction

In this section, you'll discover information about these Properties:

  • text-align
  • text-align-last
  • direction
  • unicode-bidi
  • vertical-align

Text Alignment

The text-align property is used to decide how text should be positioned horizontally.

You can position text in different ways: on the left, right, in the center, or evenly spaced on both sides.

In this example, you can see text that is aligned in the center, to the left, and to the right. The default alignment is to the left when the text flows from left to right, and it's to the right when the text flows from right to left

Example

h1 {
  text-align: center;
}

h2 {
  text-align: left;
}

h3 {
  text-align: right;
}
Try it Yourself »

When you set the text-align property to "justify," it makes every line in a text stretch to have the same width. This creates straight margins on the left and right, similar to how text appears in magazines and newspapers.

Example

div {
  text-align: justify;
}
Try it Yourself »

Text Align Last

The text-align-last property tells us how to position the final line of a piece of text.

Example

p.a {
  text-align-last: right;
}

p.b {
  text-align-last: center;
}

p.c {
  text-align-last: justify;
}
Try it Yourself »

Text Direction

You can use the direction and unicode-bidi properties to alter the way text appears in an element. These properties control the text's directionality

Example

p {
  direction: rtl;
  unicode-bidi: bidi-override;
}
Try it Yourself »

Vertical Alignment

The vertical-align property controls how an element is positioned vertically.

Example

img.a {
  vertical-align: baseline;
}

img.b {
  vertical-align: text-top;
}

img.c {
  vertical-align: text-bottom;
}

img.d {
  vertical-align: sub;
}

img.e {
  vertical-align: super;
}
Try it Yourself »

Text Decoration

In this section, you'll discover information about these characteristics:

  • text-decoration-line
  • text-decoration-color
  • text-decoration-style
  • text-decoration-thickness
  • text-decoration

Add a Decoration Line to Text

The text-decoration-line property is used to include a line decoration in text.

Tip: You can use multiple values, such as 'overline' and 'underline,' to show lines both above and below a piece of text.

Example

h1 {
  text-decoration-line: overline;
}

h2 {
  text-decoration-line: line-through;
}

h3 {
  text-decoration-line: underline;
}

p {
  text-decoration-line: overline underline;
}
Try it Yourself »

Specify a Color for the Decoration Line

The text-decoration-color property changes the color of the decoration line.

Example

h1 {
  text-decoration-line: overline;
  text-decoration-color: red;
}

h2 {
  text-decoration-line: line-through;
  text-decoration-color: blue;
}

h3 {
  text-decoration-line: underline;
  text-decoration-color: green;
}

p {
  text-decoration-line: overline underline;
  text-decoration-color: purple;
}
Try it Yourself »

Specify a Style for the Decoration Line

The text-decoration-style property is employed to determine how the decorative line appears.

Example

h1 {
  text-decoration-line: underline;
  text-decoration-style: solid;
}

h2 {
  text-decoration-line: underline;
  text-decoration-style: double;
}

h3 {
  text-decoration-line: underline;
  text-decoration-style: dotted;
}

p.ex1 {
  text-decoration-line: underline;
  text-decoration-style: dashed;
}

p.ex2 {
  text-decoration-line: underline;
  text-decoration-style: wavy;
}

p.ex3 {
  text-decoration-line: underline;
  text-decoration-color: red;
  text-decoration-style: wavy;
}
Try it Yourself »

Specify the Thickness for the Decoration Line

The text-decoration-thickness property is used to control how thick the line for decoration appears.

Example

h1 {
  text-decoration-line: underline;
  text-decoration-thickness: auto;
}

h2 {
  text-decoration-line: underline;
  text-decoration-thickness: 5px;
}

h3 {
  text-decoration-line: underline;
  text-decoration-thickness: 25%;
}

p {
  text-decoration-line: underline;
  text-decoration-color: red;
  text-decoration-style: double;
  text-decoration-thickness: 5px;
}
Try it Yourself »

The Shorthand Property

The text-decoration property is a shorthand property for:

  • text-decoration-line (required)
  • text-decoration-color (optional)
  • text-decoration-style (optional)
  • text-decoration-thickness (optional)

Example

h1 {
  text-decoration: underline;
}

h2 {
  text-decoration: underline red;
}

h3 {
  text-decoration: underline red double;
}

p {
  text-decoration: underline red double 5px;
}
Try it Yourself »

A Small Tip

In HTML, links usually have lines under them. But sometimes, you might notice links without these lines. To make links without lines, we use the code text-decoration: none;. It looks like this:

Example

a {
  text-decoration: none;
}
Try it Yourself »

Text Transformation

The text-transform property is used to control whether text should appear in uppercase (capital letters) or lowercase (small letters).

You can use this to change text in different ways:

  • Make everything UPPERCASE.
  • Make everything lowercase.
  • Capitalize the first letter of each word.

Example

p.uppercase {
  text-transform: uppercase;
}

p.lowercase {
  text-transform: lowercase;
}

p.capitalize {
  text-transform: capitalize;
}
Try it Yourself »

Text Spacing

In this section, you'll find information about these characteristics:

  • text-indent
  • letter-spacing
  • line-height
  • word-spacing
  • white-space

Text Indentation

The text-indent property helps decide how much the first line of a text should be moved in from the left margin.

Example

p {
  text-indent: 50px;
}
Try it Yourself »

Letter Spacing

The letter-spacing property helps you control how much space is between the letters in a piece of text.

The given example shows how to increase or decrease the space between characters:

Example

h1 {
  letter-spacing: 5px;
}

h2 {
  letter-spacing: -2px;
}
Try it Yourself »

Line Height

The line-height property is used to determine how much space there should be between lines of text.

Example

p.small {
  line-height: 0.8;
}

p.big {
  line-height: 1.8;
}
Try it Yourself »

Word Spacing

The word-spacing property is used to set the gap between words in a text.

In this example, we show how to make the gap between words either bigger or smaller.

Example

p.one {
  word-spacing: 10px;
}

p.two {
  word-spacing: -2px;
}
Try it Yourself »

White Space

The white-space property tells us what happens to spaces and empty areas inside an element.

This example shows how to stop text from wrapping inside an element.

Example

p {
  white-space: nowrap;
}
Try it Yourself »