Friday, November 20, 2026
HomeCSSOh No, Overflow!

Oh No, Overflow!


The overflow CSS property (shorthand for overflow-x and overflow-y) controls what occurs when the stuff you set inside a field is larger than the field itself. Ought to it bleed out of the field (overflow: seen, the default)? Hidden fully (overflow: hidden)? Or ought to the content material be scrollable (overflow: scroll or overflow: auto)?

Why can I see scroll bars (or not)?

Each scroll and auto ought to clip the content material to the component’s padding field. In response to the spec, when a component makes use of scroll the consumer agent ought to show scroll bars when this worth is about in a single path, whether or not or not any content material is overflowing or clipped. Utilizing auto, the scroll bars ought to solely show when there is overflow — subsequently they shouldn’t be seen if the content material will not be bigger that the container field.

In apply nonetheless, this behaviour is inconsistent. On a Mac the scroll bars are solely seen when the consumer hovers over the scrollable component. Whereas Home windows typically renders the scrollbars when auto is used, even when there isn’t a overflow.

For the aim of this text I’m going to speak loads about width and top, inline and block axis. I’m making some assumptions in referring principally to paperwork within the default horizontal writing mode, the place width corresponds to the inline axis and top to the block axis. It provides one other layer of complexity once we get to speaking about logical properties, writing modes and path, as I’m positive many of those guidelines can be flipped round! However as I’ve solely developed net purposes in horizontal writing modes, that’s the place my experience lies.

Overflow is sweet

Overflow is fairly helpful: if our content material is longer than the peak of the viewport, customers count on to have the ability to scroll to see it. We get that behaviour totally free (thanks browsers!). If we set a hard and fast top on a component and our content material is longer then we’ll get overflow. We not often set mounted heights on the internet, preferring (sensibly) to let the peak be decided by the content material, however an exception is perhaps a format like this, the place we have now a hard and fast, full-height menu on the left of our content material. If there are extra gadgets in our menu than will be accommodated by the peak of the component, the consumer ought to have the ability to scroll them into view. We will deal with that simply by setting max-height: 100vh (or no matter our mounted top is) and overflow-y: auto on the left-hand component.

UI with scrollable menu on the left

Preserve that in your thoughts, as a result of we’re going to come back again to that format afterward!

Uh oh, overflow!

Generally we’d must take care of sudden overflow inflicting format bugs. Typically that manifests itself as pesky scrollbars showing the place we don’t need them. Some issues that generally trigger overflow:

Photos

In contrast to top, constraint by width is commonplace, as we’re restricted by the width of the viewport itself. Block parts have a width of 100% by default, in order that they shouldn’t usually overflow until a: we give them a hard and fast width or minimal width, or b: we put one thing in them that has intrinsic width, like a picture with a width of 1600px in a 1400px viewport.

Image wider than the available space, with scrollbar below

It’s widespread so as to add max-width: 100% to your CSS reset to stop this.

Lengthy phrases

I work on an online app for wind farm operators. A few of our purchasers are German, and generally an extended German wind farm title will trigger points in a flex field or grid format.

A word longer than the widht of the menu, which is clipped

One strategy to remedy that’s with overflow-wrap: break-word, which is able to enable the textual content to wrap inside a single phrase the place there’s inadequate house.

In some instances we’d wish to clip the textual content and use text-overflow: ellipsis. I’m not an enormous fan of hiding content material from customers, however there are occasional instances that warrant it.

See the Pen
text-overflow: ellipsis
by Michelle Barker (@michellebarker)
on CodePen.

Transforms

Generally we’d cover a component off-screen with a rework, maybe with the intention to animate it into view upon a selected interplay. It’s straightforward to overlook that the reworked component can nonetheless trigger overflow!

A square box with arrow to indicate it has been moved off-screen to the right, with scrollbar below

We might cover it with show: none with the intention to forestall this, which might trigger the remainder of the format to behave as if the overflowing component isn’t there. Alternatively we might set overflow: hidden on a dad or mum component.

overflow: hidden on a container surrounding the transformed square element, scrollbar omitted to show the page is no longer scrollable

Flexbox layouts

Flexbox layouts are a typical reason for sudden overflow. Every component may by itself don’t have any mounted dimension, however mixed could possibly be wider than the out there house.

Generally the easy answer is flex-wrap: wrap, which is perhaps answer for a horizontal menu, for instance. Different occasions we’d must take a better take a look at the intrinsic width of particular person parts in our flex format, or modify the flex-grow, flex-shrink or flex-basis values.

Delegating overflow

As a result of overflow is seen by default (besides on the foundation component), we’ll usually solely get horizontal overflow on the foundation component if the overflowing component exceeds the viewport.

horizontal navigation overflowing the viewport

By setting overflow to scroll or auto on an ancestor of the overflowing component we trigger the horizontal scrolling behaviour to be eliminated on the foundation component (offering there isn’t one other component inflicting it), and the ancestor to deal with it as an alternative. This could possibly be one other attainable answer to the horizontal menu instance above.

horizontal navigation with overflow: auto instead

It’s additionally helpful for making a scrollable gallery, or a desk.

Debugging overflow

A component inflicting horizontal overflow is a typical bug, and one that may be difficult (and aggravating) to debug. At one time you’ll get a warning about it when you run your website via Lighthouse, though I’m uncertain if it’s nonetheless the case. My private approach is to undergo the DOM commenting out chunks of code till I discover the realm that’s inflicting the issue.

Debugging with Firefox

Firefox dev instruments are actually useful on the subject of debugging overflow. In case you examine the DOM you’ll see a bit “overflow” tag on the offending component, and a “scroll” tag on the scrollable ancestor. Helpful!

Drawback: overflow seen on one axis

So, undesirable overflow points generally is a little bit of a ache, however it helps to know what causes them and find out how to debug them. There are some quirks of the overflow property which might be a bit inconvenient, nonetheless.

Let’s return to our earlier instance of a hard and fast sidebar menu. The menu is perhaps longer than the seen top of its dad or mum, so we’ll want overflow-y: auto to permit the consumer to scroll with the intention to uncover all of the menu gadgets. The menu I constructed used some technical language that is perhaps unfamiliar to new customers, so I used to be requested so as to add some tooltips, which might show to the proper of the hovered component.

Menu with tooltips that display on hover

It will make sense to incorporate these within the HTML near the precise hyperlink they describe, with the intention to place it appropriately. Then we might use CSS to place the tooltip to the proper and show it on hover or focus:

.tooltip {
  place: absolute;
  prime: 50%;
  left: 100%;
  rework: translateY(-50%);
  show: none;
}

a:hover + .tooltip,
a:focus + .tooltip {
  show: block;
}

Sadly, as a result of the menu has overflow in a single path, hovering on a menu merchandise (inflicting the tooltip to look) will trigger overflow within the horizontal path too! What we actually need is overflow-x: seen and overflow-y: auto. However sadly that mixture isn’t permitted.

Scrollable menu on the left, with a tooltip that would cause overflow

I’ve tried out a CSS-only answer utilizing :has(), the place the tooltips are positioned outdoors the menu:

See the Pen
Overflow menu tooltips
by Michelle Barker (@michellebarker)
on CodePen.

There are many accessibility issues round tooltips although, so we’d wish to think about a JS answer.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments