CSS Properties
CSS overflow-y Property
Photo Credit to CodeToFun
🙋 Introduction
The overflow-y
property in CSS controls the vertical overflow of content in an element. It determines what happens if content overflows the element's box vertically.
This property is useful for managing content within a fixed-height container and ensuring that the user experience remains smooth and consistent, even when the content is too large to fit in the available space.
💡 Syntax
The syntax for the overflow-y
property is simple. It is used with any element that might have content that exceeds its bounds.
element {
overflow-y: value;
}
Here, value can be one of several keywords that control how the overflow is handled.
🎛️ Default Value
The default value of the overflow-y
property is visible. This means that content is not clipped and can overflow the element's box, potentially causing it to expand.
🏠 Property Values
Value | Description |
---|---|
visible | The default value. Content is not clipped and may overflow the element's box. |
hidden | The content is clipped and any overflow is not visible. No scrollbars are provided. |
scroll | The content is clipped, and a scrollbar is added to allow the user to scroll through the overflowed content. |
auto | If the content overflows, a scrollbar will appear. Otherwise, no scrollbar is shown. |
📄 Example
In this example, we'll use the overflow-y
property to add a vertical scrollbar to a box containing too much text.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS overflow-y Example</title>
<style>
.overflow-box {
width: 200px;
height: 100px;
border: 1px solid #000;
overflow-y: scroll;
}
</style>
</head>
<body>
<h1>Box with Vertical Scroll</h1>
<div class="overflow-box">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent non nulla ut nunc fermentum dapibus. Vestibulum in massa leo. Duis vehicula risus non mauris viverra, vel aliquam nunc scelerisque.</p>
<p>Maecenas et ligula sit amet neque consequat aliquet. Nullam varius eros at eros efficitur, eget bibendum odio efficitur.</p>
</div>
</body>
</html>
🖥️ Browser Compatibility
The overflow-y
property is widely supported across all major browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is a reliable property to use when managing vertical overflow content.
🎉 Conclusion
The overflow-y
property is an essential tool for controlling how content behaves when it exceeds the vertical limits of its container.
By using this property, you can ensure that your website's layout remains consistent and user-friendly, even with dynamic or extensive content. Experiment with the different values to see how they can best suit your design needs.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS overflow-y Property), please comment here. I will help you immediately.