Absolutely positioned flex item is not removed from the normal flow in IE11

后端 未结 4 691
误落风尘
误落风尘 2020-11-22 08:52

We have two divs with content and a third div that is a background with absolute position.

Container is a flexbox.

All works fine in Chrome and Safari, but <

4条回答
  •  花落未央
    2020-11-22 09:25

    UPDATE: This issue has been resolved in Firefox (as of v52, released March 2017). The problem still exists in IE11.


    Like you wrote in the question:

    Firefox calculates absolute positioned div, and distributes space between divs like there are 3 divs in a row.

    Firefox is considering the third div (.bg), which is absolutely positioned, an in-flow flex item and factoring it into its space-between calculation. (IE11 does this, too; Chrome and Edge ignore it.)

    Clearly, this is not in compliance with the current flexbox spec:

    4.1. Absolutely-Positioned Flex Children

    As it is out-of-flow, an absolutely-positioned child of a flex container does not participate in flex layout.

    Here are some workarounds:

    Why not move the absolutely positioned div between the other two?

    Instead of this:

    Content 1
    Content 2
    Background

    Try this:

    Content 1
    Background
    Content 2

    div.container {
      display: flex;
      flex-direction: row;
      width: 100%;
      height: 300px;
      justify-content: space-between;
      width: 100%;
      outline: 1px solid;
    }
    div.c1 {
      background: #aaeecc;
      width: 100px;
      position: relative;
      z-index: 50;
      top: 20px;
      display: flex;
    }
    div.c2 {
      background: #cceeaa;
      width: 200px;
      position: relative;
      z-index: 50;
      top: 20px;
      display: flex;
    }
    div.bg {
      background: #ccc;
      width: 100%;
      height: 100%;
      z-index: 0;
      left: 0px;
      top: 0px;
      position: absolute;
      display: flex;
    }
    Content 1
    Background
    Content 2

    OR... remove .bg from the flex container:

    Content 1
    Content 2
    Background

    div.container {
      display: flex;
      flex-direction: row;
      width: 100%;
      height: 300px;
      justify-content: space-between;
      width: 100%;
      outline: 1px solid;
    }
    div.c1 {
      background: #aaeecc;
      width: 100px;
      position: relative;
      z-index: 50;
      top: 20px;
      display: flex;
    }
    div.c2 {
      background: #cceeaa;
      width: 200px;
      position: relative;
      z-index: 50;
      top: 20px;
      display: flex;
    }
    div.bg {
      background: #ccc;
      width: 100%;
      height: 100%;
      z-index: 0;
      left: 0px;
      top: 0px;
      position: absolute;
      display: flex;
    }
    Content 1
    Content 2
    Background

    OR... use the flex order property to re-arrange the flex items.

    Add this to your code:

    .c2 { order: 1; }
    

    div.container {
      display: flex;
      flex-direction: row;
      width: 100%;
      height: 300px;
      justify-content: space-between;
      width: 100%;
      outline: 1px solid;
    }
    div.c1 {
      background: #aaeecc;
      width: 100px;
      position: relative;
      z-index: 50;
      top: 20px;
      display: flex;
    }
    div.c2 {
      background: #cceeaa;
      width: 200px;
      position: relative;
      z-index: 50;
      top: 20px;
      display: flex;
      order: 1;
    }
    div.bg {
      background: #ccc;
      width: 100%;
      height: 100%;
      z-index: 0;
      left: 0px;
      top: 0px;
      position: absolute;
      display: flex;
    }
    Content 1
    Content 2
    Background

提交回复
热议问题