Text Decoration
Table of Contents + β
In the last lesson you learned about Text Alignment. That was about where your text sits on the line. Now letβs look at how to add lines on your text, like the underline below a link.
π¨ What is Text Decoration?
When you read a webpage, some words have a line under them. You click them and they take you somewhere. Those are links. That line under a link is a decoration.
Text decoration is a CSS feature that adds a line on your text. The line can go under the text, over the text, or right through the middle.
So text decoration is not about the words themselves. It is about a line drawn on the words. You decide where the line goes and what it looks like.
Think of a teacher marking a worksheet. The teacher draws a line under an important word. The teacher crosses out a wrong answer with a line through it. The word stays the same. Only the line is new. That is what text decoration does.
π€ Why Do We Need It?
Here is the real problem. By default a link comes with a blue color and an underline. But sometimes you do not want that underline. Or you have plain text and you DO want a line on it. So you need a way to add or remove that line yourself.
Text decoration solves this for you. Here is what it lets you do.
- Remove the underline from links so they look clean, like the menu at the top of a website.
- Add an underline to normal text to make one word stand out.
- Cross out a price with a line through it, like a sale price on Amazon.
- Show the old text and the new text in an edit, with the old one crossed out.
π§© The Syntax
The main property is text-decoration. You give it a value that says what line you want.
The short way looks like this. You write the property, then the line you want.
p { text-decoration: underline;}The common values are easy to remember.
| Value | What it does |
| underline | draws a line under the text |
| overline | draws a line above the text |
| line-through | draws a line through the middle of the text |
| none | removes any line, even on links |
That word none is the one you will use the most. It is how you take the underline off a link.
βοΈ A Simple Example
Letβs try all the lines at once so you can see the difference. Each paragraph gets a different decoration.
.under { text-decoration: underline;}.over { text-decoration: overline;}.through { text-decoration: line-through;}And here is the HTML that uses these classes. Each line of text picks one class.
<p class="under">This text has a line under it.</p><p class="over">This text has a line above it.</p><p class="through">This text has a line through it.</p>What you see
Three lines of text. The first has a line below it, like a link. The second has a line floating above the words. The third has a line cutting straight through the middle, like a crossed-out word.
π A Real Example: A Sale Price
Now a real one you see every day. On a shopping site the old price is crossed out and the new price sits next to it. You can build that with line-through.
Here we cross out the old price and keep the new price normal.
.old-price { text-decoration: line-through; color: gray;}.new-price { color: red;}The HTML puts the two prices side by side. The old one gets the crossed-out look.
<p> <span class="old-price">$50</span> <span class="new-price">$30</span></p>What you see
The price $50 in gray with a line through it. Right next to it is $30 in red. It reads like a real discount, the same way Amazon shows a deal.
π Removing the Underline from Links
This is the one you will reach for again and again. Links come with an underline by default. In a top menu that underline looks messy. So you turn it off with none.
This rule removes the underline from every link.
a { text-decoration: none;}What you see
The links lose their underline. They look like clean plain words in a row, the way menu items look at the top of YouTube or Netflix.
Keep links clear
If you remove the underline, give your links another way to stand out. A different color or bold text helps. Otherwise people may not know a word is clickable.
π¨ Styling the Line Itself
You can do more than pick where the line goes. You can change the lineβs color, its style, and how thick it is. The full text-decoration property lets you set them all together.
This makes a wavy red line under the text, a bit thicker than normal.
.spell-error { text-decoration: underline wavy red;}That wavy look is the same squiggly red line you see under a spelling mistake in a text box. Other styles you can use are solid (a normal straight line), dotted, and dashed.
You can also set these one at a time with their own properties.
| Property | What it controls |
| text-decoration-line | where the line goes (underline, overline, line-through) |
| text-decoration-color | the color of the line |
| text-decoration-style | solid, wavy, dotted, or dashed |
| text-decoration-thickness | how thick the line is |
β οΈ Common Mistakes
A few small slips trip up beginners. Watch out for these.
- Writing the value wrong. It is
line-through, notlinethroughorstrikethrough. The spelling has to match exactly. - Forgetting that
noneis your tool for links. People try to hide the underline in strange ways when one line,text-decoration: none;, does it. - Removing the underline from normal text by accident. An underline on plain text can make readers think it is a link and try to click it.
- Mixing up
overlineandunderline. Over means above. Under means below. Say it slowly and you will not swap them.
β Best Practices
Keep these habits and your text will look clean and clear.
- Use
line-throughfor old prices and finished tasks. That is what readers expect it to mean. - Remove link underlines in menus. But keep them inside long paragraphs so readers can spot links while reading.
- If you take an underline off a link, add color or bold so it still looks clickable.
- Save
overlinefor special cases. It is rare, so it can look odd on normal text.
π§© What Youβve Learned
You picked up a small but handy CSS skill. Here is the short version.
- β
text-decorationadds a line on your text: under, over, or through it. - β
underline,overline,line-through, andnoneare the main values. - β
noneremoves the underline from links, which is great for menus. - β
line-throughis perfect for crossed-out sale prices and done tasks. - β
You can style the lineβs color, style (like
wavy), and thickness.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
Which value removes the underline from a link?
Why: text-decoration: none; removes any line, including the default underline on links.
- 2
Which value crosses out text with a line through the middle?
Why: line-through draws a line through the middle. 'strikethrough' is not a real CSS value.
- 3
You want a line ABOVE the text. Which value do you use?
Why: overline draws the line above the text. Over means above, under means below.
- 4
After removing the underline from a menu link, what is a good next step?
Why: Without the underline, readers need another clue like color or bold to know the word is a link.
π Whatβs Next?
Nice work. You can now add and remove lines on your text with confidence. Next we move from text to the space around your boxes, starting with the margin.