Most of a web page is words. A few text properties are enough to make those words clear, neat and easy to read.
flag
What you'll learn
How to change the color of text.
How to line text up and change its letters to capitals.
How to space letters out, add or remove lines, and give text a shadow.
waving_handLet's style some words
In HTML, you choose the words. In CSS, you choose how those words look: their color, where each line sits, and small touches like capitals, spacing, lines and shadows.
These are called text properties.
storefront
Think of a tarpaulin sign for a store's grand opening. The words are already decided, like the HTML.
The sign maker picks the ink color, centers each line, prints the title in capital letters, spreads the letters apart so people can read them from far away,
adds a shadow so the title pops, and crosses out the old price.
Grand Openingtext-transform: uppercase;letter-spacing: 6px;text-shadow: 3px 3px 0 #FFD043;
Every line on the sign is centered with text-align. The title gets three properties at once: capitals, wide spacing and a shadow.
searchA closer look
Text color is just color
The property for text color is color, not font-color or text-color. You can use the same color names, hex codes and rgb() values from CSS Lesson 1.
Text color is passed down: set color on body, and every heading and paragraph inside takes that color, unless another rule gives it a different one. This is called inheritance.
Lining up text
text-align lines up the text inside a box: left, center, right or justify.
It works on boxes that hold lines of text, like headings and paragraphs. A link sits inside a line, so to center a link, center the line it sits in.
Lines on text
text-decoration adds a line: underline (below), line-through (through the middle) or overline (above). Links are underlined by the browser, and text-decoration: none; removes that line.
If you remove the underline, keep links easy to spot with a different color.
Capital letters without retyping
text-transform changes only how the letters look: uppercase, lowercase or capitalize (first letter of each word). The HTML keeps the words exactly as you typed them, so you can change the style later without retyping anything.
Letter spacing
letter-spacing adds space between letters, like 2px. Short labels in capital letters often look better with a little extra space. A negative value, like -1px, pulls letters closer.
Text shadows
text-shadow takes up to four parts: how far right, how far down, how blurry, and the color. 2px 2px 0 #FFD043 is a sharp yellow shadow down and to the right.
Use shadows lightly, mostly on big titles. On small text, a shadow can make words harder to read.
constructionLet's build it
Let's style the words on the Front Page Bytes home page: the site name, a tagline, a story and a link. Each code box opens on the CSS tab with the new lines highlighted. Click HTML to see the page being styled.
1Step 1: Choose text colors
Your code
body { background-color: #F0F7FF; color: #3A414C;}h1 { color: #1E6FA0;}
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <p class="tagline">Your daily tech news, made simple.</p> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery and a sharper camera.</p> <a href="/news/phones">Read the full story</a> </body></html>
What you see
description(no title)
color: #3A414C; on body makes all the text a soft dark gray. The h1 rule then gives the site name its own blue. The h1 rule wins because it points at the heading directly.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <p class="tagline">Your daily tech news, made simple.</p> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery and a sharper camera.</p> <a href="/news/phones">Read the full story</a> </body></html>
What you see
description(no title)
text-align: center; moves the lines of the site name and the tagline to the middle. The story below stays on the left, because its rules don't change.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <p class="tagline">Your daily tech news, made simple.</p> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery and a sharper camera.</p> <a href="/news/phones">Read the full story</a> </body></html>
What you see
description(no title)
text-transform: uppercase; shows the tagline in capital letters. Open the HTML tab: the words there are still written normally.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <p class="tagline">Your daily tech news, made simple.</p> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery and a sharper camera.</p> <a href="/news/phones">Read the full story</a> </body></html>
What you see
description(no title)
letter-spacing: 2px; adds 2 pixels between every letter. Capital letters close together can feel crowded, and a little space makes them easier to read.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <p class="tagline">Your daily tech news, made simple.</p> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery and a sharper camera.</p> <a href="/news/phones">Read the full story</a> </body></html>
What you see
description(no title)
text-decoration: none; removes the link's underline, and color makes it the same blue as the title. The blue still shows people that it's something to click.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <p class="tagline">Your daily tech news, made simple.</p> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery and a sharper camera.</p> <a href="/news/phones">Read the full story</a> </body></html>
What you see
description(no title)
text-shadow: 2px 2px 0 #FFD043; puts a sharp yellow copy of the title 2 pixels right and 2 pixels down. The 0 means no blur, which gives a bold cartoon look.
sticky_note_2Quick notes
colorText color
h1 { color: #1E6FA0; }
How to use
skyblueA color name.
#1E6FA0A hex code for an exact shade.
rgb(30, 111, 160)Red, green and blue, each from 0 to 255.
Set it on body, and all the text inside takes the same color unless another rule changes it.
Examples
Soft dark text for the whole page.
body { color: #3A414C;}
A blue heading.
h1 { color: #1E6FA0;}
Red text for a warning.
.warning { color: #FF4B4B;}
text-alignWhere the lines sit
h1 { text-align: center; }
How to use
leftLines start at the left edge. This is the usual setting.
centerLines sit in the middle.
rightLines end at the right edge.
justifyLines stretch to touch both edges, like a newspaper column.
Examples
A centered page title.
h1 { text-align: center;}
A date on the right side.
.date { text-align: right;}
Neat edges on a long story.
.story { text-align: justify;}
text-decorationLines on text
a { text-decoration: none; }
How to use
underlineA line under the text.
line-throughA line through the middle, like a crossed-out price.
overlineA line above the text.
noneNo line. Often used to remove the underline from links.
Examples
Links without an underline.
a { text-decoration: none;}
A crossed-out old price.
.old-price { text-decoration: line-through;}
Underline a word that is not a link.
.key-word { text-decoration: underline;}
text-transformCapital or small letters
.tagline { text-transform: uppercase; }
How to use
uppercaseALL CAPITAL LETTERS.
lowercaseall small letters.
capitalizeThe First Letter Of Each Word.
Only the look changes. The words in the HTML stay as you typed them.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <p class="tagline">Your daily tech news, made simple.</p> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery and a sharper camera.</p> <a href="/news/phones">Read the full story</a> </body></html>
What you see
description(no title)
middle isn't a value of text-align, so the browser skips the whole line and the title stays on the left.
tips_and_updatesTip: Use center: text-align: center;
error
Writing font-color
The mistake
body { background-color: #F0F7FF;}h1 { font-color: #1E6FA0;}
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <p class="tagline">Your daily tech news, made simple.</p> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery and a sharper camera.</p> <a href="/news/phones">Read the full story</a> </body></html>
What you see
description(no title)
There is no property called font-color. The browser skips it, so the title keeps the usual black.
tips_and_updatesTip: The property is simply color: color: #1E6FA0;
error
Centering a link by itself
The mistake
body { background-color: #F0F7FF; color: #3A414C;}a { text-align: center;}
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <p class="tagline">Your daily tech news, made simple.</p> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery and a sharper camera.</p> <a href="/news/phones">Read the full story</a> </body></html>
What you see
description(no title)
A link sits inside a line of text, and text-align only lines up the lines inside a box. Setting it on the link itself changes nothing, so the link stays on the left.
tips_and_updatesTip: Put text-align: center; on the box that holds the line, like the body or a paragraph around the link.
quizTest your knowledge
Edit the CSS on the left. Open the HTML tab to see the class names you can use. The mini browser updates as you type, and each goal gets a check mark when it's done.
auto_awesomeNo clicking needed. Each goal gets a check mark by itself once your code does it.
Center the .title heading.not done yet
Show the .title heading in capital letters.not done yet
Change the color of the .note text.not done yet
Give the .title heading a text shadow.not done yet