Topic 4.4
Text Alignment, Decoration and Transform
In one line
text-align, text-decoration, text-transform, letter-spacing, word-spacing, text-indent and text-shadow control where text sits in its line, its lines and underlines, its capital letters, its spacing and its shadow.
Think of it like this
Decorating a birthday card. You decide whether the message goes on the left, in the middle or across the whole card (alignment), you underline the name (decoration), you write HAPPY BIRTHDAY in capitals (transform), you space the letters out for a fancy look (letter spacing), start the first line a little further in (indent), and draw a soft shadow behind the title (text shadow). The words you wrote stay the same; only how they look changes.
Words you'll meet
New words in this topic, in plain English. Come back here whenever one feels fuzzy.
- Inline content
- Things that sit in a line of text: words, links, images,
<span>s. - Block box
- A box that starts on a new line and takes the full width, like a paragraph or a
<div>. - Justify
- Stretching the spaces in each line so the text touches both the left and right edges.
- Logical values
startandend: values that follow the reading direction instead of fixed left/right.- Decoration
- A line drawn on text: underline, overline or line-through.
- Propagation
- A parent's underline being drawn across its children too, even if they say
none. - Transform (text)
- Changing letters to upper, lower or title case when drawn, without changing the HTML.
- Letter spacing
- Extra space between each character.
- Indent
- Starting the first line of a paragraph further in than the others.
Step by step
01Align the text in its box
Put text-align on the block that contains the text. The paragraph box is still full width; only the lines inside it move.
.hero {
text-align: center;
}
.price {
text-align: end; /* right in English, left in Arabic */
}02Style the underline
Links are underlined by the browser. Instead of removing the underline, make it nicer: a thinner line, a colour and some offset.
a {
text-decoration-color: rgb(100 100 255 / 0.6);
text-decoration-thickness: 2px;
text-underline-offset: 0.2em;
}
.old-price {
text-decoration: line-through;
}03Change the case with CSS, not in the HTML
Write Sign up in the HTML and show it as SIGN UP with CSS. The content stays readable for search engines and screen readers, and the design can change later without editing text.
.label {
text-transform: uppercase;
letter-spacing: 0.08em;
font-size: 0.8rem;
}04Indent and space
Book-style paragraphs indent the first line instead of leaving a gap between paragraphs. p + p (Phase 1: adjacent sibling) targets every paragraph that follows another.
p {
margin: 0;
}
p + p {
text-indent: 1.5em;
}05Add a shadow (or two)
List several shadows separated by commas. The first one in the list is painted on top.
h1 {
text-shadow:
0 1px 0 white,
0 2px 6px rgb(0 0 0 / 0.35);
}06Know what stays and what changes
All of these change only how text is painted. The DOM text, copy-and-paste source and screen reader output start from the same HTML.
Try it yourself
- 1
Change the case
In the second example, change
.uptolowercaseand edit the HTML toHELLO World. Predict both console lines, then run.textContentstill shows exactly what you typed. - 2
Remove the propagated line
In the third example, the
now ₹499text is struck through. Predict: will adding.now { text-decoration: none; }remove it? Try it. Then try.now { display: inline-block; }instead. - 3
Layer shadows
In the first example, add a third shadow
0 0 12px goldat the start of thetext-shadowlist, then move it to the end. Which order puts the glow on top?
Code & diagrams
Every line of CSS here changes only the look. Try `direction: rtl` on `body` and watch `.end` jump to the left.
Preview
Console
Nothing logged yet.`textContent` is the raw text in the DOM. `innerText` is the text as rendered, so it includes the transform. You'll meet both in the JavaScript course.
Preview
Console
Nothing logged yet.Expected console output
textContent: hello world from hectal
innerText: HELLO WORLD FROM HECTAL
capitalize innerText: Hello World From HectalThe inner span reports `none`, yet it is still underlined: the parent's line is propagated across it. Only the inline-block span escapes. In the second line, the price inside the span is struck through for the same reason.
Preview
Console
Nothing logged yet.Expected console output
span.no line: none
.fancy style: wavyBreak it on purpose
Errors are the best teachers. Make each change, read the error, guess what went wrong, then reveal the answer.
Break #1
Centre a box with text-align
Put text-align: center; on a <div class="card"> that has width: 300px, expecting the card to move to the middle of the page.
Break #2
Typing headings in ALL CAPS in the HTML
Write <h2>OUR SERVICES</h2> directly in the HTML.
Myth vs fact
Myth
text-align: center centres anything.
Fact
It centres inline content (text, images, inline-blocks) inside its block. Block-level boxes need auto margins, Flexbox or Grid.
Myth
text-transform: uppercase changes the text.
Fact
Only the painting changes. The DOM, textContent and form values keep the original case. (innerText and copy-and-paste in some browsers show the transformed text, because they work from the rendering.)
Myth
Setting text-decoration: none on a child removes the parent's underline.
Fact
Decorations propagate to inline descendants and can't be cancelled by them. The child has to become an inline-block, or the parent must not be decorated.
Pro corner
Extra depth for experienced readers. New to this? Skip it for now and come back later.
- ▸
text-align: justifystretches word gaps, which creates "rivers" of white space in narrow columns. Pair it withhyphens: auto(next topic) and a correctlang, or avoid it on mobile. WCAG 1.4.8 (AAA) recommends not justifying text. - ▸
text-decorationis not inherited, buttext-decoration-skip-ink,text-underline-offsetandtext-underline-positionare inherited, so setting the offset onbodyaffects every link. - ▸
Changing
letter-spacingbreaks ligatures (joined letter pairs likefi) in most fonts, and in cursive scripts it can disconnect letters. Browsers disable letter spacing effects for some scripts for this reason. - ▸
text-shadowdoesn't affect layout (it never pushes other content away) and is painted cheaply, but animating large blurred text shadows forces repaints every frame. Animateopacityortransformof a separate layer instead.
Remember this
- 1
text-alignplaces the inline content (text, images, inline boxes) inside a block box:left,right,center,justify(stretch lines to touch both edges), and the logical valuesstartandend(which follow the writing direction, sostartis the right edge in Arabic or Hebrew). It is set on the container, not on the text, and it is inherited. It doesn't move the block itself: to centre a box, you use margins or layout (Phase 5 onwards).text-align-lastcontrols the last line of justified text. - 2
text-decorationis a shorthand for lines on text:text-decoration-line(underline,overline,line-through,none),text-decoration-style(solid,double,dotted,dashed,wavy),text-decoration-colorandtext-decoration-thickness. Example:text-decoration: underline wavy red 2px;. Two related properties:text-underline-offsetmoves the underline further from the letters, andtext-decoration-skip-ink(on by default asauto) breaks the underline around letters like g and y. - 3
Decorations are not normally inherited, but they are propagated: an underline set on a parent is drawn across all its inline children, and a child can't remove it with
text-decoration: none. The child can only add its own extra line. To stop it, the child must leave the inline flow (for exampledisplay: inline-block). - 4
text-transformchanges the case of letters when they are drawn:uppercase,lowercase,capitalize(first letter of each word) andnone. The text in the HTML is unchanged, so write content in normal sentence case and let CSS shout. Screen readers read the original text, which matters because some read all-caps words letter by letter. Case rules follow the element'slang: in Turkish,iuppercases toİ. - 5
letter-spacingadds (or removes, with a negative value) space between characters;word-spacingadds space between words. Useemso the spacing grows with the text:letter-spacing: 0.05em. Wide letter spacing suits short capitalised labels; on body text, or on scripts with joined letters like Arabic or Devanagari, it hurts reading.text-indentpushes the first line of a block inwards (a negative value makes a hanging first line). - 6
text-shadowdraws shadows behind the letters:offset-x offset-y blur color, for exampletext-shadow: 2px 2px 4px rgb(0 0 0 / 0.4);. Several shadows can be separated by commas, and the first one is drawn on top. Unlikebox-shadow(Phase 5) there is no spread value and noinset. A subtle shadow can lift text off a busy background image; heavy shadows reduce legibility.
Explain it without notes
What does text-align actually align, and why doesn't it centre a fixed-width box?
Why can't a child element remove its parent's underline with text-decoration: none?
Why is it better to use text-transform: uppercase than to type capitals in the HTML?
What are the parts of a text-shadow value, and how does it differ from box-shadow?
Practice
Style an .eyebrow label: small text, uppercase, wide letter spacing of 0.15em, centred.
Make all links in article have a 2px underline in tomato, placed 0.25em below the text, and a sale price with a line through it.
Give an h1 a crisp white 1px shadow below it and a soft dark shadow behind it.
Trade-offs
- ↔
Justified text looks tidy in print but creates uneven gaps on screens and narrow columns; left (start) aligned text is easier to read for most people, including those with dyslexia.
- ↔
Removing link underlines looks clean but makes links harder to find for people who can't see colour differences; restyling the underline keeps both looks and accessibility.
Done when you can
I can align text with
text-align, includingstart/end, and know it doesn't move boxes.I can style underlines with line, style, colour, thickness and offset.
I can explain decoration propagation.
I use
text-transforminstead of typing capitals in the HTML.I can use
letter-spacing,word-spacingandtext-indentinem.I can write single and layered
text-shadows.