Table of contents
1.
Introduction
2.
Syntax
3.
Description
4.
Property Values
5.
CSS Overflow Examples
5.1.
Example 1: Hiding Overflowing Content
5.2.
Example 2: Adding Scrollbars
5.3.
Example 3: Automatic Scrollbars
6.
Supported Browsers
7.
Frequently Asked Questions
7.1.
Can the overflow property be applied to inline elements?
7.2.
Is it possible to have different overflow behaviors for the horizontal & vertical axes?
7.3.
What happens if the overflow property is not specified?
8.
Conclusion
Last Updated: Oct 30, 2024
Easy

Overflow Hidden in CSS

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In web design, controlling how content behaves when it exceeds the boundaries of its container is crucial. CSS offers a powerful property known as "overflow" that allows developers to manage this behavior effectively. The overflow property determines what happens when content overflows an element's box, providing options to clip, hide, or add scrollbars whenever required. This setting prevents excess text or images from spilling out, which ensures a neat and visually appealing presentation. 

Overflow Hidden in CSS

In this article, we'll learn the syntax, values, and examples of the CSS overflow property, which will help you create visually appealing and user-friendly web pages.

Syntax

The syntax for the CSS overflow property is : 

element {
  overflow: value;
}


To control the overflow behavior, you simply need to replace "element" with the appropriate selector for the HTML element you want to target. The "value" placeholder is where you specify one of the available overflow values.

Note: Using this syntax, you can quickly and efficiently manage the overflow behavior of any element on your web page. 

Description

The CSS overflow property controls the content behavior when it exceeds the dimensions of its containing element. It lets you specify how the overflowing content should be displayed or hidden. The overflow property can be applied to any block-level or inline-block element.

When content overflows an element's box, there are several scenarios that can occur:

1. The content may extend beyond the boundaries of the element, overlapping other elements or spilling out of the page layout.
 

2. The overflowing content may be clipped, meaning it will be visually cut off at the element's boundaries.
 

3. Scrollbars can be added to the element, allowing users to scroll through the overflowing content.
 

Using the overflow property, you can control which of these scenarios occurs. You can choose whether to hide the overflowing content completely, display scrollbars for accessibility, or simply allow the content to extend beyond the element's box.
 

The overflow property is very useful when dealing with elements with fixed dimensions, like containers with a specific width and height. It ensures that the layout remains intact and provides a clean and organized appearance to your web pages.

Property Values

The CSS overflow property accepts several values determining how the overflowing content should be handled. Here are the commonly used values:

1. `visible` (default): This is the default value. It allows the content to overflow the element's box & remain visible. The overflowing content will be displayed outside the element's boundaries.

 

2. `hidden`: When set to `hidden,` the overflowing content will be clipped and not visible to the user. The content will be cut off at the element's boundaries, and no scrollbars will be provided.

 

3. `scroll`: Setting the overflow property to `scroll` will add scrollbars to the element, both horizontally & vertically, regardless of whether the content overflows or not. This allows users to scroll through the content even if it fits within the element's dimensions.
 

4. `auto`: The `auto` value dynamically adds scrollbars to the element only when the content overflows. If the content fits within the element's box, no scrollbars will be displayed. Scrollbars will appear as needed, either horizontally or vertically, depending on the direction of the overflow.

CSS Overflow Examples

Now, let’s discuss some examples to understand how the CSS overflow property works.

Example 1: Hiding Overflowing Content

HTML: 

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 <link rel="stylesheet" href="style.css" />
 <title>Browser</title>
</head>
<div class="container">
 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum consectetur tellus mi, ac euismod nibh pulvinar eget. Fusce vehicula metus ut nisl rutrum, a ultricies mauris hendrerit.</p>
</div>
 <script src="script.js"></script>
</body>
</html>


CSS

body {
 margin: 20px;
}
.container {
 width: 200px;
 height: 100px;
 background-color: blue;
 overflow: hidden;
}


Output

Output

In this example, we have a `<div>` element with a fixed width & height. The `overflow` property is set to `hidden`, which means any content that exceeds the dimensions of the container will be clipped & not visible to the user.

Example 2: Adding Scrollbars

HTML: 

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 <link rel="stylesheet" href="style.css" />
 <title>Browser</title>
</head>
<div class="container">
 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum consectetur tellus mi, ac euismod nibh pulvinar eget. Fusce vehicula metus ut nisl rutrum, a ultricies mauris hendrerit.</p>
</div>
</body>
</html>


CSS : 

.container {
  width: 200px;
  height: 100px;
  background-color: #f1f1f1;
  overflow: scroll;
}


Output

Output

In this example, the `overflow` property is set to `scroll`. This adds scrollbars to the container, allowing users to scroll through the overflowing content. The scrollbars will be visible even if the content fits within the container's dimensions.

Example 3: Automatic Scrollbars

HTML : 

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 <link rel="stylesheet" href="style.css" />
 <title>Browser</title>
</head>
<div class="container">
 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum consectetur tellus mi, ac euismod nibh pulvinar eget. Fusce vehicula metus ut nisl rutrum, a ultricies mauris hendrerit.</p>
</div>
 <script src="script.js"></script>
</body>
</html>


CSS: 

.container {
  width: 200px;
  height: 100px;
  background-color: #f1f1f1;
  overflow: auto;
}

 

Output

Output

In this example, the `overflow` property is set to `auto`. The browser will automatically add scrollbars to the container only when the content overflows. If the content fits within the container's dimensions, no scrollbars will be displayed.

Supported Browsers

The CSS overflow property is widely supported across modern web browsers. Below is the list of browsers that support the overflow property:

- Chrome: Version 1.0 & above

- Firefox: Version 1.0 & above

- Internet Explorer: Version 4.0 & above

- Safari: Version 1.0 & above

- Opera: Version 7.0 & above


This means that you can confidently use the overflow property in your CSS code, knowing that it will work consistently across the vast majority of web browsers in use today.

Always Remember: While these browsers support the basic functionality of the overflow property, there might be slight variations in how certain values are interpreted or rendered. However, the core behavior of the overflow property remains consistent.

Frequently Asked Questions

Can the overflow property be applied to inline elements?

No, the overflow property only works on block-level & inline-block elements.

Is it possible to have different overflow behaviors for the horizontal & vertical axes?

Yes, you can use the overflow-x & overflow-y properties to control the overflow behavior independently for each axis.

What happens if the overflow property is not specified?

If the overflow property is not specified, the default value is "visible," which allows the content to overflow the element's box.

Conclusion

In this article, we learned about the CSS overflow property and how it allows developers to control the behavior of overflowing content within an element's box. We discussed its syntax, various overflow values, and different examples to show its use. With the help of the overflow property, you can create visually appealing and user-friendly web pages that easily handle content overflow. 

You can also check out our other blogs on Code360.

Live masterclass