Scrolling inside nested flexboxes

[亡魂溺海] 提交于 2019-12-04 10:07:31

Generally speaking, for overflow: auto to render a vertical scrollbar, you need to define a height on the container.

In order for overflow to have an effect, the block-level container must have either a set height (height or max-height) or white-space set to nowrap.

source: https://developer.mozilla.org/en-US/docs/Web/CSS/overflow

However, this isn't always a practical solution, especially in dynamic environments.

In your layout, the simple solution is to make the container (.content) a flex container. That's enough to make the layout work in Chrome (demo).

However, for the layout to also work in Firefox and Edge, you need to override the default minimum height of flex items, which is min-height: auto. This prevents flex items from shrinking below the size of their content, which eliminates the possibility for an overflow to occur. (full explanation)

Make these adjustments to your code:

.tabbed_content {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  min-height: 0;                  /* NEW, esp for FF & Edge (see link below) */
}

.content {
  padding: 1rem;
  background-color: #EEEEEE;
  overflow: auto;                /* KEEP THIS, for FF & Edge (see link below) */
  display: flex;                 /* NEW */
  flex-direction: column;        /* NEW */
}

.scrollable {
  padding: 0.5rem;
  background-color: white;
  overflow: auto;               /* RESTORE */
  border: 1px dashed #CECECE;
}

revised codepen

More details: Why doesn't flex item shrink past content size?

Layout like this is tricky! I usually try and build simplified versions and work up.

One way you could do it would be with position absolute for the scrollable div.

https://codepen.io/sheriffderek/pen/zwQxLr/ - see this pen for long and short examples

<aside class="window">

    <header>header</header>

    <main>
        <div class="scroll-area">
            <ul>
                <li>short conent</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
                <li>5</li>
            </ul>
        </div>
    </main>

    <footer>footer</footer>

</aside>

CSS (this is stylus - but it's just syntax - and the concept should be clear)

.window
    border: 10px solid $gray;
    max-width: 600px // arbitrary
    height: 80vh // arbitrary

    display: flex
    flex-direction: column

    header, main, footer
        padding: $pad

    header
        background: $color

    main
        flex-grow: 1
        background: $alternate
        position: relative

        .scroll-area
            position: absolute
            top: 0
            left: 0
            width: 100%
            height: 100%
            overflow: auto
            padding: $pad

    footer
        background: $highlight

For your additional question, you can add/remove class from the body when opening the modal and stop scrolling.

Prevent body scrolling but allow overlay scrolling

You have to set a height on .scrollable as well as overflow-y: auto; otherwise, the overflow is visible as there is no height for it to overflow from.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!