CSS Grid Layout not working in IE11 even with prefixes
Solution 1
IE11 uses an older version of the Grid specification.
The properties you are using don't exist in the older grid spec. Using prefixes makes no difference.
Here are three problems I see right off the bat.
repeat()
The repeat()
function doesn't exist in the older spec, so it isn't supported by IE11.
You need to use the correct syntax, which is covered in another answer to this post, or declare all row and column lengths.
Instead of:
.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;
}
Use:
.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;
}
Older spec reference: https://www.w3.org/TR/2011/WD-css3-grid-layout-20110407/#grid-repeating-columns-and-rows
span
The span
keyword doesn't exist in the older spec, so it isn't supported by IE11. You'll have to use the equivalent properties for these browsers.
Instead of:
.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;
}
Use:
.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;
}
Older spec reference: https://www.w3.org/TR/2011/WD-css3-grid-layout-20110407/#grid-row-span-and-grid-column-span
grid-gap
The grid-gap
property, as well as its long-hand forms grid-column-gap
and grid-row-gap
, don't exist in the older spec, so they aren't supported by IE11. You'll have to find another way to separate the boxes. I haven't read the entire older spec, so there may be a method. Otherwise, try margins.
grid item auto placement
There was some discussion in the old spec about grid item auto placement, but the feature was never implemented in IE11. (Auto placement of grid items is now standard in current browsers).
So unless you specifically define the placement of grid items, they will stack in cell 1,1.
Use the -ms-grid-row
and -ms-grid-column
properties.
- CSS Grid auto placement in IE/EDGE
- CSS Grid not working in ie11 despite prefixes
- https://www.w3.org/TR/2011/WD-css3-grid-layout-20110407/#automatic-placement-of-grid-items
Solution 2
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.
Solution 3
The answer has been given by Faisal Khurshid and Michael_B already.
This is just an attempt to make a possible solution more obvious.
For IE11 and below you need to enable grid's older specification in the parent div e.g. body or like here "grid" like so:
.grid-parent{display:-ms-grid;}
then define the amount and width of the columns and rows like e.g. so:
.grid-parent{
-ms-grid-columns: 1fr 3fr;
-ms-grid-rows: 4fr;
}
finally you need to explicitly tell the browser where your element (item) should be placed in e.g. like so:
.grid-item-1{
-ms-grid-column: 1;
-ms-grid-row: 1;
}
.grid-item-2{
-ms-grid-column: 2;
-ms-grid-row: 1;
}
Solution 4
To support IE11 with auto-placement, I converted grid
to table
layout every time I used the grid layout in 1 dimension only. I also used margin
instead of grid-gap
.
The result is the same, see how you can do it here https://jsfiddle.net/hp95z6v1/3/
![Faisal Khurshid](https://i.stack.imgur.com/4U20L.jpg?s=256&g=1)
Faisal Khurshid
Updated on July 05, 2022Comments
-
Faisal Khurshid almost 2 years
I'm using following HTML markup for my grid.
<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>
The SCSS code is something like below:
.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; } } }
Since I'm using auto-prefixer in my workflow it automatically adds all relevant properties with
-ms
prefix. I can confirm it via inspect element.Now, the issue is this code works just fine in Chrome, Firefox and Opera, but when I open this page in Microsoft Edge or in IE 11 all grid items are overlapping each other at first cell. According to this site these browsers support CSS Grid layout with
-ms
prefix. I've created a CodePen for this scenario.Why is it not working?
.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>
-
Faisal Khurshid almost 7 yearsThat makes sense. However, I've created a new pen with the alternative properties which you've suggested. It does not seem to be working either: codepen.io/imfaisal/pen/gxeGmQ?editors=1100
-
Michael Benjamin almost 7 yearsRight. Like I wrote in my answer, those were just three problems I saw right away.
grid-gap
doesn't appear to work in Edge / IE11, so you'll need to find another method to create space between the items. -
Faisal Khurshid almost 7 yearsI agree, but I'm not using
grid-gap
in this pen, other properties with your suggested syntax do not seem to be working. The old spec syntax should work in IE 11 and Edge. -
Faisal Khurshid almost 7 yearsI'm accepting your answer as it responds to the core of the issue, now I'll need to check what old spec syntax I need to use exactly.
-
Faisal Khurshid almost 7 yearsIn addition to the above-mentioned CSS syntax adjustments (for old specs), each grid-item also needs to be explicitly given a value for their placement in the column and row in the defined grid using the
grid-column
andgrid-row
values. I've updated my pen code: codepen.io/imfaisal/pen/gxeGmQ?editors=1100 -
Damien Roche about 6 years..is it even worth going down this road? I've been using 3rd party grid systems (bootstrap, etc) which have been fighting browser compatibility for years. I'd love to do away with them and use native css grid.. but.. yuk. What's the point if I have to specifically position elements? I'm using a grid system so I don't need to think about this stuff.
-
David Undy almost 6 yearsIt turns out IE11 does actually support repeat, but the sytax is slightly different: -ms-grid-columns: (1fr)[5]; css-tricks.com/…
-
sravan ganji over 5 yearswhat if we have like this
-ms-grid-rows: repeat(2, auto);
can i use some thing like this-ms-grid-rows: 1fr 1fr;
but how it will adjust to the content -
Fabian von Ellerts about 5 yearsIt's important to note that you have to define
-ms-grid-column
for every grid item, because IE doesn't understand automatic placement (all will be placed in the first column). -
redOctober13 over 4 yearsWoof, that's pretty awful it doesn't do auto placement. It's almost preferable (for my small demo I'm working on) to do floats and clears as you'd have to do less (laying out 25 elements in a 5x5 grid)!
-
Sousuke over 2 yearsTo get columns with gap you need something like this:
-ms-grid-columns: 1fr 30px 1fr 30px 1fr 30px 1fr;