×

css · level 1 · unit 6 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

lets a box stretch, but only so far.

A width in % is a share of the parent's width, so width: 50% takes half the space and changes as the window changes. That suits phones and big screens alike.

max-width sets a limit instead: the box stretches to fit, but never grows past it. max-width: 600px stops long lines of text getting too wide to read.

To centre a box, give it a width or max-width and . The 0 is for the top and bottom. auto lets the browser share the space left over equally between left and right.

<div class="page">This box stretches with the window, stops at 400px and sits in the middle.</div>
.page {
  max-width: 400px;
  margin: 0 auto;
  padding: 16px;
  background-color: mintcream;
}

Press show my page to see it here.

If your window is narrower than 400px, the box shrinks to fit.