【问题标题】:IE 10 flex, margin-left: auto pushing item out of flex containerIE 10 flex,margin-left:自动将项目推出 flex 容器
【发布时间】:2018-03-16 17:38:02
【问题描述】:

我有一个容器可以容纳: - 具有特定宽度 (50px) 并向右浮动的 div。 - 上述div前的一个按钮,表示容器内的剩余宽度。

<div class="responsive-button__container">

  <button>Retarded Button</button>

  <div class="awesome-logo"></div>

</div>

CSS:

.responsive-button__container {
  width: 500px;
  border: 3px solid black;
  display: flex;
  display: -ms-flexbox;
}

button {
  width: 100%;
}

.awesome-logo {
  margin-left: auto;
  width: 50px;
  background: red;
}

它在 Chrome、Safari 和 Firefox 上运行良好。但是在 IE 10 上中断(应该浮动在右侧的 div 显示在容器外)

Codepen:https://codepen.io/d30jeff/pen/GxZPXw

编辑:我决定使用 javascript 来减小按钮大小,这样.awesome-logo 就不会同时被推出容器。

【问题讨论】:

  • 在处理flexbox时,使用“flexbox”标签而不是“flex”.. flex指的是poperty和flexbox的技术
  • @TemaniAfif 哎呀!我会在以后的帖子中记住这一点。

标签: css flexbox internet-explorer-10


【解决方案1】:

我不会为带有 flex 容器的按钮使用 100% 的宽度。 Flex 应该自己管理项目的宽度。如果您没有定义宽度,而是允许调整其大小,它应该可以工作:

button {
  flex-grow:1;
}

https://codepen.io/anon/pen/bvpZYa

【讨论】:

  • 谢谢。但按钮不会占用容器内的可用空白空间。
  • 它在我提供的 codepen 中对我有用。再说一次,我无法尝试使用 IE10,但 IE10 模式下的 IE11 确实提供了填充容器的按钮,除了右侧的红色 div。
【解决方案2】:

flex 的默认值已更改:在 IE10 (*) 中为 flex: 0 0 auto;,但现在为 flex: 0 1 auto; (**)。

(*) 如第一版 CSS Flexbox 布局规范中所述
(**) 在 REC 规范的第三个和最后一个版本中(在现代浏览器中实现的那个)

它被称为flexbug #6(很好的资源)。
如果您支持 IE 10,则相关:永远不要将 flex-basis 写为 0,始终写为 flex-basis: 0%;flex: (unitless positive number) (unitless positive number) 0%;

Codepen
编辑:属性和值的名称在不同版本的规范之间发生了很大变化,我没有使用 Autoprefixer 在我的示例中“翻译”它们,嗯。现在添加到 Codepen 和下面的 sn-p 中:

/* EDIT : Now with an Autoprefixer go-through via Codepen: activated Autoprefixer on Codepen, viewed the compiled CSS and copy-pasted it back in CSS. There now have all the prefixed properties and values for IE10. I guess. */
.responsive-button__container {
  width: 500px;
  border: 3px solid black;
  display: -webkit-box;
  display: flex;
  display: -ms-flexbox;
}

.responsive-button__container > * {
  /* flex: 0 0 auto; IE 10 default value (none in disguise, in the 1st version of the spec) */
  -webkit-box-flex: 0;
      -ms-flex: 0 1 auto;
          flex: 0 1 auto; /* modern default value (in the spec that went to REC) */
}

button {
  width: 100%;
}

.awesome-logo {
  margin-left: auto;
  width: 50px;
  background: red;
}
<div class="responsive-button__container">
  
  <button>Badly displayed button</button>
  
  <div class="awesome-logo"></div>
  
</div>

【讨论】:

  • .awesome-logo div 仍在 IE 10 的容器外显示。
  • 我的错,它需要来自第一个规范的语法和由 Autoprefixer 提供的 -ms- 前缀。我已经习惯了 Gulp 的工作流程,以至于忘记在 Codepen 上激活它……它现在可以工作了吗?
猜你喜欢
  • 2019-01-09
  • 1970-01-01
  • 2017-09-07
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2015-12-10
  • 2015-09-02
相关资源
最近更新 更多