CSS Documentation

Text

CSS Text Properties

CSS provides various properties to control the appearance and formatting of text on web pages. These properties allow you to manipulate text color, alignment, decoration, spacing, and more.

Common Text Properties

  • color: Sets the color of the text.
  • font-family: Specifies the font for the text.
  • font-size: Sets the size of the font.
  • font-weight: Defines the thickness of the characters.
  • text-align: Aligns the text horizontally within its container.
  • text-decoration: Adds decorations to text (e.g., underline, overline).
  • line-height: Sets the height of a line of text.
  • letter-spacing: Adjusts the space between characters.
  • text-transform: Controls the capitalization of text.

Example

Here's an example of using different text properties:

.text-example {
color: #007bff;
font-family: Arial, sans-serif;
font-size: 16px;
font-weight: bold;
text-align: center;
text-decoration: underline;
line-height: 1.5;
letter-spacing: 2px;
text-transform: uppercase;
}

Text Shadow

The text-shadow property adds shadow to text:

.shadow-text {
text-shadow: 2px 2px 4px #000000;
}

Text Overflow

The text-overflow property specifies how overflowed content should be displayed:

.overflow-text {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

Best Practices

  • Use relative units (like em or rem) for font sizes to ensure better responsiveness.
  • Maintain consistent typography throughout your website for a cohesive design.
  • Ensure sufficient contrast between text and background colors for readability.
  • Use web-safe fonts or include fallback fonts in your font-family declaration.
  • Consider line-height for better readability, especially for longer text blocks.
  • Use text-transform cautiously, as it can affect screen readers and copy-paste functionality.

Mastering CSS text properties is crucial for creating visually appealing and readable web content. Experiment with different combinations to achieve the desired look and feel for your text elements.