【问题标题】:CSS Grid Layout not working in IE11 even with prefixes即使有前缀,CSS 网格布局也不能在 IE11 中工作
【发布时间】:2017-08-20 21:03:30
【问题描述】:

我正在为我的网格使用以下 HTML 标记。

<section class="grid">
    <article class="grid-item width-2x height-2x">....</article>
    <article class="grid-item">....</article>
    <article class="grid-item">....</article>
    <article class="grid-item width-2x">....</article>
    <article class="grid-item height-2x">....</article>
    <article class="grid-item">....</article>
    <article class="grid-item">....</article>
    <article class="grid-item width-2x height-2x">....</article>
</section>

SCSS 代码如下所示:

.grid {
    display: grid;
    grid-template-columns: repeat( 4, 1fr );
    grid-gap: 30px;
    align-items: start;

    .grid-item {
        &.height-2x {
            grid-row: span 2;
        }
        &.width-2x {
            grid-column: span 2;
        }
    }
}

由于我在我的工作流程中使用了自动前缀,它会自动添加带有 -ms 前缀的所有相关属性。我可以通过检查元素来确认。

现在,问题是这段代码在 Chrome、Firefox 和 Opera 中运行良好,但是当我在 Microsoft Edge 或 IE 11 中打开此页面时,所有网格项在第一个单元格中都相互重叠。根据this site,这些浏览器支持带有-ms 前缀的CSS Grid 布局。我已经为这个场景创建了一个 CodePen。

CodePen Link

为什么它不起作用?

.grid {
  display: -ms-grid;
  display: grid;
  -ms-grid-columns: (1fr)[4];
  grid-template-columns: repeat(4, 1fr);
  -ms-grid-rows: (270px)[4];
  grid-template-rows: repeat(4, 270px);
  grid-gap: 30px;
}

.grid .grid-item {
  background-color: #000;
  color: #fff;
  text-align: center;
  line-height: 270px;
}

.grid .grid-item.height-2x {
  -ms-grid-row: span 2;
  grid-row: span 2;
}

.grid .grid-item.width-2x {
  -ms-grid-column: span 2;
  grid-column: span 2;
}
<section class="grid">
  <article class="grid-item width-2x height-2x">....</article>
  <article class="grid-item">....</article>
  <article class="grid-item">....</article>
  <article class="grid-item width-2x">....</article>
  <article class="grid-item height-2x">....</article>
  <article class="grid-item">....</article>
  <article class="grid-item">....</article>
  <article class="grid-item width-2x height-2x">....</article>
</section>

