CSS Text Properties: Alignment, Spacing, Decoration and Wrapping
CSS Text Properties: Color, Alignment, Spacing, Decoration and Wrapping
CSS text properties control how text looks and flows inside its box: color, alignment, line height, letter spacing, decoration, case and wrapping. Good text styling is the biggest factor in readability, so these properties matter in every project. This lesson separates properties that style the text itself from those that control fonts (covered in the next lesson).
What You Will Learn
color,text-align,text-indentandtext-transformline-height,letter-spacingandword-spacingtext-decorationand its longhand properties- Whitespace and wrapping:
white-space,word-break,overflow-wrap,hyphens vertical-alignfor inline content- Readable text: contrast, line length and line height guidelines
Text Color
The color property sets the foreground color of text and also the default color for borders and currentColor. It is inherited, so setting it on body colors all text. Use enough contrast: WCAG AA requires a ratio of at least 4.5:1 for normal text and 3:1 for large text (about 24px, or 18.66px bold).
text-align
left,right,center,justify: horizontal alignment of inline content inside a block.startandend: direction-aware values that work in right-to-left languages such as Urdu and Arabic. Prefer them for international sites.text-alignaligns inline content, not the block itself. To center a block, usemargin-inline: autoor Flexbox.text-align: justifycan create uneven gaps between words on narrow columns. Combine it withhyphens: autoor avoid it on small screens.text-align-lastcontrols the last line of a justified paragraph.
line-height
line-height sets the height of each line box. Use a unitless number such as line-height: 1.6, because the multiplier is inherited and recalculated per element. A fixed value like 24px or a percentage is inherited as a computed length and can cause overlapping text in children with larger fonts. Good defaults: 1.5 to 1.7 for body text, 1.1 to 1.3 for large headings. WCAG 1.4.12 asks that content still works when line height is set to at least 1.5 times the font size.
letter-spacing and word-spacing
letter-spacingadds or removes space between characters. Useemso it scales with the font size, for example0.05emfor uppercase labels and small negative values for large headings.word-spacingchanges space between words.- Never add letter spacing to cursive scripts such as Arabic or Urdu, because it breaks the joined letters.
text-transform
uppercase, lowercase, capitalize and none change how text is displayed without changing the HTML. The source text stays the same for copy-paste and screen readers. Note that capitalize only capitalizes the first letter of each word and does not follow title-case grammar rules.
text-indent
text-indent indents the first line of a block, like in printed books: text-indent: 2em;. A negative value with padding can create a hanging indent for lists and references.
text-decoration
The shorthand text-decoration combines line, style, color and thickness. Longhands:
text-decoration-line:underline,overline,line-through,nonetext-decoration-style:solid,double,dotted,dashed,wavytext-decoration-colortext-decoration-thicknesstext-underline-offset: distance between text and underline. A value like0.2emgives cleaner links.text-decoration-skip-ink: auto(default) stops the underline from crossing letter descenders.
Keep underlines on links inside body text. Removing them hurts usability because color alone is not always enough to identify links.
Whitespace: white-space
normal: collapse spaces and wrap lines.nowrap: collapse spaces but never wrap.pre: preserve spaces and line breaks, never wrap (like thepretag).pre-wrap: preserve spaces and breaks, but wrap when needed. Great for user-entered text.pre-line: collapse spaces, preserve line breaks.break-spaces: like pre-wrap, but trailing spaces take space and can wrap.
Breaking Long Words
overflow-wrap: break-word(oranywhere): break an otherwise unbreakable word only if it would overflow the container.anywherealso affects min-content size, which helps in flex and grid.word-break: break-all: break between any two characters. Useful for some CJK text but ugly for English.word-break: keep-all: do not break inside CJK words.hyphens: auto: let the browser add hyphens using the language dictionary. You must set thelangattribute onhtmlfor it to work.hyphens: manualonly breaks at soft hyphens (­).
vertical-align
vertical-align works on inline, inline-block and table-cell elements, not on block elements. Values like middle, baseline, sub, super, top and bottom align an inline box relative to the line. It is often used to fix the gap under images (img { vertical-align: middle; } or display: block). To center a block vertically use Flexbox or Grid, not vertical-align.
Readable Text Guidelines
- Line length: about 45 to 75 characters per line. Use
max-width: 65ch;. - Line height: about 1.5 to 1.7 for paragraphs.
- Font size: at least 16px (1rem) for body text.
- Left or start aligned body text is easiest to read. Avoid long centered paragraphs.
- Keep enough contrast and avoid pure
#000on pure#fffif it feels harsh. Dark gray such as#1f2937is often more comfortable.
Common Mistakes
- Using a unit on
line-heightsuch asline-height: 24pxon body and breaking larger children. - Expecting
text-align: centerto center a block element. - Using
text-transformto change meaning (screen readers may still read the original). - Removing link underlines without another clear cue.
- Applying
letter-spacingto Arabic-script text. - Expecting
hyphens: autoto work without alangattribute.
Frequently Asked Questions
What is the best line-height for body text?
Between 1.5 and 1.7, written as a unitless number.
How do I remove the underline from a link?
Use text-decoration: none, but keep another visual clue such as bold weight or a border, and keep underlines in paragraphs.
What is the difference between word-break and overflow-wrap?
overflow-wrap: break-word breaks only when a word would overflow. word-break: break-all breaks words aggressively even when they would fit.
Are text properties inherited?
Most are: color, text-align, line-height, letter-spacing, text-transform and white-space. text-decoration is not inherited but is drawn across child inline boxes.
Key Takeaways
- Use unitless line-height (1.5 to 1.7) and a line length near 65ch.
- Use
text-align: startandendfor international layouts. - Control wrapping with white-space, overflow-wrap and hyphens.
- Style links with text-underline-offset and thickness instead of removing underlines.
- Always check color contrast (4.5:1 minimum for body text).
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Style the .article paragraph for comfortable reading: a maximum line length of 65 characters, a unitless line height of 1.6, left aligned text (use the direction-aware value) and dark gray text color #1f2937.
Press Run to execute.
Show expected output
The paragraph is at most 65ch wide with line-height 1.6, start alignment and color #1f2937.This is a self-check — compare your result with the expected output above.
Turn the .tag into an uppercase label with letter spacing of 0.1em, bold weight and a dotted underline that is 2px thick and offset 0.25em from the text.
Press Run to execute.
Show expected output
The text shows as NEW FEATURE with 0.1em letter spacing, bold weight and a dotted 2px underline.This is a self-check — compare your result with the expected output above.
Write a function lineCount(text, maxChars) that splits text into words and returns how many lines are needed if each line can hold at most maxChars characters (spaces count, words are never split, and assume no single word is longer than maxChars).
Press Run to execute.
Show expected output
2
3
1This is a self-check — compare your result with the expected output above.
Was this page helpful?