Hero Background

Power Your Software Testing with AI Agents and Cloud

The Native AI-Agentic Cloud Platform to Supercharge Quality Engineering. Test Intelligently and Ship Faster.

Tutorial

A Complete Guide To CSS Overflow

Read on to know how to handle your content proficiently with CSS Overflow.

Last Updated on:

CSS overflow controls what a browser does with content that does not fit inside an element box.

The overflow property takes visible, hidden, clip, scroll and auto, and each value decides whether the box clips the content, scrolls it, or lets it spill out of the layout.

This guide covers what CSS overflow is, every value with a live example, the overflow-x and overflow-y axis properties, browser support, the newer clip value, and how to test a page for responsive web design.

Key Takeaways

  • CSS overflow describes content that does not fit inside an element box, and the overflow property decides how the browser handles that content.
  • Setting overflow to hidden clips the overflowing content but still makes the element a scroll container, so a script can still scroll it.
  • Setting overflow to clip also hides the overflowing content, but the element is not a scroll container and programmatic scrolling is not possible.
  • Setting overflow to scroll always draws scrollbars, while auto draws them only when the content actually overflows.
  • The overflow-x and overflow-y properties control the horizontal and vertical axes separately, and overflow is the shorthand for both.
  • The overlay value is a legacy alias for auto that MDN discourages in new code.

What is CSS Overflow?

CSS overflow is when the content overflows from its specified container. This property controls what happens to the content that does not fit in a given area.

The overflow property has the following values:

  • visible
  • hidden
  • scroll
  • auto

Let’s see overflow in action with the help of an example.

HTML:

<h1>overflow not set</h1>
  <p class="box">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived only five centuries.</p>

CSS:

body {
    font: 15px Georgia, serif;
  }
   h1{
    font: 28px Georgia, Serif;
    text-align: center;
  }
  .box{
    width: 200px;
    height: 180px;
    padding: 8px;
    border: 1px solid #000;
    line-height:21px;
    background-color: #EEEEEE;
    margin: 0 auto;
  }

Output:

In the above example, the box has a fixed width and height. However, the text that was placed inside the box is so long that it cannot fit within the box as intended. The extra text you see dropping past the black border is the overflow.

Note

Note: Test CSS Overflow compatibility across 3000+ browser environments. Try TestMu AI Now!

CSS overflow visible

Visible is the default value of CSS overflow. It means that not setting the CSS overflow property is the same as setting it to “visible”.

When overflow is visible, the overflowing content will not be clipped. Instead, it will display outside the element’s box and might overlap other elements on the page.

Let’s see a quick example of overflow visible.

HTML:

<h1>overflow:visible;</h1>
   <p class="box">Lorem Ipsum is simply dummy text of the printing and  typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived five centuries.</p>

CSS:

body {
    font: 15px Georgia, serif;
  }
   h1{
    font: 28px Georgia, Serif;
    text-align: center;
  }
  .box{
    width: 200px;
    height: 180px;
    padding: 8px;
    border: 1px solid #000;
    line-height:21px;
    background-color: #EEEEEE;
    margin: 0 auto;
    overflow:visible;
  }

Output:

In the above example, the p element with the class name “box” has an overflow property set to visible.

As you can notice, there is no difference between the box from the previous example, which doesn’t have any overflow property, and this box which has an overflow property. This is because the overflow property is set to visible by default.

CSS overflow hidden

Overflow hidden means the overflowing content will be hidden and will not be displayed on the page.

Once a web browser comes across the CSS overflow hidden property, it hides the extra content beyond the element’s box.

Let’s look at an example of overflow hidden.

HTML:

<h1>overflow:hidden;</h1>
    <p class="box">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived five centuries.</p>

CSS:

body {
    font: 15px Georgia, serif;
  }
   h1{
    font: 28px Georgia, Serif;
    text-align: center;
  }
  .box{
    width: 200px;
    height: 180px;
    padding: 8px;
    border: 1px solid #000;
    line-height:21px;
    background-color: #EEEEEE;
    margin: 0 auto;
    overflow: hidden;
  }

Output:

As you can see in the example above, the p element with the class name “box” has an overflow property set to hidden. Because of this value, overflowing content is not visible on the page.

CSS overflow scroll

Setting the overflow value to scroll will hide the overflowing content from rendering outside the element’s box, along with providing scrollbars to view the content.

Note that with setting overflow to scroll, you always get both the horizontal and vertical scrollbars even if the content only requires one or the other.

Let’s look at an example of overflow scroll.

HTML:

<h1>overflow:scroll;</h1>
    <p class="box">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived five centuries.</p>

CSS:

