Text Styling and Wrapping

Back

Loading concept...

Typography: Text Styling and Wrapping 🎨

The Magic Paintbrush for Your Words

Imagine you have a magic paintbrush. But instead of painting pictures, this paintbrush decorates words! You can add sparkles underneath them, make them SHOUT in capitals, give them cool shadows, and even teach them how to wrap nicely when they run out of space.

That’s what CSS text styling properties do. They’re your magic toolkit for making text look amazing and behave properly on any screen.


πŸ–ŒοΈ text-decoration: Drawing Lines on Text

Think of text-decoration like drawing with a marker on top of your words.

What Can You Draw?

Value What It Does Like…
underline Line below Underlining important words in a book
overline Line above A hat on your text
line-through Line through middle Crossing out a mistake
none No line at all Erasing all decorations

Example

.link {
  text-decoration: underline;
}

.deleted {
  text-decoration: line-through;
}

.fancy {
  text-decoration: underline wavy red;
}

Pro tip: You can combine style, color, and line type all in one!

text-decoration: underline dotted blue;

This gives you a dotted blue underline. Cool, right?


πŸ”€ text-transform: The Case Changer

Imagine a magic spell that changes how letters look without retyping anything!

The Three Spells

Value What Happens Example
uppercase ALL CAPS! hello β†’ HELLO
lowercase all small HELLO β†’ hello
capitalize First Letter Big hello world β†’ Hello World

Example

.shout {
  text-transform: uppercase;
}

.whisper {
  text-transform: lowercase;
}

.title {
  text-transform: capitalize;
}

Real Life: Buttons often use uppercase to look important!


πŸŒ‘ text-shadow: Adding Shadows Behind Text

Remember how your hand creates a shadow in sunlight? text-shadow does the same for letters!

The Recipe

text-shadow: horizontal vertical blur color;

Think of it like this:

  • Horizontal: How far right (or left with negative)
  • Vertical: How far down (or up with negative)
  • Blur: How fuzzy the shadow is
  • Color: The shadow’s color

Examples

/* Simple drop shadow */
.shadow-basic {
  text-shadow: 2px 2px 4px gray;
}

/* Glowing effect */
.glow {
  text-shadow: 0 0 10px yellow;
}

/* Multiple shadows for 3D effect */
.three-d {
  text-shadow:
    1px 1px 0 #ccc,
    2px 2px 0 #999;
}

Fun fact: Setting horizontal and vertical to 0 with blur creates a glow effect!


πŸ“ text-underline-offset: Spacing the Underline

Ever noticed how some underlines cut through letters like β€œg” and β€œy”? That looks messy!

text-underline-offset pushes the underline down, away from the text.

Example

.nice-underline {
  text-decoration: underline;
  text-underline-offset: 4px;
}

Think of it like adjusting how low your marker draws under the word.

Value Result
2px Slight gap
5px Nice breathing room
auto Browser decides

πŸ“ white-space: How Text Handles Spaces

Imagine you’re writing on a very long scroll of paper. white-space decides:

  • Do spaces stay or get squished?
  • Does text wrap to the next line or keep going forever?

The Options

Value Spaces Line Breaks Wrapping
normal Collapse Ignored Yes
nowrap Collapse Ignored No
pre Keep all Keep all No
pre-wrap Keep all Keep all Yes
pre-line Collapse Keep all Yes

Examples

/* Keep text on one line */
.no-wrap {
  white-space: nowrap;
}

/* Preserve all spacing like in code */
.code-block {
  white-space: pre;
}

/* Keep spaces but allow wrapping */
.formatted {
  white-space: pre-wrap;
}

When to use what:

  • nowrap β†’ Buttons, badges, labels
  • pre β†’ Code snippets
  • pre-wrap β†’ Poetry or formatted text that should still wrap

βœ‚οΈ word-break: Breaking Long Words

What happens when you have a reallylonglonglongword that doesn’t fit?

word-break tells the browser what to do!

Options

Value Behavior
normal Break only at normal spots
break-all Break anywhere needed
keep-all Never break (especially for Asian text)

Example

.break-words {
  word-break: break-all;
}

Use case: URLs and email addresses that might be super long!


πŸ”„ overflow-wrap: The Gentle Word Breaker

overflow-wrap (also called word-wrap) is like word-break’s polite cousin.

It only breaks words when absolutely necessaryβ€”when they would overflow their container.

Options

Value What It Does
normal Only break at normal points
break-word Break words only if they overflow
anywhere Break anywhere, even mid-word

Example

.safe-text {
  overflow-wrap: break-word;
}

word-break vs overflow-wrap

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ word-break: break-all           β”‚
β”‚ Breaks words AGGRESSIVELY       β”‚
β”‚ Even when not needed            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ overflow-wrap: break-word       β”‚
β”‚ Breaks words ONLY when they     β”‚
β”‚ would overflow the box          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Rule of thumb: Use overflow-wrap: break-word for most cases. It’s safer!


βž– hyphens: Adding Hyphens When Words Break

When a word breaks across lines, it looks better with a hyphen (-).

Like in books: β€œpro- gramming” instead of just β€œpro gramming”

Options

Value Behavior
none Never add hyphens
manual Only where you put ­
auto Browser adds hyphens smartly

Example

.nice-text {
  hyphens: auto;
}

Important: For auto to work, tell the browser the language:

<p lang="en">Your English text here</p>

🎯 Quick Reference Flow

graph TD A["Text Styling"] --> B["Decoration"] A --> C["Transform"] A --> D["Shadow"] B --> B1["underline"] B --> B2["line-through"] B --> B3["overline"] C --> C1["uppercase"] C --> C2["lowercase"] C --> C3["capitalize"] D --> D1["x y blur color"]
graph TD A["Text Wrapping"] --> B["white-space"] A --> C["word-break"] A --> D["overflow-wrap"] A --> E["hyphens"] B --> B1["normal/nowrap/pre"] C --> C1["normal/break-all"] D --> D1["normal/break-word"] E --> E1["none/manual/auto"]

πŸ† Putting It All Together

Here’s a complete example using all these properties:

.styled-paragraph {
  /* Make text uppercase */
  text-transform: uppercase;

  /* Add underline with offset */
  text-decoration: underline;
  text-underline-offset: 3px;

  /* Subtle shadow */
  text-shadow: 1px 1px 2px rgba(0,0,0,0.2);

  /* Handle long words gracefully */
  overflow-wrap: break-word;

  /* Add hyphens when breaking */
  hyphens: auto;

  /* Preserve line breaks */
  white-space: pre-line;
}

πŸ’‘ Remember These Golden Rules

  1. text-decoration = Lines ON text (under, over, through)
  2. text-transform = Change letter CASE
  3. text-shadow = Shadows BEHIND text
  4. text-underline-offset = DISTANCE between text and underline
  5. white-space = How SPACES and BREAKS behave
  6. word-break = AGGRESSIVE word breaking
  7. overflow-wrap = GENTLE word breaking (only when needed)
  8. hyphens = Add HYPHENS when breaking words

πŸŽ‰ You Did It!

You now know how to style and wrap text like a pro! These eight properties give you complete control over how text looks and behaves.

Next time you see:

  • A glowing button β†’ text-shadow!
  • An ALL CAPS heading β†’ text-transform!
  • A nice underlined link β†’ text-decoration + text-underline-offset!
  • Long URLs that don’t break β†’ overflow-wrap to the rescue!

You’re not just reading CSS anymore. You’re understanding it. πŸš€

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.