Encodera

Thanks for reading!

Donate to the site — we work hard for you.

HTML & CSS reference

overflow

What to do with content that does not fit into the element.

Syntax

overflow: value;

Values

ParameterTypeDescription
visiblekeywordIt sticks out (the default).
hiddenkeywordIt is clipped; it can still be scrolled with code.
autokeywordA scrollbar appears when needed.
scrollkeywordScrollbars are always shown.
clipkeywordClipped with no possibility to scroll.

Examples

Different behavior for overflowing text:

<style>
  div { width: 160px; height: 50px; border: 1px solid gray; margin-bottom: 30px; }
</style>
 
<div style="overflow: visible;">visible: the text sticks out of the block and overlaps what is below.</div>
<div style="overflow: hidden;">hidden: the extra text is cut off and cannot be seen.</div>
<div style="overflow: auto;">auto: a scrollbar appears only when the text does not fit.</div>

Horizontal scrolling only — overflow-x:

<div style="overflow-x: auto; white-space: nowrap; width: 220px; border: 1px solid gray;">
  <img src="/media/photo-200x200.jpg" alt="" width="100">
  <img src="/media/photo-200x200.jpg" alt="" width="100">
  <img src="/media/photo-200x200.jpg" alt="" width="100">
</div>

Notes

overflow other than visible breaks position: sticky for children. When a whole page scrolls horizontally, look for an element wider than the screen rather than hiding it with overflow-x: hidden on body.

See also

  • text-overflow — How text that does not fit is cut: abruptly or with an ellipsis.
  • max-height — The maximum height: the element grows with its content only up to this limit.
  • overflow-wrap — Allows a long word to break so that it does not stick out of its block.

Related recipes

In the tutorial

  • Overflow and layers — What to do when content does not fit into a block — overflow, scrolling, an ellipsis — and how z-index decides which element lies on top.

Thanks for reading!

Donate to the site — we work hard for you.