CSS Text

CSS has many properties to control how text looks and where it sits in its box.

PropertyUse
colorText colour
text-alignleft, right, center or justify
text-decorationnone, underline, overline, line-through
text-transformuppercase, lowercase, capitalize
text-indentIndent of the first line
letter-spacing, word-spacingSpace between letters and words
line-heightHeight of each line
white-spaceHow spaces and line breaks are handled
text-overflowWhat to show when the text is too long

Examples

h1 { text-align: center; text-transform: uppercase; letter-spacing: 3px; }
a  { text-decoration: none; }
p  { line-height: 1.6; text-indent: 30px; }
.del { text-decoration: line-through; }
Output in the browser

Centred heading

This paragraph has a line height of 1.6 and the first line is indented by 30 pixels, so it is easier to read.

Old price New price


Line height

A line-height of 1.5 to 1.7 (without a unit) makes body text comfortable to read. Without a unit the value is a multiple of the font size.


One line with ellipsis

To cut a long text and show three dots, you need three properties together.

.title {
  width: 200px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
Output in the browser
This is a very long title that will not fit


Summary

  • Use text-align, text-transform and text-decoration to change the look of text.
  • Set line-height around 1.6 for paragraphs.
  • For fonts, see CSS Fonts.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.