Most of the web is text, so the way text is styled matters more than almost anything else on a page. A few properties, such as alignment, line height and spacing, make the difference between a wall of words and something people are happy to read.
Good text styling is often invisible. Visitors don't notice comfortable line spacing or a sensible line length; they just find the page easy to read and stay longer. In this lesson you'll learn the properties that control how text sits on the page.
Text alignment and spacing
| Property | Example | What it does |
|---|---|---|
text-align | left, center, right, justify | Horizontal alignment |
line-height | 1.6 | Space between lines (a unitless number is best) |
letter-spacing | 0.05em | Space between letters |
word-spacing | 4px | Space between words |
text-indent | 2em | Indents the first line |
Left-aligned text is the easiest to read in long passages. Centred text works for short headings and buttons but becomes tiring over several lines, because each line starts in a different place.
justify stretches lines to fill the full width, which can leave large gaps between words on narrow screens. Use it with care.
Text decoration and letter case
| Property | Example | What it does |
|---|---|---|
text-decoration | none, underline, line-through | Draws lines on text |
text-underline-offset | 4px | Moves the underline down |
text-transform | uppercase, capitalize | Changes the case visually |
text-shadow | 2px 2px 4px #0005 | Adds a shadow behind letters |
text-transform only changes how text looks. Screen readers and search engines still see the original letters, so write your HTML in normal sentence case and let CSS create the capitals.
Handling long text and overflow
white-space: nowrapstops text from wrapping onto a new line.overflow-wrap: anywherelets very long words, such as URLs, break so they don't spill out of their box.- The ellipsis pattern,
white-space: nowrap; overflow: hidden; text-overflow: ellipsis;, cuts off text that doesn't fit and adds "…", as in "Some long tit…".
The ellipsis is common in tables, file lists and card titles, where the layout must stay neat whatever the length of the text.
Text and accessibility
Good text styling is also an accessibility issue. Keep body text at least 16px (1rem), use a line height of around 1.5, and make sure the contrast between text and background is at least 4.5 to 1 for normal text. Avoid justified text (text-align: justify) on narrow screens: it creates uneven gaps between words, called rivers, that make reading harder, especially for people with dyslexia. Left-aligned text with a ragged right edge is easiest to read in English. Limit paragraphs to roughly 60 to 75 characters per line with max-width: 65ch, which you'll learn more about in the responsive typography lesson.
Text in other languages
CSS text properties work with every writing system. text-align: start aligns to the left in English but to the right in Arabic and Hebrew, which is why it's a better default than left for multilingual sites. text-transform: uppercase follows the rules of the page's lang attribute, and hyphens: auto only hyphenates correctly when lang is set.
CSS Text Syntax
Common text properties
p {
text-align: left;
line-height: 1.6;
letter-spacing: 0.01em;
text-transform: none;
text-decoration: none;
}CSS Text Summary
text-aligncontrols horizontal alignment; left-aligned text is easiest to read.- Use a unitless
line-heightof around 1.5 to 1.7 for body text. letter-spacing,word-spacingandtext-indentfine-tune spacing.text-transformandtext-decorationchange case and lines without changing the HTML.- Use
white-space,overflow-wrapandtext-overflowto handle long text.
CSS Text Examples
Each example runs in the page. Edit the code and press Run to see what changes.
Readable article text
Try changing line-height to 1 and then 2.5 to feel the difference.
<article>
<h2>Why sloths are slow</h2>
<p>Sloths eat leaves, which give them very little energy. To survive, they move slowly and sleep up to fifteen hours a day. Their slowness is a clever strategy, not laziness.</p>
</article>article {
max-width: 60ch;
font-family: Georgia, serif;
}
h2 {
text-align: center;
text-transform: uppercase;
letter-spacing: 0.1em;
}
p {
line-height: 1.7;
text-indent: 2em;
}Line-by-line explanation
- Line 2Limits lines to about 60 characters for comfortable reading.
- Line 7Centres the heading.
- Lines 8 to 9Uppercase with a little extra letter spacing, a classic heading style.
- Line 13Roomy lines make paragraphs much easier to read.
- Line 14Indents the first line like a book.
Links, shadows and ellipsis
<p><a href="#" class="fancy">A nicely underlined link</a></p>
<h1 class="glow">Game Over</h1>
<p class="title">This is a really really long product title that will not fit in the box</p>.fancy {
color: #4467ff;
text-decoration: underline;
text-decoration-thickness: 2px;
text-underline-offset: 4px;
}
.glow {
color: white;
background: #15161b;
text-shadow: 0 0 12px #f0592b;
padding: 10px;
}
.title {
width: 240px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
border: 1px dashed #aaa;
}Line-by-line explanation
- Lines 3 to 5A thicker underline, pushed further from the text.
- Line 12A blurry orange shadow makes a neon glow.
- Lines 17 to 20The ellipsis trick: one line, hide the overflow, show "…".
Common CSS Text Mistakes
Removing link underlines with nothing else
a { text-decoration: none; color: inherit; }a { text-decoration: none; color: #4467ff; font-weight: 600; }
a:hover { text-decoration: underline; }If links look exactly like normal text, nobody knows they can click them.
Typing in capitals instead of using text-transform
<h2>OUR SERVICES</h2><h2 class="caps">Our services</h2>
<style> .caps { text-transform: uppercase; } </style>Screen readers may spell out ALL-CAPS words letter by letter, and you can't change it later without editing the HTML.
CSS Text in Real Projects
Typography makes sites feel premium
Look at a well-designed site like a magazine or a bank: generous line height, uppercase labels with letter spacing, carefully offset underlines, and long titles neatly truncated with an ellipsis in cards and tables. These small properties do a lot of heavy lifting.
CSS Text Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Centre the heading and make it uppercase.
h1 {
: center;
text-transform: ;
}
text-align positions text; text-transform changes its case.Which removes the underline from links?
Which line-height value is best for body text?
The product name should stay on one line and end with "…" when too long, but it's wrapping. Fix the CSS.
<p class="name">Ultra Comfortable Ergonomic Office Chair with Lumbar Support</p>.name {
width: 200px;
text-overflow: ellipsis;
}- Text doesn't wrap
- Overflow is hidden
- Ellipsis is set
CSS Text Quiz
3 questions to check what you learned.
Which property spaces out letters?
- letter-spacing
- word-spacing
- text-spacing
- font-spacing
What does
text-transform: capitalizedo to "hello world"?- HELLO WORLD
- Hello world
- hello world
- Hello World
Why is a unitless
line-heightlike1.6recommended?- It loads faster
- It scales with each element's font size
- Units aren't allowed
- It's always 16px
CSS Text Challenge
Style a pull quote
Style .pull so that:
- Text is centred
font-styleis italicline-heightis1.5- It has a
letter-spacingof0.02emor more - The
citeinside it is uppercase
<blockquote class="pull">
The internet is just a lot of people being creative together.
<cite>A very wise developer</cite>
</blockquote>- Centred
- Italic
- Line height 1.5
- Letter spacing added
- Cite is uppercase