Height & Width — CSS

width sets an element's width, height sets its height. Use px for an exact size, % for a size that follows the parent, vw and vh for a size that follows the…

You make an empty <div>, save the file and refresh the page. Nothing shows on screen. The <div> is there, but its height is 0 and it has no colour, so you can't see it.

To see it, set its size yourself: width for how wide, height for how tall. The value can be an exact size like 200px, a percentage of its parent's size like 50%, or auto, which means the browser calculates the size itself.

A box with an exact size

This example makes one green box:

<div class="box"></div>

<style>
.box {
  width: 200px;
  height: 100px;
  background-color: lightgreen;
}
</style>

The result: a green rectangle, 200 × 100px. For a square, give width and height the same number. An empty <div> with no height has a height of 0, because by default an element's height follows its content, and this <div> has none.

Colour names in CSS are English: orange, red, lightblue. The browser throws away a name it doesn't know (like the misspelled ornage), so the colour doesn't change.

Percentages come from the parent

width: 50% means half the width of its parent, not half the width of the screen. A width: 50% box inside a 240px wide parent is 120px wide. If the parent gets wider, the box gets wider too.

Other values you will see a lot:

Lower and upper limits

This rule makes an article as wide as its parent, but with an upper limit:

.article {
  width: 100%;
  max-width: 600px;
}

max-width sets an upper limit: the article can't be wider than 600px. On a phone, where the screen is narrower than 600px, this article is as wide as the screen. On a wide screen, its width stops at 600px. min-width is the opposite: a lower limit, so an element never gets narrower than a certain size. Height has the same pair: min-height and max-height.

⚠️ Sizes that do nothing, or spill out

Options for a section as tall as the screen:

Value Details
100svh The screen height with the browser bars showing, so it always fits. The safest choice for the opening section of a page.
100dvh Changes when the browser bars appear or hide, so the element can change size while the page scrolls.
100vh Fine on a laptop screen, which has no bars that appear and hide.

Write min-height: 100svh, not height. With min-height, the element may grow taller than the screen, so longer content still fits.

TL;DR: width sets an element's width, height sets its height. Use px for an exact size, % for a size that follows the parent, vw and vh for a size that follows the screen, or leave it on auto so the browser calculates it. max-width sets the biggest width allowed and min-width the smallest (for height: max-height and min-height). Colour names are written in English, like orange.

⚡ A box with an exact size

Write:

.box {
  width: 200px;
  height: 100px;
  background-color: lightgreen;
}
Guess first: .box is an empty <div>. Without height, how tall is it?

Its height is 0. By default an element's height follows its content, and this <div> has none.

Meaning: width sets the width, height sets the height. Without a background colour, an empty <div> can't be seen at all.

Colour names are written in English, like orange.

⚡ Percentages of the parent

Meaning: width: 50% means half the width of its parent, not half the width of the screen. For sizes that follow the screen, use the units vw and vh: 100vw is as wide as the screen, 100vh is as tall as the screen.

⚡ A width limit

Write:

.article {
  width: 100%;
  max-width: 600px;
}

Meaning: width: 100% makes the article as wide as its parent. max-width sets an upper limit: on a phone the article is as wide as the screen, but on a wide screen its width stops at 600px. min-width is the opposite: a lower limit, so an element never gets narrower than a certain size.

What you will learn