IE 11 flexbox child 宽度不考虑内容(Safari 也是)

IE 11 flexbox child width not respecting content (Safari too)

我有一个奇怪的错误,我似乎一直在绊倒它,我想知道这里到底发生了什么。我有一个带有两个 child divs (flex: 1) 的 flexbox 容器(换行)。当我有一个我不想换行的标题时 (white-space: nowrap),包含 div 的 IE 11 不尊重它的宽度并缩小到小于 header 宽度。这似乎在 Chrome 中工作得很好,但 Safari 和 IE 似乎不尊重标题的宽度。这是怎么回事?

这是 js 库:https://jsbin.com/bowodiz/edit?css,output

为了方便起见,我已将主要样式和标记放在下面。

HTML:

<div class="flex-container">

    <div class="left">
      <div class="heading">Here is a heading that shouldn't wrap</div>
      <div class="sub-text">This is just some content that should wrap just fine ..</div>
    </div>

    <div class="right">
      <div class="heading">Here is a heading that shouldn't wrap</div>
      <div class="sub-text">This is just some content that should wrap just fine ...</div>
    </div>

</div>

CSS(部分):

.flex-container {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-between;
}

.flex-container > div {
  flex: 1;
}

.heading {
  white-space: nowrap;
}

期望:

IE/Safari 行为:

而不是 flex: 1 使用 flex-grow: 1; flex-basis: 0; 或为 shorthand 指定您想要的所有 3 个值。如果您指定 flex-basis,请确保您指定了一个单位。 https://jsbin.com/ketolifuhu/edit?html,css,output

这是一篇很好的文章,涵盖了一些错误和不一致之处https://philipwalton.com/articles/normalizing-cross-browser-flexbox-bugs/

:root {
    --dark-primary-color: #303F9F;
    --default-primary-color: #3F51B5;
    --light-primary-color: #C5CAE9;
    --text-primary-color: #FFFFFF;
    --accent-color: #FF4081;
    --primary-text-color: #212121;
    --secondary-text-color: #757575;
    --divider-color: #BDBDBD;
    --box-size: 150px;
    font-family: Roboto, 'sans-serif';
    font-size: 20px;
    letter-spacing: 1.25px;
    font-weight: 100;
    color: white;
}

body {
  background-color: #BDBDBD;
}

.flex-container {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-between;
}

.flex-container > div {
  flex-grow: 1;
  margin: 10px;
  box-shadow: 2px 2px 6px 1px rgba(0,0,0,.2);
  padding: 30px;
  flex-basis: 0;
}

.left {
  background-color: #3F51B5;
}

.right {
  background-color: #3F51B5;
}

.heading {
  letter-spacing: .5px;
  font-weight: 400;
  white-space: nowrap;
}

.sub-text {
  margin-top: 20px;
  font-size: 14px;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <link href="https://fonts.googleapis.com/css?family=Roboto:100,400" rel="stylesheet">
  <title>JS Bin</title>
</head>
<body>

  <div class="flex-container">
    
    <div class="left">
      <div class="heading">Here is a heading that shouldn't wrap</div>
      <div class="sub-text">This is just some content that should wrap just fine and I don't really care what it says, but it'll just wrap just fine and that behavior should be handled by the heading.</div>
    </div>
    <div class="right">
      <div class="heading">Here is a heading that shouldn't wrap</div>
      <div class="sub-text">This is just some content that should wrap just fine and I don't really care what it says, but it'll just wrap just fine and that behavior should be handled by the heading.</div>
    </div>
    
  </div>
  
  
</body>
</html>

您应该避免使用 flex: 1 shorthand,因为一些旧版本的浏览器有不同的默认值,这会给您带来问题。

很多人都提到指定flex-basis,但你需要指定单位。 IE。不要写flex-basis: 0,写flex-basis: 0%flex-basis: 0px,否则某些浏览器将不知道如何处理0值。

现在进入重点,为了避免溢出,我通常只在 flex-child 上设置 max-width: 100%。 如果 flex-child 有填充,例如 padding-left: 10px; padding-right:10px; 然后将 max-width 设置为 100% 减去填充!即...

padding: 0 10px;
max-width: calc(100% - 20px);

现在,如果您执行这些步骤并删除 white-space: nowrap,那么您将摆脱错误。

(投我赞成票,投我反对票...但我只是想说,是什么帮助了我¯_(ツ)_/¯)

更改子元素上的 flex-property
flex: auto

至:

flex: 100%   (or  1 1 100%, if you prefer) 

帮我解决了问题。

问题仅在 IE11 下存在,在 Edge 下不存在。