How to make a sticky footer using flexbox in IE11?

后端 未结 5 1507
春和景丽
春和景丽 2020-11-29 08:56

I\'m trying to make a simple design with flexbox but I\'m having trouble with IE11. Basically, I want a footer that sticks to the bottom only if the content is not high enou

相关标签:
5条回答
  • 2020-11-29 09:00

    On main, instead of flex: 1 use flex: auto. That should be all you need.


    The flex: 1 shorthand rule breaks down to:

    • flex-grow: 1
    • flex-shrink: 1
    • flex-basis: 0

    The flex: auto shorthand rule breaks down to:

    • flex-grow: 1
    • flex-shrink: 1
    • flex-basis: auto

    IE has trouble parsing flex-basis: 0.

    More information:

    • flex property not working in IE
    0 讨论(0)
  • 2020-11-29 09:11

    This answer (@ceindeg answer) partially worked for me, but shrunk the parent container size, and I had a background I wanted to position at the bottom.


    So I just went to position: absolute for the footer only for IE.

    You can use a media-query only for IE, so the other browsers work fine (take a look here: How to target only IE (any version) within a stylesheet?).

    In my case, I wanted to aim IE10 and IE11, so I used this:

    @media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
      footer {
       position: absolute;
       bottom: 0;
      }
    
    
      .parent-container {
        position: relative
        // Add some padding-bottom to the parent container so it won't be glued together
        padding-bottom: 30px;
      }
    }
    
    0 讨论(0)
  • 2020-11-29 09:13

    A solution that really helped me (may not be applicable in all cases) is adding an arbitrary fixed height in pixels - the min-height overrides the fixed height so there's no cropped content. Here's a CSS example:

    .fullheight {
        min-height: 100vh;
        height: 200px; /*IE 11 flexbox min-height fix*/
        display: flex;
        align-items: center;
        justify-content: center;
        flex-wrap: wrap;
        align-content: center;
    }
    
    0 讨论(0)
  • 2020-11-29 09:15

    IE has a min-height bug and needs display: flex on the flex column containers parent, in this case the html

    Fiddle demo

    Update your CSS like this

    *,
    *:after,
    *:before {
      box-sizing: border-box;
    }
    html, body {
      margin: 0;
      display: flex;
    }
    body {
      flex-direction: column;
      min-height: 100vh;
    }
    main {
      flex-grow: 1;
    }
    <header>
      Header
    </header>
    <main>
      <p>Main</p>
      <p>Main</p>
      <p>Main</p>
      <p>Main</p>
      <p>Main</p>
    </main>
    <footer>
      Footer
    </footer>

    0 讨论(0)
  • 2020-11-29 09:21

    Since this might be a desired solution: if you don't necessarily want grow the main tag but still align the footer at the bottom:

    html, body {
      margin: 0;
      display: flex;
    }
    html {
      height: 100%;
    }
    body {
      flex-flow: column nowrap;
      flex-grow: 1;
    }
    footer {
      margin-top: auto;
    }
    <header>
      Header
    </header>
    <main>
      <p>Main</p>
      <p>Main</p>
      <p>Main</p>
      <p>Main</p>
      <p>Main</p>
    </main>
    <footer>
      Footer
    </footer>

    0 讨论(0)
提交回复
热议问题