Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my flexbox expand despite overflow:hidden/auto?

Tags:

html

css

Can anyone explain this behavior? I've created two stacked flexbox children to be a simple outline for a full-screen web app. However, adding content inside of my top element seems to expand it, eventually pushing the bottom one off of the screen.

var buttonEl = document.getElementById('addButton');

buttonEl.addEventListener('click', function() {
    var newEl,
        ulEl;
    
    // Create a list item element
    newEl = document.createElement('li');
    newEl.innerHTML = "Test List Item";
    
    // Add the list item element to the list
    ulEl = document.getElementById('list');
    ulEl.appendChild(newEl);
}, false);
* {
    padding: 0px;
    margin: 0px;
    overflow: hidden;
}
html {
    width: 100%;
    height: 100%;
}
body {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    background: lightblue;
}
#top {
    flex-grow: 1;
    overflow-y: auto;
}
#bottom {
    flex-basis: 1em;
}
li {
    width: 100%;
    height: 2em;
    line-height: 2em;
    background: white;
}
<div id="top">
    <button id="addButton">add list item</button>
    <ul id="list">
    </ul>
</div>
<div id="bottom">
    Bottom
</div>
like image 385
BCDeWitt Avatar asked Sep 08 '14 23:09

BCDeWitt


People also ask

How do I break out of overflow hidden?

You can do this by setting the child to be position: absolute. Usually, this should work: overflow: hidden doesn't clip absolutely positioned descendants unless the same container doesn't have position:relative (see cssmojo.com/clearfix-reloaded-overflowhidden-demystified).

What is the opposite of overflow hidden?

visible - Default. The overflow is not clipped. The content renders outside the element's box. hidden - The overflow is clipped, and the rest of the content will be invisible.

How do I fix the overflow in CSS?

To change this, set the min-width or min-height property.” This means that a flex item with a long word won't shrink below its minimum content size. To fix this, we can either use an overflow value other than visible , or we can set min-width: 0 on the flex item.


1 Answers

Here's one way to do it: http://jsfiddle.net/1ejgybmv/.

* {
    padding: 0px;
    margin: 0px;
}

html, body {
    height: 100%;
}

body {
    display: flex;
    flex-direction: column;
    background: lightblue;
}
#top {
    flex: 1 1 auto;
    overflow-y: auto;
}

#bottom {
    flex: 0 0 1em;
}

li {
    height: 2em;
    line-height: 2em;
    background: white;
}
like image 150
DRD Avatar answered Oct 01 '22 02:10

DRD