Text Color
The color property is used to set the color of the text. The color is specified by:
a color name - like "red"
a HEX value - like "#ff0000"
an RGB value - like "rgb(255,0,0)"
The default text color for a page is defined in the body selector.
EXAMPLE:
body {
color: blue;
}
h1 {
color: green;
}
Text Color and Background Color
In this example, we define both the background-color property and the color property:
EXAMPLE:
body {
background-color: lightgrey;
color: blue;
}
h1 {
background-color: black;
color: white;
}
Text Alignment
The text-align property is used to set the horizontal alignment of a text.
A text can be left or right aligned, centered, or justified.
The following example shows center aligned, and left and right aligned text (left alignment is default if text direction is left-to-right, and right alignment is default if text direction is right-to-left):
EXAMPLE:
h1 {
text-align: center;
}
h2 {
text-align: left;
}
h3 {
text-align: right;
}
When the text-align property is set to "justify", each line is stretched so that every line has equal width, and the left and right margins are straight (like in magazines and newspapers):
EXAMPLE:
div {
text-align: justify;
}
Text Direction
The direction and unicode-bidi properties can be used to change the text direction of an element:
EXAMPLE:
p {
direction: rtl;
unicode-bidi: bidi-override;
}
Vertical Alignment
The vertical-align property sets the vertical alignment of an element.
This example demonstrates how to set the vertical alignment of an image in a text:
EXAMPLE:
img.top {
vertical-align: top;
}
img.middle {
vertical-align: middle;
}
img.bottom {
vertical-align: bottom;
}
Text Decoration
The text-decoration property is used to set or remove decorations from text.
The value text-decoration: none; is often used to remove underlines from links:
EXAMPLE:
a {
text-decoration: none;
}
The other text-decoration values are used to decorate text:
EXAMPLE:
h1 {
text-decoration: overline;
}
h2 {
text-decoration: line-through;
}
h3 {
text-decoration: underline;
}
Text Transformation
The text-transform property is used to specify uppercase and lowercase letters in a text.
It can be used to turn everything into uppercase or lowercase letters, or capitalize the first letter of each word:
EXAMPLE:
p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
p.capitalize {
text-transform: capitalize;
}
Text Indentation
The text-indent property is used to specify the indentation of the first line of a text:
EXAMPLE:
p {
text-indent: 50px;
}
Letter Spacing
The letter-spacing property is used to specify the space between the characters in a text.
The following example demonstrates how to increase or decrease the space between characters:
EXAMPLE:
h1 {
letter-spacing: 3px;
}
h2 {
letter-spacing: -3px;
}
Word Spacing
The word-spacing property is used to specify the space between the words in a text.
The following example demonstrates how to increase or decrease the space between words:
EXAMPLE:
h1 {
word-spacing: 10px;
}
h2 {
word-spacing: -5px;
}
White Space
The white-space property specifies how white-space inside an element is handled.
This example demonstrates how to disable text wrapping inside an element:
EXAMPLE:
p {
white-space: nowrap;
}
Text Shadow
The text-shadow property adds shadow to text.
In its simplest use, you only specify the horizontal shadow (2px) and the vertical shadow (2px):
EXAMPLE:
h1 {
text-shadow: 2px 2px;
}
Next, add a color (red) to the shadow:
EXAMPLE:
h1 {
text-shadow: 2px 2px red;
}
Then, add a blur effect (5px) to the shadow:
EXAMPLE:
h1 {
text-shadow: 2px 2px 5px red;
}