body {
    font: 15px Georgia, serif;
  }
   h1{
    font: 28px Georgia, Serif;
    text-align: center;
  }
  .box{
    width: 200px;
    height: 180px;
    padding: 8px;
    border: 1px solid #000;
    line-height:21px;
    background-color: #EEEEEE;
    margin: 0 auto;
    overflow: scroll;
  }

Output:

As you can see in the example above, the box has the overflow property set to scroll which gives us the scrollbar in the output.

Test across 3000+ browser and OS environments with TestMu AI

CSS overflow auto

The overflow auto value is almost similar to overflow scroll, in addition it also solves the problem of getting scrollbars even when we don’t need them.

As discussed above, with overflow scroll we get both the horizontal and vertical scrollbar even if we don’t need them. But in case of overflow auto, we get the scrollbar only when the content actually goes outside the element’s box.

Let’s look at an example of overflow auto.

HTML:

<h1>overflow:auto;</h1>
    <p class="box">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived five centuries.</p>

CSS:

body {
    font: 15px Georgia, serif;
  }
   h1{
    font: 28px Georgia, Serif;
    text-align: center;
  }
  .box{
    width: 200px;
    height: 180px;
    padding: 8px;
    border: 1px solid #000;
    line-height:21px;
    background-color: #EEEEEE;
    margin: 0 auto;
    overflow: auto;
  }

Output:

As we can see in the example above, the p element with the class name “box” has the overflow attribute set to auto. Because of that, we only get the horizontal scrollbar since our content only overflows horizontally.

X and Y property of CSS overflow

Till this point, we used the overflow shorthand property to set overflow. Instead, we can also use overflow-x and overflow-y to control the overflow horizontally or vertically.

Overflow-x is used to control the horizontal overflow of an element. In simple terms, it specifies what to do with the right or left edges of the content.

Overflow-y is used to control the vertical overflow of an element. In simple terms, it specifies what to do with the top or bottom edges of the content.

Below is an example of overflow-x and overflow-y properties.

HTML:

<h1>overflow x and y</h1>
    <p class="box">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived five centuries.</p>

CSS:

body {
    font: 15px Georgia, serif;
  }
   h1{
    font: 28px Georgia, Serif;
    text-align: center;
  }
  .box{
    width: 200px;
    height: 180px;
    padding: 8px;
    border: 1px solid #000;
    line-height:21px;
    background-color: #EEEEEE;
    margin: 0 auto;
    overflow-x:scroll;
    /*add horizontal  scrollbar*/
    overflow-y: hidden;
    /*hide vertical scrollbar */
    white-space: nowrap;
  }

Output:

In the example above, we set overflow-x to scroll, because of which, we can scroll right and left. Next, we also set overflow-y to hidden, because of which we can not scroll up and down.

We used a property named “white-space with the value “no-wrap”. This is used to specify how whitespace inside elements is handled. White-space set to no-wrap means the element’s content will not be wrapped to a new line.

CSS overflow wrap

With the use of the overflow-wrap property in CSS, we can tell the browser to break lines of long words in case the content overflows from the element’s box. This helps prevent layout issues caused by overflow from an unusually long string of text.

This property has three values which are:

  • normal: this is the default value where the browser will break lines according to its line-breaking rules. Long strings will not break even if they overflow the element’s box.
  • break-word: with this value, words or strings of characters too long to fit inside their container will split at random points, causing a line break.
  • inherit: this will inherit the value of the overflow-wrap attribute, which was set on the targeted element’s immediate parent.

Below is an example of overflow-wrap.

HTML:

<h1>overflow-wrap</h1>
    <p class="box">This element contains a very long word: thisisaveryveryveryveryveryverylongword. By default, the long word will not break and wrap to the next line. With break-word, the long word will break.</p>

CSS:

body {
    font: 15px Georgia, serif;
  }
   h1{
    font: 28px Georgia, Serif;
    text-align: center;
  }
  .box{
    width: 200px;
    height: 180px;
    padding: 8px;
    border: 1px solid #000;
    line-height:21px;
    margin: 0 auto;
    background-color:yellow;
    overflow-wrap: break-word;/*try removing the overflow-wrap to see the difference*/
  }

Output:

In the above example, we can see that the box has the overflow-property set to break-word, which breaks the long string of characters and wraps them to the next line, ensuring that our content does not overflow from the container. However, if we do not specify the property, the long words will not break, and it will overflow the container.

CSS Overflow browser compatibility

Now, let’s talk about cross browser compatibility. To be cross-browser compatible, an app must function across different browsers and degrade gracefully when certain browser features are unavailable or disabled.

As web developers, we need to keep browser compatibility in mind because our end users can access our website from browsers of their choice. So, every website needs to function flawlessly across various OS platforms and browsers.

