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, labelspreβ Code snippetspre-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
- text-decoration = Lines ON text (under, over, through)
- text-transform = Change letter CASE
- text-shadow = Shadows BEHIND text
- text-underline-offset = DISTANCE between text and underline
- white-space = How SPACES and BREAKS behave
- word-break = AGGRESSIVE word breaking
- overflow-wrap = GENTLE word breaking (only when needed)
- 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-wrapto the rescue!
Youβre not just reading CSS anymore. Youβre understanding it. π
