What is Overflow in CSS: Explanation and Examples
overflow controls what happens when content inside a box is too big to fit. It decides if extra content is clipped, scrollable, or visible outside the box.How It Works
Imagine you have a box, like a photo frame, and you want to put a picture inside it. If the picture is bigger than the frame, you need to decide what to do with the parts that don't fit. This is exactly what overflow does in CSS for web elements.
When content inside an element is larger than the element's size, overflow tells the browser whether to hide the extra content, show scroll bars so you can move to see it, or let it spill outside the box. This helps keep your page neat or lets users access all content.
Example
This example shows a box with fixed size and content that is bigger. The overflow property is set to scroll so scroll bars appear to see all content.
html, body {
height: 100%;
margin: 0;
font-family: Arial, sans-serif;
}
.container {
width: 200px;
height: 100px;
border: 2px solid #333;
overflow: scroll;
padding: 10px;
box-sizing: border-box;
}
.content {
width: 300px;
height: 150px;
background-color: #cce5ff;
padding: 5px;
}
/* HTML structure */
/* <div class="container">
<div class="content">This content is bigger than the container box, so scroll bars appear.</div>
</div> */When to Use
Use overflow when you want to control how extra content behaves inside a fixed-size area. For example:
- Creating scrollable boxes for text or images.
- Hiding parts of content that should not be visible.
- Preventing layout breakage when content is too large.
This is useful in menus, cards, modals, or any place where space is limited but content might vary.
Key Points
overflowcontrols content that is too big for its container.- Common values:
visible,hidden,scroll, andauto. visibleshows overflow outside the box;hiddenclips it.scrollalways shows scroll bars;autoshows them only if needed.- Helps keep layouts clean and user-friendly.
Key Takeaways
overflow manages extra content inside fixed-size containers.scroll or auto to add scroll bars for large content.hidden clips content to keep it inside the box.overflow value improves page layout and usability.