0

Assume we have following markup

<div id="parent">
   <div id="header">
   </div>
   <div id="content">
   </div>
</div>

header div is variable-sized, so we don't know what exact offset gets the second one. We want to accomplish two following objectives:

  1. The second div should never overflow its parent. If overflow occurs, the scrollbar appears.

  2. It should be done in pure-CSS manner, for IE9+ if possible.

I have been thinking that this task is pretty easy, really, I just want my div to be non-overflowing, that's all, but I found that it's not possible even with flexbox.

When i say overflow I mean vertical overflow, because they all are of the same width.

Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151
  • the height of the parent is defined? is it the whole viewport height? – Fabrizio Calderan Nov 29 '16 at 16:15
  • `#parent { display: inline-table; } #header, #content { display: table-row; }` – Banzay Nov 29 '16 at 16:25
  • @Banzay sorry it doesn't work. – Alex Zhukovskiy Nov 29 '16 at 16:34
  • http://stackoverflow.com/q/35100237/483779 with a solution for IE9+ – Stickers Nov 29 '16 at 17:09
  • @Pangloss unfortunly, it's not working. I found similar issue (another than linked as duplicate), but it has no answer: http://stackoverflow.com/questions/22566960/display-table-is-exceeding-parent-height-in-every-browser-but-chrome – Alex Zhukovskiy Nov 30 '16 at 11:28
  • @Pangloss it's not working becuase specification treats `height` as minimum value, when I needed it as maximum: [The height of a table is given by the 'height' property for the 'table' or 'inline-table' element. Any value other than 'auto' is treated as a minimum height.](https://www.w3.org/TR/CSS21/tables.html#height-layout). See an article with a workaround which works: https://blogs.msdn.microsoft.com/kurlak/2015/02/20/filling-the-remaining-height-of-a-container-while-handling-overflow-in-css-ie8-firefox-chrome-safari/ – Alex Zhukovskiy Nov 30 '16 at 14:34
  • @Pangloss I'm talking about second approach (CSS table), because flexbox is obviosly working, but only in IE10+. And CSS table approach is not working without several wrappers with specific styles (as linked above). – Alex Zhukovskiy Nov 30 '16 at 15:55

1 Answers1

1

It's very simple to do this with Flexbox, just set flex-direction: column on parent and overflow-y: auto on content.

#parent {
  height: 300px;
  border: 1px solid black;
  display: flex;
  flex-direction: column;
  width: 200px;
}
#header {
  background: lightblue;
}
#content {
  flex: 1;
  overflow-y: auto;
  background: lightgreen;
}
<div id="parent">
   <div id="header"><br><br><br></div>
   <div id="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Atque culpa consequuntur repellat ex, ut cumque fugit minus, itaque omnis, cum quidem in debitis. Consequuntur perspiciatis corporis nostrum similique eveniet voluptates cumque molestias rerum, distinctio accusamus? Quas explicabo, ea praesentium ad velit rem accusamus officia quibusdam ut accusantium possimus dignissimos dolores ipsum quia placeat tenetur omnis veritatis molestias voluptatibus consequuntur odit consectetur vero, voluptas corporis laudantium! Rem laborum excepturi quia temporibus, veniam blanditiis tempore eaque nostrum suscipit, quam laboriosam ratione provident obcaecati iste magni molestiae explicabo quibusdam expedita veritatis officia, debitis officiis sed. Neque veniam eius saepe. Quo doloremque, repellat mollitia!</div>
</div>
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
  • And is it possible for IE9? I'l mark it as answer anyway, but i'm wondering if it's possible. – Alex Zhukovskiy Nov 29 '16 at 16:33
  • The problem is that you don't know the height of header so content needs to have dynamic height to take rest of free space and also `overflow-y: auto`. I am not sure if there is other way to do this in css but there could be. Maybe something with tables. – Nenad Vracar Nov 29 '16 at 16:39
  • I actually found a solution for IE9: https://blogs.msdn.microsoft.com/kurlak/2015/02/20/filling-the-remaining-height-of-a-container-while-handling-overflow-in-css-ie8-firefox-chrome-safari/ . Add it in your answer, please. – Alex Zhukovskiy Nov 30 '16 at 11:56