CSS Text

In addition to font properties, there is a separate set of properties provided by CSS that can help you style your web page's text. In this chapter, you will get to know about various text properties of CSS.

CSS Text Properties

It is possible to assign various CSS text properties in your HTML element using the CSS text properties listed below:

CSS Text PropertyDescription
colorThe color property can be applied to set the color in your text.
directionThe direction property can be applied to set the direction of your text.
letter-spacingThe letter-spacing property can be applied to add or subtract space between letters, which will form a word.
text-decorationThe text-decoration property can be applied for underlining, overlining, and strikethrough text.
text-shadowThe text-shadow property can be applied for setting the text-shadow on your text.
text-transformThe text-transform property can be applied to capitalize text or convert text to uppercase or lowercase letters.
word-spacingThe word-spacing property can be applied to add or subtract space between different words in your sentence.
text-indentThe text-indent property can be applied to indent the text of your paragraph.
text-alignThe text-align property can be applied to align text in your document.
white-spaceThe white-space property can be applied to control flow, as well as to the formatting of your text.

Let us discuss these in detail and see how to implement them and how it looks in the browser.

CSS Text Color

The color property of CSS is implemented for assigning colors to your texts. Three different approaches can specify these colors:

  • By using the color name, such as red, aqua, blue
  • By using the HEX value, such as #ff0000, #ffff00
  • By using the RGB value, such as rgb(255,0,0), rgb(255,255,0)

Here is a code snippet of how to implement:

Example:
<style>
p {
    color: red;
}

body {
    color: aqua;
}
</style>

CSS Text Alignment

This property is implemented to specify a horizontal alignment in your text. Text alignment can be of these four values.

  1. left
  2. right
  3. center or
  4. justify
Example:
<style>
p {
    text-align:left;
}

body {
    text-align: center;
}

h2 {
    text-align: right;
}
</style>

It is to be noted that when the text-align property is assigned with a justify value, the line is stretched to fit the page where the right and the left margins become straight.

CSS Text Decoration

This property of CSS is implemented to add or remove decorations from your text. When the text-decoration value is set to none, it is often employed for removing underlines from links, which looks something like this:

Example:
<style>
a {
    text-decoration: none;
}
</style>

Other code snippets are:

Example:
<style>
h2 {
    text-decoration: line-through;
}

p {
    text-decoration: overline;
}

body {
    text-decoration: underline;
}
</style>

CSS Text Transformation

This CSS property is implemented for specifying the uppercase as well as the lowercase letters of your text. The transformation can be either converting every text to lowercase or upper case or capitalize the starting letter of every word.

Example:
<style>
h2.uppercase {
    text-transform: uppercase;
}

h3.lowercase {
    text-transform: lowercase;
}

h4.capitalize {
    text-transform: capitalize;
}
</style>

CSS Text Indentation

The text-indent property of CSS is implemented to specify the indentation of the initial line of your text.

Example:
<style>
p {
    text-indent: 40px;
}
</style>

CSS Line Height

This CSS line-height property is applied to assign space between lines:

Example:
<style>
p.small {
    line-height: 0.6;
}

p.big {
    line-height: 1.5;
}
</style>

CSS Letter and word Spacing

The letter-spacing property is implemented for specifying the space involving the characters within your HTML text. And the word-spacing property is implemented for specifying the space involving the words between your texts.

Example:
<style>
h2 {
    letter-spacing: 2px;
}

h3 {
    letter-spacing:3px;
}

h1 {
    word-spacing: 10px;
}
</style>

CSS Text Shadow

The text-shadow property of CSS allows including shadow to your text. Here is a code snippet:

Example:
<style>
h2 {
    text-shadow: 2px 1px gray;
}
</style>

Here, in the example above, the first position value designates the horizontal shadow (2px), and the second parameter value shows the vertical shadow (1px). The last value designates the color of your shadow. Note that space separates each of these values.


Scroll Back to Top