CSS Grid Layout not working in IE11 even with prefixes

前端 未结 4 2113
长发绾君心
长发绾君心 2020-11-22 08:28

I\'m using following HTML markup for my grid.

....
4条回答
  •  心在旅途
    2020-11-22 08:49

    Michael has given a very comprehensive answer, but I'd like to point out a few things which you can still do to be able to use grids in IE in a nearly painless way.

    The repeat functionality is supported

    You can still use the repeat functionality, it's just hiding behind a different syntax. Instead of writing repeat(4, 1fr), you have to write (1fr)[4]. That's it. See this series of articles for the current state of affairs: https://css-tricks.com/css-grid-in-ie-debunking-common-ie-grid-misconceptions/

    Supporting grid-gap

    Grid gaps are supported in all browsers except IE. So you can use the @supports at-rule to set the grid-gaps conditionally for all new browsers:

    Example:

    .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;
      }
    }
    

    It's a little verbose, but on the plus side, you don't have to give up grids altogether just to support IE.

    Use Autoprefixer

    I can't stress this enough - half the pain of grids is solved just be using autoprefixer in your build step. Write your CSS in a standards-complaint way, and just let autoprefixer do it's job transforming all older spec properties automatically. When you decide you don't want to support IE, just change one line in the browserlist config and you'll have removed all IE-specific code from your built files.

提交回复
热议问题