Beginner30 minutesCSSTypographyStyling

Text Manipulation with CSS

Learn how to style and manipulate text using CSS properties

Learning Objectives

By the end of this lesson, you'll be able to:

  • Apply Apply font families and font stacks for cross-browser text consistency
  • Control text size, weight, and style using appropriate CSS properties
  • Adjust line height and letter spacing to improve readability
  • Transform text case and add decorative effects like shadows
  • Create Create text alignment and indentation for professional layouts
  • Implement web fonts to expand typographic possibilities
  • Practice typography principles through real-world exercises

Understanding Typography

Typography is the art and technique of arranging type to make written language legible, readable, and appealing. Its history spans from ancient handwritten texts to modern digital displays.

Type Anatomy

Type Ascender Line Cap Height Mean Line (x-height) Baseline Descender Line x-height Cap Height Ascender Descender Key Typography Terms • Baseline: The invisible line where most letters sit • x-height: Height of lowercase letters (like 'x') • Cap Height: Height of capital letters • Ascender: Extends above x-height (like in 'h') • Descender: Extends below baseline (like in 'p')

Historical Evolution

A B C D E F
Gutenberg's movable type system (1440s)
Modern digital type design

Historical Context

3000 BCE

Early writing systems and pictographs

1440s

Gutenberg's movable type revolutionizes printing

1980s

Digital typography emerges with desktop publishing

1990s

Web fonts and CSS bring typography to the internet

Key Concepts

  • Type Anatomy: The parts of letters (baseline, x-height, ascenders, descenders)
  • Typography Terms: Leading, kerning, tracking, and other essential concepts
  • Type Classification: Serif, sans-serif, display, and decorative fonts
  • Digital Evolution: From bitmap to vector fonts, web fonts to variable fonts

Basic Text Properties

Master the fundamental properties for styling text in CSS.

Font Family

Historical Context

The concept of font families emerged from traditional typesetting, where related fonts were grouped into families. Each family included variations like regular, bold, and italic, designed to work harmoniously together.

In digital typography, font families help maintain consistency across different devices and platforms, leading to the development of web-safe fonts and later, web fonts services.

The font-family property specifies the typeface for text elements.

Web Safe Fonts

Web safe fonts are typefaces that are pre-installed across most operating systems and devices. They're considered "safe" because:

Why They're "Safe"
  • Universal Availability: Pre-installed on 99%+ of devices
  • Zero Loading Time: No download required, instant rendering
  • Consistent Performance: No FOUT (Flash of Unstyled Text)
  • Fallback Reliability: Perfect for system font stacks
  • Legacy Support: Work on older browsers and systems
Historical Context

Web safe fonts emerged in the early days of the web when:

  • Bandwidth was limited and expensive
  • @font-face wasn't widely supported
  • Operating systems came with a standard set of fonts
  • Web designers needed reliable typography solutions

Font Stacks

Modern System Font Stack

Modern approach: Using system UI fonts for native feel

Common Web Safe Fonts by Category
CategoryFont NamesBest Used ForOS Availability
Sans-serifArial, Helvetica, VerdanaBody text, UI elementsWindows, macOS, Linux
SerifTimes New Roman, GeorgiaHeadlines, long-form contentWindows, macOS
MonospaceCourier New, MonacoCode, technical contentUniversal
Important Considerations:
  • Web safe fonts might render differently across operating systems
  • They often lack the design sophistication of modern web fonts
  • Limited selection of weights and styles
  • Best used as reliable fallbacks in font stacks
Best Practices:
  • Always include fallback fonts
  • Use web-safe fonts or Google Fonts
  • Consider loading times with web fonts
  • Test fonts across different devices
  • Use quotes for font names with spaces
  • End with a generic font family

Web Font Formats

Modern web typography uses several font file formats, each with specific browser support and use cases:

FormatExtensionDescriptionSupport
WOFF2.woff2 Web Open Font Format 2.0. Best compression and performance. Modern browsers
WOFF.woff Original Web Open Font Format. Good fallback option. Wide support
TTF/OTF.ttf/.otfTrueType/OpenType fonts. Original font formats.Legacy support
Using @font-face
Best Practices:
  • Always include WOFF2 format first for best performance
  • Provide WOFF as fallback for broader compatibility
  • Consider using variable fonts for multiple weights/styles
  • Implement font loading strategies to prevent FOUT/FOIT
  • Use local() to leverage system-installed fonts when available

Font Size

Control the size of your text using different CSS units.

Size Units

Pixels (16px)
Em units (1.2em)
Root Em (1.2rem)
Percentage (120%)

Size Examples

Size Unit Tips:
  • Use rem for consistent sizing across the site
  • Em units are relative to parent element's font size
  • Pixels provide precise control but less flexibility
  • Consider responsive design when choosing units

Font Weight

Adjust the thickness of your text with font-weight property.

