Decision
Add this to your code:
.container > * { flex-shrink: 0; }
Description
The initial configuration of flex items, in accordance with the flexbox specification , should be flex-shrink: 1 . This means that flexible elements are allowed to be compressed to avoid overflowing the container.
Chrome and IE11 seem to stick with this guide.
Other browsers, such as Firefox, Edge, and Safari, set flex-shrink to 0 by default.
On the other hand, this may have little or nothing to do with flex-shrink . The problem may relate to the default settings of min-height and min-width for flex items. (See Here: Why aren't flexible elements reducing the size of the content? )
Or maybe this is a combination of both. It really depends on the browser.
The fact is that browser versions vary. That is why you should test.
Spec's initial settings are not completely reliable since
- browsers are independent objects that can do whatever they want, and
- the use of "interventions".
Interventions
Intervention is when a user agent decides to deviate slightly from standardized behavior to provide a significantly enhanced user experience.
In other words, the browser commits itself to providing settings that, in its opinion, are best suited for its users, regardless of specifications.
Chrome seems to be doing this a lot. Here are some examples:
One thing is clear: if you disable flex-shrink , your layout will work in all browsers. Add this to your code:
.container > * { flex-shrink: 0; }
.container>* { flex-shrink: 0; } .container { height: 150px; width: 300px; display: flex; flex-direction: column; overflow-y: auto; border: 1px solid #000; padding: 4px; } .options { display: flex; flex-direction: row; flex-wrap: wrap; } .buttons { display: flex; flex-direction: row; justify-content: space-between; margin-top: 10px; }
<div class="container"> <div class="options"> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> </div> <div class="buttons"> <input type="button" value="Cancel"> <input type="button" value="Save"> </div> </div>
You can also consider moving the scroll bar only to the options window, after which you really fixed the buttons at the bottom of the screen. Make this setting in your code:
.container { height: 150px; width: 300px; display: flex; flex-direction: column; border: 1px solid #000; padding: 4px; } .options { display: flex; flex-direction: row; flex-wrap: wrap; overflow-y: auto; flex: 1; }
.container > * { flex-shrink: 0; } .container { height: 150px; width: 300px; display: flex; flex-direction: column; border: 1px solid #000; padding: 4px; } .options { display: flex; flex-direction: row; flex-wrap: wrap; overflow-y: auto; flex: 1; } .buttons { display: flex; flex-direction: row; justify-content: space-between; margin-top: 10px; }
<div class="container"> <div class="options"> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> <div class="option"> <input type="checkbox"> Option </div> </div> <div class="buttons"> <input type="button" value="Cancel"> <input type="button" value="Save"> </div> </div>
In addition, I deleted the spacer element that you had. This does not seem necessary when there are many clean ways to create space between elements in a flexible container.