Nested flexbox with scrolling area
Setting min-height
is indeed required, and the correct way to achieve the desired layout. From the spec:
By default, flex items won’t shrink below their minimum content size (the length of the longest word or fixed-size element). To change this, set the min-width or min-height property.
So only by setting min-height
do you allow your your <article>
to actually make use of flex-shrink
and fit into the parent flex container.
If I see this correctly, the issue you see with IE matches the bug described here and acknowledged here:
In IE 10-11, min-height declarations on flex containers in the column direction work to size the containers themselves, but their flex item children do not seem to know the size of their parents. They act as if no height has been set at all.
This might (or might not) encompass issues regarding overflow.
Full page with nested flexbox and scrolling
I got this after a bit of tweaking:
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
}
.page {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden;
}
.topbar {
background-color: lightgreen;
}
.vcontainer {
flex-grow: 1;
display: flex;
flex-direction: row;
min-height: 0;
}
.sidebar {
overflow-y: auto;
background-color: yellow;
}
.content {
flex-grow: 1;
overflow: auto;
background-color: lightblue;
}
.footer {
background-color: pink;
}
.contentcontainer {
height: 100%;
}
.contentsub {
max-height: max(40%, 2rem);
margin: 1rem;
overflow-y: scroll;
background-color: orange;
}
<div class="page">
<nav class="topbar">
top bar
</nav>
<div class="vcontainer">
<nav class="sidebar">
side<br/>side<br/>side
</nav>
<main class="content">
<div class="contentcontainer">
<div>
some additional heading content
</div>
<div class="contentsub">
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
<p>some actual content</p>
</div>
<div>
some additional footer content
</div>
</div>
</main>
</div>
<footer class="footer">
footer
</footer>
</div>
Scrolling inside nested flexboxes
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
ormax-height
) orwhite-space
set tonowrap
.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?
Scrolling inside nested flexboxes with bootstrap
After discussion with other people we came up with a satisfying solution for this problem:
Code Example
JSFiddle example
HTML
<body style="height: 100vh;">
<div id="app-container" class="d-inline-flex h-100 justify-content-stretch">
<nav class="navbar justify-content-start navbar-dark bg-dark">
<div class="navbar-nav">
<a class="nav-item nav-link pointer">Navbar</a>
</div>
</nav>
<div class="d-flex">
<div class="col p-3">
<div class="myContainer h-100 d-flex flex-column">
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link">Tab 1</a>
</li>
<li class="nav-item">
<a class="nav-link">Tab 2</a>
</li>
<li class="nav-item">
<a class="nav-link">Tab 3</a>
</li>
</ul>
<div class="tab-content flex-grow-1 overflow-auto">
<div class="tab-pane active">
<div class="flex-item">
<div class="row h-10">
<div class="col">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl sit amet erat. Duis semper. Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue. Praesent egestas leo in pede. Praesent blandit odio eu enim. Pellentesque sed dui ut augue blandit sodales. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam nibh. Mauris ac mauris sed pede pellentesque fermentum. Maecenas adipiscing ante non diam sodales hendrerit.</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
Make content scroll horizontally within nested flex containers with position sticky
The issue comes from using flexbox.
Switching to grid fixes the problem.
body {
height: 1920px;
margin: 0;
}
#primary-container {
position: relative;
display: flex;
margin: 1rem;
}
#secondary-container {
position: relative;
display: grid;
grid-template-columns: max-content 1fr;
align-items: start;
}
#column-3 {
display: grid;
grid-auto-rows: min-content;
height: 200px;
}
#content-wrapper {
overflow: auto;
}
#extra-large-content {
width: 3000px;
background-color: lightgreen;
}
.sticky {
position: sticky;
top: 0;
align-self: flex-start;
}
.border {
border: 1px solid #ccc;
}
<div id="primary-container" class="border">
<div class="sticky">
column1
</div>
<div id="secondary-container" class="border">
<div class="sticky">
column2
</div>
<div id="column-3" class="sticky border">
column3
<div id="content-wrapper">
<div id="extra-large-content">
extra-large content
</div>
</div>
...
</div>
</div>
</div>
Problem with flexbox layout and nested scrollbar
You need to allow .content
to shrink then add min-width:0
or overflow:hidden
so its size is calculated at screen inside its parent so overflow works on the children:
.content {
flex: 1 1 auto;/* updated*/
background: green;
min-width:0;/* added */
}
.flexContainer {
display: flex;
}
.sidebar {
flex: 1 0 auto;
min-width: 150px;
max-width: 190px;
color: white;
background: blue;
}
.content {
flex: 1 1 auto;
background: green;
min-width:0;
}
.scrollableElement {
display: flex;
overflow-x: scroll;
}
.textElement {
color: white;
background: grey;
}
<html>
<body>
<div class="flexContainer">
<div class="sidebar">
<div>Sidebar</div>
</div>
<div class="content">
<div class="scrollableElement">
<div class="textElement">
<div>Content11111111111111111111111111111111111111111111111111111111111111111111111</div>
<div>Content22222222222222222222222222222222222222222222222222222222222222222222222</div>
<div>Content33333333333333333333333333333333333333333333333333333333333333333333333</div>
<div>Content44444444444444444444444444444444444444444444444444444444444444444444444</div>
<div>Content55555555555555555555555555555555555555555555555555555555555555555555555</div>
<div>Content66666666666666666666666666666666666666666666666666666666666666666666666</div>
<div>Content77777777777777777777777777777777777777777777777777777777777777777777777</div>
<div>Content88888888888888888888888888888888888888888888888888888888888888888888888</div>
</div>
</div>
</div>
</div>
</body>
</html>
Nested flexbox's overflow: scroll not working
Adding h="100%"
to the parent Flex container, removing h="100%"
from the first child Flex container and adding minH="0"
to it instead gives the desired result.
The problem here was that the parent height was unbounded meant that the children could take up as much space as they can and the parent would reflow to account for that since the children are set to flex-grow: 1
.
import React from "react";
import _ from "lodash";
import {
Flex,
Box,
Center,
UnorderedList,
ListItem,
Heading
} from "@chakra-ui/react";
const Container = (props) => {
const list = [];
_.times(100, (i) => list.push(`Item ${i}`));
return (
<Flex id="container" flex={1} direction="column" h="100%">
<Center id="header" h="75px" bg="yellow.300">
<Heading>Flexbox Scroll Overflow Prototype</Heading>
</Center>
<Flex
id="3-col-container"
flex={1}
bg="gray.100"
p={4}
minH="0"
alignItems="stretch"
gridColumnGap={2}
>
<Flex flex={1} bg="gray.50" direction="column" gridRowGap={2}>
<Box minH="200px" bg="gray.500" />
<Flex
direction="column"
pl={4}
pt={2}
flex={1}
bg="teal.100"
overflow="scroll"
>
<UnorderedList>
{list.map((item) => (
<ListItem mb={1} listStyleType="none" color="gray.700">
{item}
</ListItem>
))}
</UnorderedList>
</Flex>
</Flex>
<Flex flex={1} bg="blue.100" />
<Flex flex={1} bg="blue.200" />
</Flex>
</Flex>
);
};
export default Container;
Related Topics
CSS Word-Wrap: Break-Word Don't Work on IE9
Checkboxes with Font Awesome 5 Icons
Weird Webkit Issue with Position: Fixed
How to Include Caption for Gallery Using Magnific Popup
How to Set Maximum Number of Rows in a Flexbox Container and Hide Extra Element
Css, Nested Divs & Margins VS. Padding
How to Push a Footer to The Bottom of Page When Content Is Short or Missing
Why Is My CSS Media Query Being Ignored or Overridden
Does a Cache Buster on an Image Url in CSS Cause an Extra Request
How to Render Bootstrap Dropdown Above the Object
Decrease the Tabs Bar Height in Gnome Terminal
CSS Attribute Selector + Descendant Gives a Bug in Webkit
Which CSS Pseudo-Classes Don't Have Specificity
Resource Interpreted as Script But Transferred with Mime Type Text/X-C++