A box is 100 pixels tall, but it holds ten lines of text. The lines that don't fit come out of the bottom of the box and cover whatever is below it, like water spilling from a glass that is too full.
Content that comes out of its box is called overflow. The CSS property overflow decides what happens to that content. The content stays visible outside the box, or it is cut off, or the box gets a scrollbar (a bar for moving the content of the box).
A box with too much inside
This example has a box with a fixed size, and text that is longer than the box. In the code, three dots stand for the rest of the text:
<div class="box">
<p>Some very long text, much longer than the box is tall...</p>
</div>
<style>
.box {
width: 200px;
height: 100px;
overflow: auto;
}
</style>
.boxselects the elements withclass="box".width: 200pxandheight: 100pxfix the box's width and height. Because the height is fixed, the box doesn't grow when the text is long. As a result, the text that doesn't fit overflows.overflow: auto;is the new declaration. It means: if the content doesn't fit, give this box a scrollbar.
The result: a box of 200 by 100 pixels. You can scroll the text inside it down to the end. Without the overflow line, the text comes out of the bottom of the box.
In the exercises, the .box { ... } rule is ready for you. You only add one overflow line inside that rule's curly braces, before the }. You don't need a new rule.
The four overflow values
visible: the default value. Content that overflows stays visible outside the box.hidden: content that overflows is cut off at the edge of the box. There is no scrollbar, so the cut part can't be read at all.scroll: there is always a scrollbar, even when the content fits. (On a phone, the scrollbar usually shows only while you scroll.)auto: a scrollbar appears only when the content doesn't fit.
So a box with overflow: auto and short text has no scrollbar. That is why auto is the safe choice for a box whose content is sometimes short and sometimes long. Use hidden when it is fine to lose the part that gets cut. To keep a title on one line, with three dots where the text is cut, write white-space: nowrap; overflow: hidden; text-overflow: ellipsis; on a box whose width is fixed. nowrap keeps the text from going to a new line, and ellipsis is the name for those three dots.
To control only one direction, use overflow-x (horizontal, left to right) or overflow-y (vertical, top to bottom). Both take the same values: visible, hidden, scroll or auto.
⚠️ Overflow that does nothing, or cuts too much
- The
overflow: auto;line has to be inside the{ }of.box. If you write it outside the curly braces, the browser ignores it. - The browser doesn't know
overflow: hiden(one letter d is missing), so it quietly skips it. The box then goes back to the default value,visible. - A box whose
heightisn't fixed grows with its content. No content overflows, sooverflowseems to do nothing. hiddenalso cuts off parts that are meant to come out of the box. Two examples: a tooltip (a small note that appears next to an element) and a dropdown (a menu that opens downward).
TL;DR: The overflow property decides what happens when a box's content is bigger than the box. It has four values. visible (the default) lets the content come out of the box. hidden cuts the content off at the edge of the box. scroll always adds a scrollbar (a bar for moving the content of the box). auto adds a scrollbar only when the content doesn't fit. Write overflow inside the rule for that box, for example .box { overflow: auto; }, and make sure the box has a fixed size, for example with height: 100px. A box with no height grows with its content, so no content comes out of the box.
⚡ A scrollbar when needed
Write:
.box {
width: 200px;
height: 100px;
overflow: auto;
}
Meaning: height: 100px fixes the box's height, so text that is longer than the box overflows: it comes out of the bottom of the box. overflow: auto gives the box a scrollbar (a bar for moving the content of the box), but only when the content doesn't fit.
In the exercises, the .box { ... } rule is already there. You only add one overflow line inside its curly braces, before the }.
⚡ Let it out, cut it, or scroll
Guess first: does a box with overflow: auto and short text have a scrollbar?
No. auto only shows a scrollbar when some content overflows. The value that always adds a scrollbar is scroll.
Meaning: overflow has three more values. visible (the default) lets content that overflows come out of the box. hidden cuts that content off at the box's edge, with no scrollbar. scroll always adds a scrollbar (on a phone it shows only while you scroll).
⚡ Overflow that does nothing
Guess first: a box with no height gets overflow: hidden. Is the text cut off?
No. A box with no height grows with its content, so no content overflows.
Meaning: overflow only has a visible effect when the box has a fixed size, for example from height: 100px.