css - flex-shrink 不会在 IE 10/11 中缩小元素并导致父级比父级宽

标签 css flexbox internet-explorer-11 internet-explorer-10

我的布局由一行三列组成。

Left 和 right 应该与其内容一样宽。其余宽度应由标题列占据。所有列的总宽度不应比父列宽。

所有列应该只有一行内容,没有换行符。

如果标题列的内容不符合约束条件,则应在末尾添加省略号。

所有这些在 Chrome 和 Edge 中都有效,但 IE 拒绝缩小标题列以使列适合其父级的宽度。

html,
body {
  width: 100%;
  margin: 0;
}

.row {
  display: -ms-flexbox;
  display: flex;
  /* https://css-tricks.com/snippets/css/a-guide-to-flexbox/ */
  width: 200px;
}

[class*='cell-'] {
  -ms-flex: 0 0 auto;
      flex: 0 0 auto;
  /* https://css-tricks.com/flexbox-truncated-text/ */
  min-width: 0;
}

.cell-left {
  background: red;
}

.cell-title {
  white-space: nowrap;
  -ms-flex: 0 1 auto;
      flex: 0 1 auto;
}

.hover {
  overflow-x: hidden;
  text-overflow: ellipsis;
  position: relative;
  padding-right: 1em;
}

.chevron {
  position: absolute;
  right: 0;
}

.hover:hover {
  background: green;
}

.cell-right {
  background: yellow;
  margin-left: auto;
}
<div class="row">
  <div class="cell-left">Left</div>
  <div class="cell-title">
    <div class="hover">Pellentesque habitant morbi tristique senectus et netus et malesuada<span class="chevron">^</span></div>
  </div>
  <div class="cell-right">Right</div>
</div>

最佳答案

IE 10/11 需要在中间项上指定宽度。对您的代码进行此调整

.row {
  display: flex;
  width: 200px;
}

.cell-title {
  width: 100%;
  min-width: 0;
  display: flex;
}

.hover {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cell-title::after {
  content: '^';
  padding-left: 5px;
  margin-right: auto;
}

.hover:hover { background: green; }
.cell-left   { background: red; }
.cell-right  { background: yellow; }
html, body   { margin: 0; }
<div class="row">
  <div class="cell-left">Left</div>
  <div class="cell-title">
    <div class="hover">Pellentesque habitant morbi tristique senectus et netus et malesuada</div>
  </div>
  <div class="cell-right">Right</div>
</div>

jsFiddle demo

关于css - flex-shrink 不会在 IE 10/11 中缩小元素并导致父级比父级宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42914278/

相关文章:

html - 在最后一行调整 flex 元素的大小

javascript - 如何使用 flex-box 容器在多行上保留间距模板

html - 使用位置为 :sticky 的 Flexbox

javascript - MutationObserver 在 IE11 中挂起

javascript - $(item).load() 脚本中断(仅在 IE11 中),适用于开发人员工具

html - 在调整窗口大小时将按钮 div 保持在静态位置

javascript - 如何在 jQuery 中获取溢出元素的可见高度

jquery - 如何使菜单始终附加在顶部

javascript - 使用 jQuery 删除 div 不透明度并在样式中应用指针事件

internet-explorer - IE 11 向不同的子域发送不同的 User-Agent header