Overflow
The overflow property controls how content that overflows an element’s box is handled (displayed, hidden, or scrollable).
Main property
Section titled “Main property”| Property | Description |
|---|---|
overflow | Defines the overall overflow behavior (both horizontal and vertical). |
overflow-x | Defines horizontal overflow behavior only. |
overflow-y | Defines vertical overflow behavior only. |
Possible values
Section titled “Possible values”| Value | Description |
|---|---|
visible (default) | Overflowing content remains visible. |
hidden | Overflowing content is hidden (not accessible without scrolling). |
scroll | Adds scrollbars (always visible, even if not needed). |
auto | Adds scrollbars only when necessary. |
clip | Clips content without scrollbars (stricter than hidden). |
Example
Section titled “Example”.box { width: 200px; height: 100px; border: 1px solid #333; overflow: auto;}Here, if the content overflows the box, a scrollbar is displayed automatically.
.container { width: 300px; height: 150px; overflow-x: scroll; /* forced horizontal scrollbar */ overflow-y: hidden; /* vertical content hidden */}Special cases
Section titled “Special cases”1. overflow: clip
Section titled “1. overflow: clip”Clips the overflowing content without showing scrollbars.
Unlike hidden, it is not possible to scroll the content by other means (e.g., keyboard, programmatic scrolling).
.box { overflow: clip;}2. Interaction with scrollbars
Section titled “2. Interaction with scrollbars”overflow: auto and overflow: scroll can display a custom scrollbar via scrollbar-width (Firefox) or ::-webkit-scrollbar (Chrome/Safari/Edge).
overflow: hidden or clip remove any scrollbar.
Best practices
Section titled “Best practices”- Prefer
overflow: autofor natural behavior. - Use
overflow: hiddenorcliponly if you want to block access to overflowing content. - Always test with dynamic content (long text, large images) to avoid unexpected clipping.
Key takeaways
Section titled “Key takeaways”overflowcontrols overflowing content by hiding, clipping, or enabling scrolling.overflow-xandoverflow-yprovide independent horizontal/vertical control.clipis a stricter version ofhidden, with no scrolling possible.overflowis often combined with scrollbar customization.