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>
.boxis the selector: the declarations inside{ }apply to the element withclass="box".width: 200pxmakes the box 200 pixels wide.height: 100pxmakes the box 100 pixels tall.background-color: lightgreengives it a light green background. Without a background colour, an empty<div>can't be seen at all.
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:
vwandvh: percentages of the screen size (more exactly the viewport, the area of the browser window that shows the page).1vwis 1% of the screen's width, so100vwis as wide as the screen.100vhis as tall as the screen.auto: the default. A block element (an element that takes a whole line, like<div>) is as wide as its parent, and its height follows its content.
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
width: 100%plus padding. With the defaultbox-sizing: content-box, padding is added outside thewidth. The box becomes wider than its parent and spills out of it. Usewidth: autoorbox-sizing: border-box.height: 100%does nothing. A percentage height is calculated from the parent's height, so the parent needs a fixed height. If the parent's height isauto,height: 100%is the same as not writing it.- Sizes on a
<span>don't work. Inline elements like<span>ignorewidthandheight. To make the sizes apply, give the elementdisplay: inline-blockorblock(covered in the display lesson). 100vhgets cut off on phones. On a phone,vhis measured from the screen with the browser's address bar hidden. When the bar is showing, it covers the bottom of an element that is100vhtall, and a button often sits there.
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.