Popping Out of Hidden Overflow

Avatar of Agop Shirinian
Agop Shirinian on (Updated on )

The following is a guest post by Agop Shirinian. Agop ran into an interesting scenario where he needed an element to be scrollable in one direction, while allowing the overflow in the other direction. You’d think that’s what overflow-x and overflow-y are for, but it’s not that simple. I’ll let Agop explain.

So you’re tasked with creating a scrollable menu with submenus that pop out when you hover over a parent menu item.


Create a list for the menu, add some nested lists for the submenus, position the nested lists based on their parent list items, voilà!

See the Pen Scrollable menu with pop out submenus (broken) by Agop (@agop) on CodePen.

Wait, that’s not right. Oh, of course, we used overflow: auto – perhaps if we use overflow-x: visible, the horizontal overflow of the submenus will be visible:

See the Pen Scrollable menu with pop out submenus (broken #2) by Agop (@agop) on CodePen.

What gives? Why do we still get scrollbars?

The Problem

If we look at the W3C spec, we find the following explanation:

The computed values of ‘overflow-x’ and ‘overflow-y’ are the same as their specified values, except that some combinations with ‘visible’ are not possible: if one is specified as ‘visible’ and the other is ‘scroll’ or ‘auto’, then ‘visible’ is set to ‘auto’.

Basically, this:

overflow-x: visible;
overflow-y: auto;

Turns into this:

overflow-x: auto;
overflow-y: auto;

So we can’t have visible horizontal overflow if the vertical overflow is invisible, and vice versa.

And if we can’t have visible horizontal overflow, we can’t have our pop out submenus!

The Solution

Interestingly enough, if we omit the position: relative from the menu items, the submenus do show up, positioned based on their closest positioned ancestor. In this case, they don’t have a positioned ancestor, so they’re positioned relative to <body>:

See the Pen Scrollable menu with pop out submenus (step 1) by Agop (@agop) on CodePen.

Basically, in order for an absolutely positioned element to appear outside of an element with overflow: hidden, its closest positioned ancestor must also be an ancestor of the element with overflow: hidden.

Knowing this, we can add a wrapper around the menus to act as the closest positioned ancestor for each submenu. Then, whenever the user hovers over a menu item, we can position the submenu wrappers using a bit of JavaScript:

See the Pen Scrollable menu with pop out submenus by Agop (@agop) on CodePen.

And that’s it! Since neither the menus nor the menu items are positioned, the submenus are able to pop out of the hidden/scrollable overflow. Now we can have as many levels of nested submenus as we want, and we won’t get any undesired clipping.


Unfortunately, this method of showing items that would otherwise be hidden is very obscure.

It’d be nice if we could specify a clip depth, which would control which ancestor in the hiearchy would be responsible for clipping a particular element:

./* Fair warning: not real code */
.submenu {
  /* only an ancestor 2 levels up can clip this element */ 
  clip-depth: 2;

Or, even better, perhaps we could specify the clipping parent by a CSS selector:

/* Fair warning: not real code */
.submenu {
  /* only an ancestor that matches the .panel selector can clip this element */
  clip-parent: .panel;