【问题讨论】:

  • 根据 CanIUse IE 和 Edge 使用旧版本的 Grid 规范。这可能是问题所在。
  • CanIUse 还指定它在 IE 11 和 Edge >16 上部分支持
  • 替代 -ms-* 属性是否映射到旧规范中的相同行为?如果不是,那么添加这些属性有什么意义?
  • CodePen 链接已损坏 :(

标签: html css internet-explorer-11 css-grid


【解决方案1】:

IE11 使用older version of the Grid specification

您使用的属性在旧的网格规范中不存在。使用前缀没有区别。

这是我立即发现的三个问题。


repeat()

repeat() 函数在旧规范中不存在,因此 IE11 不支持。

您需要使用another answer to this post 中介绍的正确语法,或者声明所有行和列的长度。

代替:

.grid {
  display: -ms-grid;
  display: grid;
  -ms-grid-columns: repeat( 4, 1fr );
      grid-template-columns: repeat( 4, 1fr );
  -ms-grid-rows: repeat( 4, 270px );
      grid-template-rows: repeat( 4, 270px );
  grid-gap: 30px;
}

用途:

.grid {
  display: -ms-grid;
  display: grid;
  -ms-grid-columns: 1fr 1fr 1fr 1fr;             /* adjusted */
      grid-template-columns:  repeat( 4, 1fr );
  -ms-grid-rows: 270px 270px 270px 270px;        /* adjusted */
      grid-template-rows: repeat( 4, 270px );
  grid-gap: 30px;
}

旧规格参考: https://www.w3.org/TR/2011/WD-css3-grid-layout-20110407/#grid-repeating-columns-and-rows


span

span 关键字在旧规范中不存在,因此 IE11 不支持它。您必须使用这些浏览器的等效属性。

代替:

.grid .grid-item.height-2x {
  -ms-grid-row: span 2;
      grid-row: span 2;
}
.grid .grid-item.width-2x {
  -ms-grid-column: span 2;
      grid-column: span 2;
}

用途:

.grid .grid-item.height-2x {
  -ms-grid-row-span: 2;          /* adjusted */
      grid-row: span 2;
}
.grid .grid-item.width-2x {
  -ms-grid-column-span: 2;       /* adjusted */
      grid-column: span 2;
}

旧规格参考: https://www.w3.org/TR/2011/WD-css3-grid-layout-20110407/#grid-row-span-and-grid-column-span


grid-gap

旧规范中不存在grid-gap 属性及其常用形式grid-column-gapgrid-row-gap,因此IE11 不支持它们。你必须找到另一种方法来分隔盒子。我还没有阅读整个旧规范,所以可能有一种方法。否则,请尝试使用边距。


网格项自动放置

有一些discussion in the old spec about grid item auto placement,但该功能从未在 IE11 中实现。 (网格项目的自动放置现在是当前浏览器的标准配置)。

因此,除非您专门定义网格项的位置,否则它们将堆叠在单元格 1,1 中。

使用-ms-grid-row-ms-grid-column 属性。

【讨论】:

  • 这是有道理的。但是,我创建了一个具有您建议的替代属性的新笔。它似乎也不起作用:codepen.io/imfaisal/pen/gxeGmQ?editors=1100
  • 对。就像我在回答中所写的那样,这只是我马上看到的三个问题。 grid-gap 在 Edge / IE11 中似乎不起作用,因此您需要找到另一种方法在项目之间创建空间。
  • 我接受你的回答,因为它回应了问题的核心,现在我需要检查我需要使用什么旧规范语法。
  • 除了上述 CSS 语法调整(针对旧规范),还需要使用grid-columngrid-row 值。我更新了我的笔码:codepen.io/imfaisal/pen/gxeGmQ?editors=1100
  • 事实证明 IE11 确实支持重复,但语法略有不同:-ms-grid-columns: (1fr)[5]; css-tricks.com/…
【解决方案2】:

Michael 给出了一个非常全面的答案,但我想指出一些您仍然可以做的事情,以便能够以几乎无痛的方式在 IE 中使用网格。

支持repeat 功能

您仍然可以使用重复功能,它只是隐藏在不同的语法后面。你必须写(1fr)[4],而不是写repeat(4, 1fr)。就是这样。 现状见本系列文章:https://css-tricks.com/css-grid-in-ie-debunking-common-ie-grid-misconceptions/

支持网格间隙

除 IE 之外的所有浏览器都支持网格间隙。因此,您可以使用@supports 规则为所有新浏览器有条件地设置网格间隙:

例子:

.grid {
  display: grid;
}
.item {
  margin-right: 1rem;
  margin-bottom: 1rem;
}
@supports (grid-gap: 1rem) {
  .grid {
    grid-gap: 1rem;
  }
  .item {
    margin-right: 0;
    margin-bottom: 0;
  }
}

这有点冗长,但从好的方面来说,您不必为了支持 IE 而完全放弃网格。

使用自动前缀

这一点我怎么强调都不为过 - 只需在构建步骤中使用自动前缀,就可以解决一半的网格问题。以符合标准的方式编写 CSS,让 autoprefixer 自动完成所有旧规范属性的转换。当您决定不支持 IE 时,只需在 browserlist 配置中更改一行,您就会从构建的文件中删除所有 IE 特定的代码。

【讨论】:

    【解决方案3】:

    Faisal Khurshid 和 Michael_B 已经给出了答案。
    这只是试图使可能的解决方案更加明显。

    对于 IE11 及以下版本,您需要在父 div 中启用网格的旧规范,例如身体或像这里的“网格”这样:

    .grid-parent{display:-ms-grid;}
    

    然后定义列和行的数量和宽度,例如所以:

    .grid-parent{
      -ms-grid-columns: 1fr 3fr;
      -ms-grid-rows: 4fr;
    }
    

    最后你需要明确告诉浏览器你的元素(项目)应该放在哪里,例如像这样:

    .grid-item-1{
      -ms-grid-column: 1;
      -ms-grid-row: 1;
    }
    
    .grid-item-2{
      -ms-grid-column: 2;
      -ms-grid-row: 1;
    }
    

    【讨论】:

    • 请务必注意,您必须为每个网格项定义-ms-grid-column,因为 IE 不支持自动放置(所有将放置在第一列中)。
    • Woof,这太糟糕了,它不会自动放置。几乎最好(对于我正在处理的小型演示)进行浮动和清除,因为您必须做的更少(在 5x5 网格中布置 25 个元素)!
    【解决方案4】:

    为了支持 IE11 的自动布局,我每次在 仅 1 维中使用网格布局时都将 grid 转换为 table 布局。我还使用了margin 而不是grid-gap

    结果是一样的,看你怎么弄到这里https://jsfiddle.net/hp95z6v1/3/

    【讨论】:

      猜你喜欢
      • 2018-01-28
      • 1970-01-01
      • 2019-04-12
      • 2018-04-16
      • 2016-10-17
      • 2019-11-22
      • 2018-04-21
      相关资源
      最近更新 更多