1

I have the following basic HTML/CSS for a flexbox layout I'm trying to use for a web page:

body, div, html, h1, h2, li, p, ul {
    margin: 0;
    padding: 0;
}
html,
body {
    height: 100%;
}
.container {
    display: flex;
    flex-direction: column;
    min-height: 100%;
}
.headerContainer {
    background: darkblue;
    color: white;
    padding: 10px;
}
.sidebarAndContentContainer {
    display: flex;
    flex: 1;
}
.sidebar {
    background: gray;
    padding: 10px;
}
.nav {
    color: white;
    list-style: none;
}
.content {
    padding: 10px;
}
<div class="container">
    <div class="headerContainer">
        <h1>Site Title</h1>
    </div>
    <div class="sidebarAndContentContainer">
        <div class="sidebar">
            <ul class="nav">
                <li>Nav Link #1</li>
                <li>Nav Link #2</li>
                <li>Nav Link #3</li>
                <li>Nav Link #4</li>
                <li>Nav Link #5</li>
            </ul>
        </div>
        <div class="content">
            <h2>Page Title</h2>
            <p>Content</p>
            <p>Content</p>
            <p>Content</p>
            <p>Content</p>
        </div>
    </div>
</div>

The layout looks like what I want in Chrome, Firefox and Edge, but when I load it into IE11, the sidebar div is not stretching the vertical length of the page like it should. In fact, it's only 20px tall (which is just the padding).

Why is this happening in IE11 (which I thought supported flexbox), and more inportantly, how can I fix it? Thanks.


Edit: After figuring out a working solution and posting it, I don't think that this question is a duplicate of the ones linked at the top of the question because ultimately, those other posts did not have the same problem or answer the question I was asking.

HartleySan
  • 7,404
  • 14
  • 66
  • 119

4 Answers4

1

Try changing it from flex:1; to flex: 1 0 0;

This link will explain https://stackoverflow.com/a/22883146/6288545

Max Hughes
  • 73
  • 1
  • 7
  • This works great in IE11, but then it causes the sidebar background color to cut off at `height: 100%;` in Chrome when the content is enough that it flows off the screen. Any thoughts? – HartleySan Jan 25 '19 at 14:56
  • 1
    have you tried using ```flex: 1 0 auto;``` or ```flex: 1 0 100%; ``` – Max Hughes Jan 25 '19 at 15:03
  • 1
    I was able to use your answer with Chris W.'s to get what I needed. Thank you so much. – HartleySan Jan 25 '19 at 15:19
1

The problem is just min-height vs height semantics. Try;

body, div, html, h1, h2, li, p, ul {
    margin: 0;
    padding: 0;
}
html,
body {
    height: 100%;
}
.container {
    display: flex;
    flex-direction: column;
    /* min-height: 100%;  <---- Your Culprit */
    height: 100%;
}
.headerContainer {
    background: darkblue;
    color: white;
    padding: 10px;
}
.sidebarAndContentContainer {
    display: flex;
    flex: 1;
}
.sidebar {
    background: gray;
    padding: 10px;
}
.nav {
    color: white;
    list-style: none;
}
.content {
    padding: 10px;
}
<div class="container">
    <div class="headerContainer">
        <h1>Site Title</h1>
    </div>
    <div class="sidebarAndContentContainer">
        <div class="sidebar">
            <ul class="nav">
                <li>Nav Link #1</li>
                <li>Nav Link #2</li>
                <li>Nav Link #3</li>
                <li>Nav Link #4</li>
                <li>Nav Link #5</li>
            </ul>
        </div>
        <div class="content">
            <h2>Page Title</h2>
            <p>Content</p>
            <p>Content</p>
            <p>Content</p>
            <p>Content</p>
        </div>
    </div>
</div>
Chris W.
  • 22,835
  • 3
  • 60
  • 94
  • Overall, I like this answer, but the one problem I see is that when the content scrolls off the page, the sidebar background color stops at where the initial (unscrolled) screen stops (i.e., `height: 100%;`). Any thoughts? – HartleySan Jan 25 '19 at 14:55
  • 1
    Ya, you'll need to eventually learn [how flex works](https://css-tricks.com/snippets/css/a-guide-to-flexbox/) with `grow`, `shrink`, `basis` and `order` and change your overall structure. I wasn't assuming you wanted your current UI stuff changed to that extent, was just answering the question of why it wasn't stretching vertical length of your example page in IE11 :) – Chris W. Jan 25 '19 at 15:03
  • I feel like I understand flexbox and most of its properties pretty well, but `flex` (and the longhand `flex-grow`, `flex-shrink` and `flex-basis` properties) really confuses me. I think it's the interplay of how the three go together and the variations across browsers that makes this really hard. At the end of the day, I was able to use your answer along with Max Hughes's and just played around to get what I wanted (I will post the final solution in a minute). Thanks again for your help, but the `flex-basis` explanation in the linked CSS Tricks article still confuses me. – HartleySan Jan 25 '19 at 15:19
  • Ya it's all kind of a pain in the butt. Hence why personally I just normally go for good old fashioned box model style layouts for the core layout instead of flex. Glad you got a remedy though and have a great weekend! – Chris W. Jan 25 '19 at 15:24
0

IE11 only has partial support for flexbox.

Check out this site if you haven't already: https://caniuse.com/#feat=flexbox

Unless its super critical I dont think it's worth losing too much sleep over supporting a 2 generation old browser that Microsoft has long moved on from.

kevin
  • 2,707
  • 4
  • 26
  • 58
  • 2
    Some folks don't have an option to drop support for it until it reach end of life support from MS, AKA the wonderful world of enterprise dev. ;) – Chris W. Jan 25 '19 at 14:39
  • @ChrisW. dang, that's rough. – kevin Jan 25 '19 at 14:42
  • 2
    Hence why microsoft should occasionally offer to pick up the bar tab for forcing some of us to drink lol. ;) – Chris W. Jan 25 '19 at 14:44
  • 2
    @ChrisW. Well, I guess someone has to make sure the desktop left in the attic for 10 years can still run the company website without issues when booted up. – kevin Jan 25 '19 at 14:47
0

First and foremost, many thanks to Chris W. and Max Hughes for your answers and advice. Without it, I would have never found an answer. With that said, I ended up having to use a combination of both answers to get what I was looking for.

And before I provide the answer, this is what I was looking for that I wasn't getting before:

  • The sidebar to always stretch 100% the vertical space of the screen of whatever is not taken up by the header.
  • The sidebar to stretch to the bottom of the view window regardless of whether the page content causes the page to scroll or not.
  • Something that works in Chrome, Firefox, Edge, IE10 + IE11.

With that said, the following two changes to the code in my original answer is what met all of the above conditions:

.container {
    display: flex;
    flex-direction: column;
    height: 100%; /* Changed from min-height. */
}
...
.sidebarAndContentContainer {
    display: flex;
    flex: 1 0 auto; /* Changed from flex: 1. */
}
HartleySan
  • 7,404
  • 14
  • 66
  • 119
  • There are cleaner and more efficient solutions. See the duplicates I posted. – Michael Benjamin Jan 25 '19 at 17:00
  • Michael_B, could you please offer some more guidance on what you mean by "cleaner and efficient solutions"? The two things I saw were using `flex: auto` instead of `flex: 1 0 auto` and making the `html` and/or `body` elements `display: flex` as well. For the first, there was still some weirdness in IE11, so `flex: auto` is not the same thing as `flex: 1 0 auto`, and I've read in several places (and I agree) that adding `display: flex` to the `html` and `body` elements is probably not ideal. What am I missing? Thank you. – HartleySan Jan 25 '19 at 18:35