However, while using CSS properties, you could get into a situation where the majority of browsers do not support a particular CSS feature.

So you must perform cross browser testing to ensure it works flawlessly across various browsers, devices, and operating systems.

For doing that, you can use a cloud-based testing platform like TestMu AI that offers an online browser farm of 3000+ real browsers and operating systems combinations to test your websites (and web apps). It lets you perform tests at scale for detecting cross browser compatibility issues in HTML/CSS and fixing them so that your website (or web app) works seamlessly across different browsers & OS combinations.

Shift from a legacy test platform to TestMu AI

The overflow property itself is safe to use everywhere. MDN marks overflow as Baseline Widely available and records support across browsers since July 2015. Browser differences show up in the newer values and in how each browser draws scrollbars, not in the property itself.

Which Other CSS Overflow Values Should You Know?

Beyond visible, hidden, scroll and auto, the overflow property also accepts clip, and it still accepts overlay as a legacy alias for auto. The difference that matters is whether the element becomes a scroll container.

  • clip: clips the overflow and creates no scroll container, so no scrollbars appear and programmatic scrolling is not possible.
  • overflow-clip-margin: sets how far past the padding box the clip edge sits when overflow is clip, and defaults to 0px.
  • overlay: a legacy alias for auto kept for web compatibility, which MDN discourages in new code.
  • hidden against clip: hidden also hides the content but still creates a scroll container, so a script can scroll the element even though no scrollbars show.
  • Scroll container side effect: every value except visible and clip turns the element into a scroll container, which changes where a position: sticky child sticks.

Clipping carries an accessibility cost. MDN notes that focusable content inside a clipped area still receives keyboard focus, but it never scrolls into view, which leaves keyboard users on an element they cannot see.

Pick clip when the element must never scroll, pick hidden when a script may still need to scroll it, and pick auto instead of overlay in any new stylesheet.

Responsiveness test of a website

A website must be designed to be responsive for a strong online presence. Since responsive websites tend to boost user satisfaction, Google prefers them for SEO. Therefore, responsiveness testing of the website components is crucial.

Youtube thumbnail

However, performing tests to check responsiveness can be time-consuming. Moreover, it’s impossible to own devices of all sizes.

There are multiple web development tools that make up a Front-End Developer’s essential toolchain. The usability of these tools allows flexibility while increasing the difficulty in the overall structure. However, there is a need for a tool like LT Browser to help a web developer analyze mistakes and view multiple devices from a real-time view.

TestMu AI’s mobile-friendly test tool, LT Browser, comes with 50+ pre-installed viewports with multiple standard resolutions. Additionally, LT Browser offers sophisticated developer tools for testing.

Youtube thumbnail

You can also Subscribe to the TestMu AI YouTube Channel and stay updated with the latest tutorials around automated browser testing, Selenium testing, Cypress E2E testing, CI/CD, and more.

Below are some of the top-notch features of LT Browser that any developer would love to have:

Need more reasons to love LT Browser? Here are more reasons why developers should use LT Browser.

Run tests up to 70% faster on the TestMu AI cloud grid

Conclusion

Congratulations on reaching this far! You’re a fantastic reader!!

So in this blog, we discussed CSS overflow in detail, including how we can use the CSS overflow, different values of CSS overflow, browser compatibility, its importance, and finally, responsive testing of our websites using tools like LT Browser.

I hope this blog has given you a thorough understanding of CSS overflow. It’s time to apply CSS overflow to your website like a pro! Again, feel free to refer back to this anytime; it is solely for your use.

Happy styling!

Author

...

Tahera Alam

Blogs: 14

  • Twitter
  • Linkedin

Tahera is a freelance Frontend Developer and Technical Writer with over 2 years of hands-on experience in JavaScript, React, Next.js, TypeScript, Tailwind CSS, and Git/GitHub. She specializes in building responsive user interfaces and writing clear, engaging documentation, tutorials, and API guides on technologies like Selenium, Cypress, and modern web development. Tahera has contributed to open-source projects focused on improving UI components and documentation. She has built a following of 7,000+ on X, with some of her posts reaching 400,000+ views, showcasing her influence in the frontend and tech writing community. She currently collaborates with clients on web development and long-form technical content. Although her academic background is in Polity & IR (Bachelor’s) and English Literature (Master’s), she has built her technical expertise through continuous learning and real-world projects.

Add to Google preferred sources

Summarise with AI

Copied to Clipboard!
...

3000+ Browsers. One Platform.

See exactly how your site performs everywhere.

Try it free
...

Write Tests in Plain English with KaneAI

Create, debug, and evolve tests using natural language.

Try for free

CSS Overflow FAQs

Did you find this page helpful?

More Related Blogs

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests