Your Course Progress

Topics
0 / 0
0.00%
Practice Tests
0 / 0
0.00%
Tests
0 / 0
0.00%
Assignments
0 / 0
0.00%
Content
0 / 0
0.00%
% Completed

CSS Text Styling: A Comprehensive Guide

CSS offers a robust collection of properties to style and manage text on your web pages. Let’s explore the key properties that control text appearance, flow, and behavior.

CSS provides a powerful set of properties to style and manipulate text within your web pages. Let's dive into the key properties that allow you to control text appearance, flow, and behavior.

Use the text-align property to control the horizontal alignment of text within a block element:

p {
  text-align: left; /* Default alignment */
}

p {
  text-align: center;
}

p {
  text-align: right;
}

p {
  text-align: justify; /* Align text to both edges */
}
Do You Know?

The text-align property works on block-level elements like paragraphs (<p>), headings (<h1> - <h6>), and divs (<div>).

The text-decoration property lets you add decorative elements like underlines, overlines, and strikethroughs to text:

p {
  text-decoration: underline;
}

p {
  text-decoration: overline;
}

p {
  text-decoration: line-through;
}

The text-transform property controls the capitalization of text:

p {
  text-transform: capitalize; /* First letter of each word capitalized */
}

p {
  text-transform: uppercase;
}

p {
  text-transform: lowercase;
}

Control the spacing around text using the following properties:

  • letter-spacing: Adjust the space between individual letters.
  • word-spacing: Adjust the space between words.
  • text-indent: Indent the first line of a paragraph.
p {
  letter-spacing: 2px;
}

p {
  word-spacing: 5px;
}

p {
  text-indent: 2em;
}

Add a shadow effect to text using the text-shadow property. You can specify the offset, blur radius, and color of the shadow:

h1 {
  text-shadow: 2px 2px 5px #888888;
}
Important Note

The text-shadow property can be applied to multiple shadows by separating them with commas. For example: text-shadow: 2px 2px 5px #888888, -2px -2px 5px #888888;

Control how text handles overflow situations (when the content exceeds the container's boundaries) with these properties:

  • overflow: Determines how content is handled when it overflows the element's box. Common values are visible (default), hidden, scroll, and auto.
  • text-overflow: Specifies how text should be handled when it overflows its container. The ellipsis value adds an ellipsis (...) to truncate text. text-overflow:ellipsis; only works when the following are true:
    • The element's width must be constrained in px (pixels). Width in % (percentage) won't work.
    • The element must have overflow:hidden and white-space:nowrap set.
  • white-space: Controls how white space (spaces, tabs, and newlines) is handled. Values like normal, nowrap, and pre affect how text wraps and displays.
p {
  overflow: hidden; /* Content is hidden if it overflows */
}

p {
  text-overflow: ellipsis; /* Add ellipsis to truncated text */
}

p {
  white-space: nowrap; /* Prevent text from wrapping */
}

Control word wrapping and breaking behavior using these properties:

  • word-wrap: Allows long words to be broken into multiple lines if they don't fit within the container. The break-word value enables this behavior.
  • word-break: Controls how words are broken into multiple lines. Values like break-all (breaks words anywhere) and keep-all (prevents word breaks) can be used.
p {
  word-wrap: break-word;
}

p {
  word-break: break-all;
}
Avoid This

Using word-break: break-all liberally can lead to poor readability as words are split in unexpected places.

  • CSS provides a range of properties to control text appearance, flow, and behavior.
  • Use text-align, text-decoration, and text-transform to style text visually.
  • Control spacing and indentation with letter-spacing, word-spacing, and text-indent.
  • Add text shadows using text-shadow.
  • Handle overflow situations with overflow, text-overflow, and white-space.
  • Control word wrapping and breaking with word-wrap and word-break.

Discussion