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?
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>
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>
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;
Flexbox layout with two equal height children, one containing nested flexbox with scrolling content
In general, for overflow: scroll
(or auto
and hidden
) to work, a height constraint is needed in one way or the other, or else element's normally grow as much as needed to fit their content.
There is mainly 3 ways, where either an actual height is set, as in this first sample, where I added it to the container
.
Stack snippet 1
.container { display: flex; height: 100vh;}
.child { border: 1px solid grey; background-color: lightgrey; flex: 1 1 auto;}
.controls-panel { display: flex; flex-direction: column;}
.controls { flex: 0 0 auto;}
.content-wrapper { flex: 1 1 auto; width: 400px; overflow-y: auto;}.content-item { display: inline-block; width: 100px; height: 100px; background-color: red;}
<div class="container"> <div class="child"> <p>In real life I am an inline img.</p> <p>I am some content whoop de doo.</p> <p>I am some content whoop de doo.</p> <p>I am some content whoop de doo.</p> <p>I want my sibling to equal my height.</p> </div> <div class="child controls-panel"> <div class="controls"> <p>Small controls area. Panel below should scroll vertically.</p> </div> <div class="content-wrapper"> <div class="content-item"></div> <div class="content-item"></div> <div class="content-item"></div> <div class="content-item"></div> <div class="content-item"></div> <div class="content-item"></div> <div class="content-item"></div> <div class="content-item"></div> <div class="content-item"></div> <div class="content-item"></div> <div class="content-item"></div> <div class="content-item"></div> <div class="content-item"></div> <div class="content-item"></div> <div class="content-item"></div> <div class="content-item"></div> </div> </div></div>
Nested flexbox UI with scrolling content rendered differently in Safari and in MS Edge
Ok this was a hard one! Add this to your CSS and it should work. (tested in Safari)
body .multiple-variable-selection-columns-container {
height: calc(100% - 66px);
}
body .modal-menu .tab-content {
height: calc(100% - 57px);
}
body .modal-wide .modal-dialog .modal-content .modal-body {
height: calc(100% - 110px);
}
ng-include {
display: block;
}
You have added height: 100%;
to elements who have neighbour elements with there own height. You have definitely to cleanup your markup, but the CSS above should work for the moment.
I have added the body
to get a stronger selector to overwrite your code. I also added a default display style for ng-include
tag, the browser doesn't know what kind of styles to use, so the tag has no hight and is hard to debug.
I hope I could help you.
Related Topics
Resize Unknown Number of Elements to Fill Width of Parent Container
Hide Overflow on Elements with Fixed Position
CSS Selector for First Element of Visual (Block Reflow) Row
Can It Flexbox? Chat Window with Input at the Bottom, Chats Scrolling Up
Why Is My Android Device-Width 980Px
Internet Explorer Automatically Switches to Compatibility Mode (IE9 and Ie10)
Nested Column Flexbox Inside Row Flexbox with Wrapping
Adding CSS Stylesheet to Pages Based on Route in Opencart
Css: Set Color for Selected Row in a Table
How to Get a SASS Nested Nested Media Query to Work with the Use of the Media Query or Operator
Change Color in Shinydashboard
How to Figure Out Proper Min-Width and Max-Width Values for Responsive CSS
Bootstrap 4 CSS Causing Chrome's Print "Layout" to Disappear
Align Block Elements on Top When Using Line-Height
Content Scrolling on Mobile Page with Fixed Header/Footer
Sass Loop to Output Classes with Unique Number and Background-Image