Text color and basic properties

The most fundamental text property is color, which sets the text color. You can use color names, hex codes, RGB, or HSL.

Beyond color, CSS gives you control over alignment, decoration, transformation, and spacing โ€” all essential for professional typography.

style.css Text color and decoration basics
Pro tip:
Always ensure text color has sufficient contrast with the background for accessibility. Tools like WebAIM Contrast Checker help verify WCAG compliance.

Text spacing and effects

Fine-tune text spacing with letter-spacing, word-spacing, and line-height. Add visual flair with text-shadow and text-indent.

style.css Text spacing, indentation, and shadows

Font family and web-safe fonts

The font-family property sets which typeface to use. Always provide a fallback font stack โ€” if the first font isn't available, the browser tries the next one.

Common web-safe fonts include Arial, Verdana, Georgia, Times New Roman, and Courier New. These are available on almost all devices.

style.css Font families and fallback stacks
Key point:
Always quote font names with spaces (e.g., 'Segoe UI') and end with a generic family (sans-serif, serif, monospace).

Font size, weight, and style

Font size can be set in pixels (px), em, rem, or percentages. Font weight controls boldness (100โ€“900 or bold/normal). Font style adds italics.

style.css Font size, weight, style, and shorthand

Google Fonts

Google Fonts offers hundreds of free, open-source typefaces. Import them in your HTML <head>, then use them in CSS like any font.

index.html Importing Google Fonts

Visit fonts.google.com to browse fonts. You can filter by weight and preview them before importing.

Custom fonts with @font-face

Use @font-face to load custom font files. This allows you to use any typeface, even if it's not installed on the user's device.

style.css Defining and using @font-face
Performance tip:
Use modern formats like WOFF2 for smaller file sizes. Limit custom fonts to 2โ€“3 typefaces to reduce page load time.

Try it yourself: Typography playground

Edit the CSS in the left pane to see text styling changes in real-time. Try different values for text-align, text-transform, letter-spacing, line-height, and text-shadow.

โœ๏ธ Try It Yourself โ€” Blog Post Styling
Preview

Try it yourself: Google Fonts demo

This example imports two Google Fonts and demonstrates different font weights. Modify the font-family lines to switch fonts, or change font-weight to see how typefaces look at different weights.

โœ๏ธ Try It Yourself โ€” Google Fonts
Preview
Challenge: Style a Magazine Article

Task: Create and style a magazine article with:

  • Import two different Google Fonts (one for headings, one for body text)
  • Style the main heading with text-shadow and text-transform: uppercase
  • Use justify text-align on the article body
  • Add letter-spacing to the subtitle
  • Style a pull-quote with italic and larger font-size
  • Use different font-weights to show emphasis

Use the editors above to experiment, or create your own HTML file. Start simple and build complexity!

Hint: Visit fonts.google.com to find fonts you like. Copy the import link into your HTML head. For the pull-quote, wrap it in a <blockquote> or <p> with a class, then apply italics and increase font-size. Use text-shadow: 1px 1px 3px rgba(0,0,0,0.2) for the heading.

Key takeaways

  • color sets text color; text-align aligns text; text-decoration adds underlines or strikethroughs.
  • text-transform changes case (uppercase, lowercase, capitalize); text-shadow adds shadows.
  • font-family selects typeface; always use a fallback stack (e.g., 'Open Sans', sans-serif).
  • font-size uses px, em, rem, or %; font-weight controls boldness (100โ€“900); font-style adds italics.
  • Import Google Fonts via <link> in the <head>; use @font-face for custom font files.
  • letter-spacing, word-spacing, and line-height control text spacing for readability.

Test your knowledge

Question 1 of 5
Which property changes text to all uppercase?
Question 2 of 5
What unit is relative to the root (html) font size?
Question 3 of 5
Which property adds a shadow to text?
Question 4 of 5
What is the correct order for the font shorthand?
Question 5 of 5
Why use a font stack like 'Open Sans', sans-serif?
0
/5
Great work! You understand CSS text and fonts.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just mastered CSS typography.
Your design skills are growing fast.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’