【问题标题】:IE11 table layout breaks when ancestor has display: flex [duplicate]祖先显示时 IE11 表格布局中断:flex [重复]
【发布时间】:2018-03-15 05:58:56
【问题描述】:

我有一个在 Chrome 中正确布局的简单表格。但是在 IE11 中,当祖先元素具有 display: flex 时,布局会被破坏。

页面底部的输入框应距表格标题底部10px。为此,我在标题文本本身上设置了margin-bottom,以为输入框(过滤器)腾出空间,然后我将它们绝对定位到th 元素的底部,这些元素有position:relative 来锚定它们.

这里是链接: https://codepen.io/nfknight/pen/KXyKdw

任何帮助表示赞赏!或者我很好奇是否还有其他关于如何使用固定过滤器元素完成可变高度标题文本的想法。 header-text 元素和 filter 元素都必须是同级元素。

【问题讨论】:

标签: html css internet-explorer flexbox


【解决方案1】:

这不是 flex 祖先的问题。删除它似乎对 IE11 或 Chrome 没有任何影响。看起来是绝对定位的渲染差异。

您可以通过使列成为弹性容器来解决此问题。

revised codepen

.flexAncestor {
  display: flex;
}

tr {
  display: flex;     /* gives full height to children */
  height: 250px;     /* for demo only */
}

.col1 {
  display: inline-flex;
  flex-direction: column;
  width: 100px;
  background: pink;
}

.col2 {
  display: inline-flex;
  flex-direction: column;
  width: 100px;
  background: lightblue;
}

.ui-column-title {
  margin: auto;     /* flex auto margin; see below */
}

.ui-column-filter {
  align-self: center;
  margin-bottom: 10px;
  width: calc(100% - 15px);
}
<div class="flexAncestor">
  <div class="tableContainer">
    <table class="testDataTable">
      <thead>
        <tr>
          <th class="col1">
            <span class="ui-column-title">id</span>
            <input class="ui-column-filter" type="text" />
          </th>
          <th class="col2">
            <span class="ui-column-title">Wrapping column header inside flex ancestor throws layout off in IE11 </span>
            <input class="ui-column-filter" type="text" />
          </th>
        </tr>
      </thead>
    </table>
  </div>
</div>

在此处了解有关 flex auto 边距的更多信息:

【讨论】:

  • 我决定以一个骗子的身份结束,因为那个骗子很好地描述了它。尽管如此,我还是回答了我找到的解决方案,该解决方案在骗局中不存在,并且为您的解决方案 +1。
  • 如果我在祖先上注释掉 display:flex ,问题就不存在了。请注意,codepen 似乎并不总是在 IE 中自动重新加载。
【解决方案2】:

如前所述,它与 Flexbox 无关,它基于 position: relative 对表格元素的影响是未定义的,这里有很好的描述:


在这种情况下,事实证明,在堆栈向上固定height: 100% 可以解决绝对定位问题,因此如果添加此规则,它也可以在 IE 中工作。

.tableContainer, table, thead, tr, th { height: 100% }

源:https://davidwalsh.name/table-cell-position-absolute

堆栈sn-p

.flexAncestor {
  display: flex;
}

.tableContainer {
  flex: 1 0 auto; /* not sure if needed. */
}

.tableContainer, table, thead, tr, th {            /*  added  */
  height: 100%
}

.ui-column-title {
  margin-bottom: 100px;
  display: inline-block;
  white-space: normal;
}

.ui-column-filter {
  position: absolute;
  bottom: 10px;
  left: 5px;
  width: calc(100% - 15px);
}

.testDataTable th {
  position: relative;
}

.col1 {
  width: 100px;
  background: pink;
}

.col2 {
  width: 100px;
  background: lightblue;
}
<div class="flexAncestor">
  <div class="tableContainer">
    <table class="testDataTable">
      <thead>
        <tr>
          <th class="col1">
            <span class="ui-column-title">id</span>
            <input class="ui-column-filter" type="text" />
          </th>
          <th class="col2">
            <span class="ui-column-title">Wrapping column header inside flex ancestor throws layout off in IE11 </span>
            <input class="ui-column-filter" type="text" />
          </th>
        </tr>
      </thead>
    </table>
  </div>
</div>

【讨论】:

  • 如果我在祖先上注释掉 display:flex ,问题就不存在了。请注意,codepen 似乎并不总是在 IE 中自动重新加载。在链上添加 height:100% 似乎在 codepen 示例中有效,但在我的应用程序中不起作用。所以我仍在追查问题所在。
  • @NateK 由于我们提供了 2 种方法使其工作,如果您发布的代码不是真实代码的子集,我们将无法做更多。另外,如果把Codepen上的display: flex去掉,还是不行,证明和Flexbox没有关系,我在回答的第一部分也解释过。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
  • 2017-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多