Every size in CSS needs a unit, like the px you have used so far. Different units answer one question differently: "compared to what?"
flag
What you'll learn
The difference between fixed sizes (px) and sizes that follow something else.
How %, em and rem size things compared to a parent or the page.
How vh and vw size things compared to the browser window.
waving_handLet's measure things
A unit is the word after a number, like px in 16px. Some units are fixed: they stay the same everywhere.
Others are relative: they are a part of something else, like the box an element sits in or the size of the screen. Relative units help a page fit both phones and computers.
straighten
Think of asking a carpenter for a shelf. You can give an exact length: "140 centimeters". That shelf is the same length in any room, like px.
Or you can say "half the wall". In a small room, the shelf comes out short. In a big room, it comes out long. That's like %: the size depends on what it sits in.
Small room
width: 140px;
width: 50%;
Big room
width: 140px;
width: 50%;
Now let's see the CSS ↓
Small room
width: 140px;
width: 50%;
Big room
width: 140px;
width: 50%;
The brown shelf is fixed: 140 pixels in both rooms. The green shelf is relative: half of whatever wall it's on.
searchA closer look
px: pixels
px is a fixed size. 16px is 16 pixels on a phone and on a big monitor. It's a good choice for borders and small, exact spaces.
For big widths, fixed sizes can be a problem: a 600px box is too wide for most phones.
%: part of the parent
% is a part of the box the element sits in, called its parent. width: 50%; is half the parent's width, so the box gets narrower on a phone and wider on a computer.
em and rem: compared to text size
em and rem are based on text size. rem means "root em": it's compared to the text size of the whole page, set on <html>, which is usually 16px. So 1.5rem is 24px everywhere on the page.
em is compared to the element's own text size. That's handy for padding: padding: 0.5em 1em; on a button grows when the button's text grows.
Watch out with em in font-size: there it means the parent's text size, so boxes inside boxes can keep getting bigger.
vh and vw: parts of the window
vh and vw are parts of the browser window, which is called the viewport. 1vh is 1% of the window's height, and 1vw is 1% of its width.
min-height: 100vh; makes a section at least as tall as the screen. A title in vw grows and shrinks as the window changes width.
Which one to use?
Text size:rem, so all text follows one page setting.
Borders and small spaces:px.
Widths of boxes:%, often with a max-width in px or rem.
Padding that follows text size:em.
Full-screen sections:vh and vw.
constructionLet's build it
Let's size a Front Page Bytes page using a different unit in each step. Each code box opens on the CSS tab with the new lines highlighted. Click HTML to see the page being styled.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <div class="story"> <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 class="button" href="/news/phones">Read more</a> </div> </body></html>
What you see
description(no title)
The border, padding and width are all in pixels. The story box is exactly 300 pixels wide, however wide the mini browser is.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <div class="story"> <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 class="button" href="/news/phones">Read more</a> </div> </body></html>
What you see
description(no title)
width: 80%; makes the story 80% as wide as its parent, the body. Try this lesson on a phone and on a computer: the box changes width to fit.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <div class="story"> <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 class="button" href="/news/phones">Read more</a> </div> </body></html>
What you see
description(no title)
1.125rem is 1.125 times the page's text size: 1.125 × 16 = 18 pixels. If the page's text size ever changes, this paragraph changes with it.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <div class="story"> <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 class="button" href="/news/phones">Read more</a> </div> </body></html>
What you see
description(no title)
The button's padding is 0.5em 1em: half the button's text size on the top and bottom, and one full text size on the sides. Make the button text bigger, and the padding grows with it, so the button keeps its shape.
5Step 5: A banner as tall as part of the screen: vh
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <div class="story"> <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 class="button" href="/news/phones">Read more</a> </div> </body></html>
What you see
description(no title)
min-height: 30vh; makes the site name's banner at least 30% as tall as the window. In these lessons, the "window" is the mini browser.
6Step 6: A title that follows the screen width: vw
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <div class="story"> <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 class="button" href="/news/phones">Read more</a> </div> </body></html>
What you see
description(no title)
font-size: 9vw; makes the title 9% of the window's width. On a wide screen it's big, and on a phone it shrinks, so it fits on one line.
sticky_note_2Quick notes
pxFixed pixels
.card { border: 3px solid black; }
How to use
16pxExactly 16 pixels, on every screen.
Good for borders and small, exact spaces.
0Zero needs no unit.
Examples
A thin border.
.card { border: 2px solid #11141A;}
A small exact gap under a heading.
h2 { margin-bottom: 8px;}
%Part of the parent
.story { width: 80%; }
How to use
50%Half the size of the box it sits in (its parent).
100%As wide as the parent.
The size changes when the parent changes, like on a phone.
Examples
A box that fills most of its parent.
.story { width: 80%;}
Two side-by-side boxes that share the space.
.half { width: 50%;}
A picture that fits any screen.
img { max-width: 100%;}
emCompared to this text size
.button { padding: 0.5em 1em; }
How to use
1emThe same as the text size of this element.
2emTwice that size.
For font-size, 1em is the parent's text size, so em values inside each other multiply.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <div class="story"> <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 class="button" href="/news/phones">Read more</a> </div> </body></html>
What you see
description(no title)
16 px with a space is not a size the browser understands, so it skips the line. The story loses its padding, and the text touches the border.
tips_and_updatesTip: Write the number and unit together: padding: 16px;
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <div class="story"> <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 class="button" href="/news/phones">Read more</a> </div> </body></html>
What you see
description(no title)
The story's text is 1.5 times the page size: 24px. The paragraph inside it is 1.5 times that: 36px. Each em is measured from its parent, so the sizes multiply.
tips_and_updatesTip: Use rem for font sizes, so each size is measured from the same place.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <div class="story"> <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 class="button" href="/news/phones">Read more</a> </div> </body></html>
What you see
description(no title)
100vw is the whole window, but the box starts after the page's margin, and padding and border are added on top. So it's wider than the window and sticks out on the right.
tips_and_updatesTip: Use width: 100%; to fill the parent. It counts the space the box really has.
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.
Make .card 90% wide with width and a % value.not done yet
Give .card padding in em, like 1em.not done yet
Set the .title font-size in rem, like 2rem.not done yet
Give .card a min-height in vh, like 40vh.not done yet