IE 11 flexbox子宽度不尊重内容(Safari也是)

时间:2017-03-30 04:14:12

标签: html css internet-explorer flexbox overflow

我有一个奇怪的错误,我似乎一直在磕磕绊绊,我想知道这里到底发生了什么。我有一个带有两个子div(flex:1)的flexbox容器(行换行)。当我有一个我不想包装的标题(白色空间:nowrap)时,包含div的IE 11并不尊重它的宽度并缩小到小于标题宽度。这似乎在Chrome中运行得很好,但Safari和IE似乎不尊重标题的宽度。这里发生了什么?

这是js bin: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;
}

渴望:

Desired side by side state

Desired wrapped state

IE / Safari行为:

enter image description here

3 个答案:

答案 0 :(得分:4)

而不是flex: 1使用flex-grow: 1; flex-basis: 0;或指定您想要的所有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>

答案 1 :(得分:0)

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

很多人都提到指定flex-basis,但是你需要指定单位。即不要写“flex-basis:0”,写“flex-basis:0%”或“flex-basis:0px”,否则有些浏览器不会知道如何处理0值。

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

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

现在,如果您执行这些步骤并删除白色空间nowrap,那么您将摆脱错误。

答案 2 :(得分:-1)

(投票给我,投票给我......但我只是说,是什么帮助了我__(ツ)_ /¯)

更改子元素的弹性属性,

flex: auto

收件人:

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

为我解决了这个问题。

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