Weight Values

Light (300)
Regular (400)
Semi-Bold (600)
Bold (700)
Black (900)

Weight Examples

Text Color

Control the color of your text using CSS. We'll cover the basics here, but for a deeper dive into colors, check out the Colors tutorial.

Basic text color (#333333)
Secondary text (#666666)
Muted text (#999999)

Color Examples

Text Alignment

Control how text is aligned within an element using the text-align property.

Alignment Examples

Left aligned text (default): Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Center aligned text: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Right aligned text: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Justified text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.

Code Example

Best Practices:
  • Use left alignment for most body text (default in left-to-right languages)
  • Center alignment works well for headings and short pieces of text
  • Right alignment is useful for numbers in tables or specific design elements
  • Use justify with caution as it can create uneven spacing between words
  • Consider text direction in different languages when setting alignment

Vertical Alignment

First column text aligned to the baseline grid for consistent vertical rhythm. Text in the second column maintains the same baseline alignment across columns. The third column shows how text aligns perfectly with the baseline grid.
Traditional baseline grid showing how text aligns across columns

Text Vertical Alignment Examples

Text Vertical Alignment Examples
Base with super/sub:
Base text superscript and subscript example
Baseline:
Baseline (default)
Middle:
Middle alignment
Text-top:
Text top

Code Example

Vertical Align Properties
  • baseline: Default, aligns with text baseline
  • sub: Subscript alignment
  • super: Superscript alignment
  • text-top: Aligns with top of parent's font
  • text-bottom: Aligns with bottom of parent's font
  • middle: Aligns with middle of parent
  • Length values: Raise or lower by specific amount
Best Practices:
  • Use vertical-align only with inline or table-cell elements
  • Consider using flexbox or grid for complex vertical alignments
  • Be consistent with baseline alignment in typography
  • Test alignment across different font sizes and families
  • Use percentage values for responsive designs

Text Spacing

Line One Line Two Line Three 24px
Traditional leading with metal type
VA -2px
Kerning adjustments between letters
TRACK 0.2em spacing
Tracking affects overall letter spacing

Control the spacing between lines, letters, and words to improve readability and visual appeal.

Spacing Examples

Default Spacing
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.
Expanded Spacing
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.
Condensed Spacing
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

Code Example

Property Details
  • line-height: Controls the height of each line of text
    • Can be unitless (recommended)
    • Common values: 1.5-1.7 for body text
  • letter-spacing: Adjusts space between characters
    • Use em units for relative spacing
    • Negative values bring letters closer
  • word-spacing: Controls space between words
    • Default is normal (0)
    • Use em units for relative spacing
Best Practices:
  • Use unitless values for line-height to maintain proportional spacing
  • Keep letter-spacing subtle for body text (±0.05em or less)
  • Test readability across different screen sizes
  • Consider using tighter spacing for headings
  • Use expanded spacing sparingly for emphasis or style

Additional Text Properties

Text Decoration

text-decoration: underline;Underlined text
text-decoration: line-through;Strikethrough text
text-decoration: overline;Overlined text
text-decoration: underline wavy red;Styled underline

Text Transform

text-transform: uppercase;uppercase text
text-transform: lowercase;LOWERCASE TEXT
text-transform: capitalize;capitalized text

Text Spacing

letter-spacing: 0.2em;Spaced out letters
word-spacing: 1em;Words with extra space
white-space: nowrap;This text won't wrap to new line

Text Shadow

text-shadow: 2px 2px 4px #000;Basic shadow
text-shadow: 0 0 5px #ff0000;Glowing text
text-shadow: 1px 1px #ff0000, -1px -1px #00ff00;Multiple shadows

Best Practices

  • Use text-decoration sparingly to maintain readability
  • Consider accessibility when using text-transform (screen readers may read uppercase text as acronyms)
  • Ensure text shadows maintain sufficient contrast
  • Be cautious with letter-spacing as it can affect readability at extreme values

⏸️ Pause & Check: Do You Understand?

Before moving forward, can you answer these?

  1. What is the difference between font-size, line-height, and letter-spacing?
  2. When should you use em versus rem units for font sizing?
  3. What is the purpose of font fallback stacks in font-family?
  4. How do text-align and text-decoration affect readability and user experience?
Check Your Answers
  1. font-size controls the height of text characters, line-height controls the vertical space between lines of text (affecting readability), and letter-spacing adjusts horizontal space between characters. Together, they create typography rhythm and readability.
  2. rem units are relative to the root (html) font size and are predictable across the page. em units are relative to the parent element and compound, making them useful for component-level scaling but potentially confusing. rem is generally preferred for consistency.
  3. Font fallback stacks provide alternative fonts if the primary font fails to load or is unavailable. Example: font-family: "Helvetica Neue", Arial, sans-serif; ensures text displays even if Helvetica Neue is missing. Always end with a generic font family for reliability.
  4. text-align controls horizontal alignment (left, center, right, justify). Left alignment is most readable for body text in left-to-right languages. text-decoration adds underlines, overlines, or strikethroughs—use carefully as underlines imply links. Both affect readability and user expectations.

How confident are you with this concept?

😕 Still confused | 🤔 Getting there | 😊 Got it! | 🎉 Could explain it to a friend!

Practical Exercises

Exercise 1: Black Swan Bistro Typography

Requirements

  • Implement the brand fonts (Playfair Display and Source Sans Pro)
  • Set up proper font sizes and weights as specified in the style guide
  • Ensure proper line heights for readability
  • Apply appropriate text colors from the brand palette

Steps

  1. Add the Google Fonts link to your HTML:
  2. Set up your base typography CSS:
  3. Style specific elements:

Success Criteria

  • Brand name uses Playfair Display (700 weight)
  • Headings use correct font sizes (2.5rem, 2rem, 1.5rem)
  • Body text uses Source Sans Pro with proper line height
  • Menu descriptions are properly styled and readable

Exercise 2: Personal Profile Page Typography

Requirements

  • Create a clear typographic hierarchy for the profile sections
  • Style headings to distinguish between sections
  • Format the skills list for easy scanning
  • Ensure proper spacing and readability for Alex Chen's bio

Steps

  1. Set up a system font stack:
  2. Create your typography scale:
  3. Style content sections:

Success Criteria

  • Clear distinction between content sections
  • Readable paragraphs with appropriate line height
  • Consistent spacing between elements
  • Proper emphasis for important information

Lesson checkpoint

Test Your Knowledge

5 questions

Strengthen your understanding of Text by answering the quiz below.

Text Quiz

Test your understanding of Text concepts.

Lesson Complete: What You Learned

Key Takeaways:

  • font-family, font-size, font-weight, and font-style control typeface appearance and emphasis
  • line-height affects readability—1.5 to 1.6 is optimal for body text
  • letter-spacing and word-spacing adjust character and word spacing for visual effect
  • text-align, text-indent, and text-decoration control text layout and decoration
  • rem units provide consistent, predictable sizing relative to the root element
  • Web fonts via @font-face or Google Fonts expand typography options beyond system fonts
  • Good typography improves readability, hierarchy, and overall user experience

Learning Objectives Review:

Look back at what you set out to learn. Can you now:

  • ✅ Master font properties: family, size, weight, style Check!
  • ✅ Control text spacing with line-height, letter-spacing, word-spacing Got it!
  • ✅ Apply text alignment and decoration effectively Can explain it!
  • ✅ Use relative units (em, rem) for responsive typography Could teach this!
  • ✅ Load and use web fonts from Google Fonts or custom sources Check!
  • ✅ Create readable, accessible typography hierarchies Got it!

If you can confidently answer "yes" to most of these, you're ready to move on!

Think & Reflect:

Typography Hierarchy

Good typography guides users through content naturally. Use size and weight to establish hierarchy, adequate spacing for readability, and appropriate fonts for the content's tone and purpose.
  • How do font size, weight, and spacing create visual hierarchy?
  • What makes text readable and comfortable to read for long periods?

Responsive Typography

Responsive typography uses relative units and media queries to ensure readability across devices. Text that's perfect on desktop might be too small or too large on mobile. Plan typography for all screen sizes.
  • Why are relative units important for responsive design?
  • How can typography adapt to different screen sizes?

🤔 Real-World Test:

Typography is fundamental to professional web design. Sites like Medium, The New York Times, and Stripe are renowned for their typography—careful font choices, spacing, and hierarchy that make content pleasant to read. Good typography isn't noticed; bad typography drives users away.

Modern web typography leverages variable fonts, fluid typography (responsive font sizing), and careful performance optimization to deliver beautiful, fast-loading text across all devices. Understanding CSS text properties is the foundation for these advanced techniques.

🎯 Looking Ahead:

With typography mastered, you're ready to tackle CSS layout—one of the most powerful aspects of CSS. In the next lesson, you'll learn positioning, display modes, floats, and layout strategies that control where elements appear on the page.

Layout is what transforms static HTML into dynamic, visually interesting web pages. Understanding layout fundamentals prepares you for Flexbox, Grid, and modern CSS layout techniques.

Recommended Next Steps

Practice Projects

Apply what you've learned with these hands-on projects:

Enhanced Personal Profile

Style your profile page with typography and layout

CSSIntermediateTypography
Start Project

Additional Resources

Deepen your understanding with these helpful resources:

Progress tracking is disabled. Enable it in to track your completed tutorials.

Quiz Available: Test your knowledge with the interactive quiz at the end of this tutorial.

Jump to Quiz

Progress tracking is disabled. Enable it in to track your completed tutorials.