1

I'm facing a strange issue that might have link with flexbox misbehaving with max-height, but so far I didn't find any pure css solution.


I made a plunker to summarize the problem. If you resize your window to reduce its height, at some point you should have a scrollbar in the first block, but if you get back to a higher height, even if there is enough space, the scrollbar won't disappear unless you put your mouse over it (which feels very bugy) : https://plnkr.co/edit/VsJ7Aw8qZdSM1iJeL7Bj?p=preview

I have a main container (in flex) containing 2 blocks (also in flex).
The main container has its height set to 100%, allowing it to resize itself following the window size.
Both children have a fixed content and an overflow-y set to auto.
The first child has a max-height in % to let more height to the second child.

The issue seems to come from this max-height rule. If you remove it, then there's no problem, but I need this max-height...
I don't want to use something like:

.max { flex: 1 1 auto; }
.all { flex: 3 1 auto; }

because it would make my first block higher than its content depending on the window size. I want the first block to have at most its content height.

So my question is: Is it an implementation issue in many browsers (maybe all, but I only tested it in Chrome, IE10 and IE11), or is something wrong in my logic ? Thank you.


UPDATE: I used a fixed height for my content in this example, but in my project it's a list of n elements in it. So I can't really set my max-height with px value.


UPDATE2: I can't use vh in .max max-height property because it takes 100vh as 100% of viewport height (basically your browser window height). But in my context, .main is already in other containers. Those containers have already their heights defined and are smaller than my window height.

/* Styles go here */

html {
  height: 100%;
}
body {
  height: calc(100% - 16px);
}
.main {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
}
.max,
.all {
  display: flex;
  flex-direction: column;
  width: 100%;
  overflow-y: auto;
}
.max {
  flex: 0 1 auto;
  min-height: 103px;
  max-height: 40%;
  background-color: green;
}
.all {
  flex: 2 1 auto;
  min-height: 235px;
  background-color: blue;
}
.content {
  flex: 0 0 auto;
  box-sizing: border-box;
  height: 200px;
  margin: 5px;
  border: 1px dashed black;
  background-color: white;
}
<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="main">
    <div class="max">
      <div class="content"></div>
    </div>
    <div class="all">
      <div class="content"></div>
    </div>
  </div>
</body>
</html>
  • well use `flex: 0 1 40%;` for setting a max-height to `max`... which is more proper I think – kukkuz Sep 26 '16 at 14:53
  • Setting the flex-basis to 40% will make me lose the auto, and I need it to stay auto. :/ – Dacre-Wright Jean-Eudes Sep 26 '16 at 15:16
  • I think I get it now... I was only considering the case when content height exceeds 40%... below that the container won't shrink to the content if you use flex: 0 1 40% – kukkuz Sep 26 '16 at 16:20

2 Answers2

3

It is a bug, in Chrome, a test in FF and Edge, it works fine.

Since you use full viewport height, change the max-height: 40%; to max-height: 40vh;.

Another way, as in below sample, is to change the 100% in height: 100% to 100vh.

I guess this works better because viewport units like vh is a fixed unit, which percent is not.

Plnkr demo: https://plnkr.co/edit/66W4a2lOI58XLudCmkw9?p=preview

html {
  height: 100vh;
}
body {
  height: calc(100vh - 16px);
}
.main {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100%;
}

.max,
.all {
  display: flex;
  flex-direction: column;
  width: 100%;
  overflow-y: auto;
}
.max {
  flex: 0 1 auto;
  min-height: 103px;
  max-height: 40%;
  background-color: green;
}
.all {
  flex: 1 1 auto;
  min-height: 235px;
  background-color: blue;
}

.content {
  flex: 0 0 auto;
  box-sizing: border-box;
  height: 200px;
  margin: 5px;
  border: 1px dashed black;
  background-color: white;
}
<div class="main">
  <div class="max">
    <div class="content"></div>
  </div>
  <div class="all">
    <div class="content"></div>
  </div>
</div>
Asons
  • 84,923
  • 12
  • 110
  • 165
  • Thank you for your answer ! I should have said it earlier, but my example is really a simplified version of my context. I can't use vh here cause it takes 100vh as 100% of viewport height (basically your window). But in my context, .main is already in other containers. Those containers have already their heights defined and smaller than my window height. :/ It looks like I have a specific situation that neither Chrome neither IE10/11 handle. I might create an issue for the dev team, but I don't know where is the best place for this. – Dacre-Wright Jean-Eudes Sep 27 '16 at 15:25
  • @Dacre-WrightJean-Eudes As a work-around, in this plnkr I am using all width properties (min-width, width, max-width), and that seems to self adjust when resizing, so can you use that to make your code work? https://plnkr.co/edit/RJX5XhsWoLFG0rdpCKAW?p=preview – Asons Sep 27 '16 at 16:13
  • Thank you for your comment @LGSON, but as I said "I used a fixed height for my content in this example, but in my project it's a list of n elements in it.", so, sadly, I can't set my .max height property. :/ – Dacre-Wright Jean-Eudes Sep 29 '16 at 10:08
  • I'll wait until tomorrow, but as it looks really like a bug in Chrome and IE 10/11, if there's no workaround, I'll accept your answer. – Dacre-Wright Jean-Eudes Sep 29 '16 at 10:19
  • @Dacre-WrightJean-Eudes Did you find any more info on this? – Asons Oct 02 '16 at 17:24
  • Sadly, no. We had big issues at work so I couldn't come back until now... I might have time to investigate further by the end of the week. – Dacre-Wright Jean-Eudes Oct 03 '16 at 16:33
0

Yes it feels buggy. If you increase the height of the window the height of the first box does not get updated unless:

  • you decrease the height again
  • "put your mouse over it" (did not quite get your meaning here)

IMHO this is a browser bug.

If you set flex-grow to anything greater 0 for the first box, the height gets updated correctly, if you increase the height of the window (as you would expect) But using flex-grow isn't an option as the box could potentially grow bigger than its content.

Rather than using max-height:40% you should use the exact same height as you use for .content and use flex-grow: 1 as well to circumvent the "browser bug"

  • Thank you for your answer. I used a fixed height for my content here, but in my project it's a list of n elements in it. So I can't really set my max-height with px value. – Dacre-Wright Jean-Eudes Sep 26 '16 at 15:38