Skip to main content

Overflow in Specific Directions

The overflow-x and overflow-y properties let you control overflow separately for the horizontal and vertical directions.


CSS overflow-x and overflow-y

The overflow-x and overflow-y properties set what happens to overflow just horizontally or just vertically (or both).

  • overflow-x handles the left and right edges of the content.
  • overflow-y handles the top and bottom edges of the content.

Example

The horizontal scrollbar is hidden and a vertical scrollbar is added.

Prompt
An HTML page that hides the horizontal scrollbar and shows a vertical one. In an internal <style> block, give a <div> a width, height, and coral background, with overflow-x hidden and overflow-y scroll. Put more text inside than fits vertically.
Output: complete HTML page

Common Use Cases

These properties help when you need different scroll behavior in each direction, for example: overflow-x: auto for wide tables, overflow-y: scroll for chat windows, and overflow-x: auto for code blocks with long lines.

Example

Only a horizontal scrollbar is shown.

Prompt
An HTML page that shows only a horizontal scrollbar. In an internal <style> block, give a <div> a width, height, and coral background, with overflow-x scroll and overflow-y hidden, and keep the text on one line (white-space nowrap) so it overflows sideways.
Output: complete HTML page