Home 〉 CSS Tutorials 〉 CSS Overflow Property Explained with Examples | Control Overflow in CSS
CSS Overflow Property Explained with Examples | Control Overflow in CSS !
Ever had content spill outside its container? The `overflow` property lets you handle that using values like `visible`, `hidden`, `scroll`, and `auto`. This is key for scrollable boxes, tooltips, and responsive designs. Learn how to use overflow with fixed-height containers.
In CSS, the overflow property is used to control how content that overflows an element's box is handled. This is especially useful when dealing with elements that contain more content than can fit within their specified height or width.
Let's explore the 'overflow' property in CSS and see how to handle overflowing content effectively!
The 'overflow' property is written as:
Syntax ✍
selector {
overflow: visible | hidden | scroll | auto;
}
Available Values:
By default, content will overflow outside the container if it's too large.
<!DOCTYPE html>
<html>
<body>
<div style="overflow: visible; width: 200px; height: 100px; border: 1px solid black;">
<p>This is a large content that overflows outside the container. Lorem ipsum dolor, sit amet consectetur adipisicing elit. Officiis, praesentium? At autem, qui mollitia animi doloremque repellendus vel cumque id? Eveniet autem ut, obcaecati excepturi architecto ratione cumque repudiandae ab?</p>
</div>
</body>
</html>
output 📌
This is a large content that overflows outside the container. Lorem ipsum dolor, sit amet consectetur adipisicing elit. Officiis, praesentium? At autem, qui mollitia animi doloremque repellendus vel cumque id? Eveniet autem ut, obcaecati excepturi architecto ratione cumque repudiandae ab?
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Output: The paragraph text will overflow and be visible outside the container.
Clips the overflowing content, hiding it from view while maintaining the element's dimensions.
<!DOCTYPE html>
<html>
<body>
<div style="overflow: hidden; width: 200px; height: 100px; border: 1px solid black;">
<p>This content is hidden when it overflows. Lorem ipsum dolor, sit amet consectetur adipisicing elit. Officiis, praesentium? At autem, qui mollitia animi doloremque repellendus vel cumque id? Eveniet autem ut, obcaecati excepturi architecto ratione cumque repudiandae ab?</p>
</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Output: The content will be clipped and anything that exceeds the container's boundaries will not be visible.
Displays scrollbars whether the content overflows or not, allowing users to scroll through the content.
<!DOCTYPE html>
<html>
<body>
<div style="overflow: scroll; width: 200px; height: 100px; border: 1px solid black;">
<p>This is a long content that will overflow and show scrollbars. Lorem ipsum dolor, sit amet consectetur adipisicing elit. Officiis, praesentium? At autem, qui mollitia animi doloremque repellendus vel cumque id? Eveniet autem ut, obcaecati excepturi architecto ratione cumque repudiandae ab?</p>
</div>
</body>
</html>
output 📌
This is a long content that will overflow and show scrollbars. Lorem ipsum dolor, sit amet consectetur adipisicing elit. Officiis, praesentium? At autem, qui mollitia animi doloremque repellendus vel cumque id? Eveniet autem ut, obcaecati excepturi architecto ratione cumque repudiandae ab?
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Output: The element will have scrollbars, even if the content doesn't overflow. Users can scroll to see the content.
Shows scrollbars only when the content overflows.
<!DOCTYPE html>
<html>
<body>
<div style="overflow: auto; width: 200px; height: 100px; border: 1px solid black;">
<p>This is a large content that will only show scrollbars when necessary. Lorem ipsum dolor, sit amet consectetur adipisicing elit. Officiis, praesentium? At autem, qui mollitia animi doloremque repellendus vel cumque id? Eveniet autem ut, obcaecati excepturi architecto ratione cumque repudiandae ab?</p>
</div>
</body>
</html>
output 📌
This is a large content that will only show scrollbars when necessary. Lorem ipsum dolor, sit amet consectetur adipisicing elit. Officiis, praesentium? At autem, qui mollitia animi doloremque repellendus vel cumque id? Eveniet autem ut, obcaecati excepturi architecto ratione cumque repudiandae ab?
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Output: Scrollbars will appear only if the content overflows.
The CSS overflow property is an essential tool for controlling the flow of content inside elements. Whether you need to hide content, add scrollbars, or allow content to overflow, this property gives you flexibility in designing layouts that adapt to various content types.
Mastering the 'overflow' property will allow you to create better, more controlled layouts for your web projects.
'overflow: scroll' always shows scrollbars, even when they're not needed. 'overflow: auto' only shows scrollbars when the content exceeds the container's boundaries.
Yes, 'overflow' works with both flexbox and grid containers. You can use it to manage overflow within flexible or grid-based layouts.
Yes, 'overflow: hidden' will clip any content that exceeds the container's bounds, including text, images, and other elements.
No, the 'overflow' property itself cannot be directly animated. However, you can animate the size of an element, which may result in overflow behavior.
Yes, 'overflow: hidden' can be used to clear floats, preventing floated elements from affecting the layout of subsequent elements.