Is there a way to use both or either display: grid/-ms-grid
into one style sheet or do I have to use an HTML hack or JavaScript to query what type of browser a page is being viewed with grid layout?
Example:
The following styling doesn't seem to work
.container {
display: grid -ms-grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(150px, 50px);
grid-gap: 1vw;
-ms-grid-columns: repeat(4, 1fr);
-ms-grid-rows: repeat(150px, 50px);
-ms-grid-gap: 1vw;
}
Your
display
rule needs to be structured correctly. What you have is invalid.Also, your
grid-template-rows
rule is invalid. The first argument is supposed to be an integer that specifies the number of repetitions.Also, I don't believe the
repeat()
notation existed in the older specs. It looks like it was introduced in the current spec, so IE wouldn't support it.Lastly, it's best to put the official (W3C) properties after the prefixed versions so they are given priority in the cascade (more details).
Try this:
Transforming new CSS Grid layout syntax to outdated IE's/Edge's is really a challenge. It's not just the matter of adding some vendor prefixes.
IE/Edge has very limited support of what is present in new version of CSS Grid Layout. There is no IE/Edge support of
grid-auto-flow
CSS property);repeat
function and some special values likeauto-fill
andauto-fit
). In some cases this mean that you'll just have to replace with explicit values, like in your case, you can replacegrid-template-columns: repeat(4, 1fr)
with-ms-grid-columns: 1fr 1fr 1fr 1fr
and this will work perfectly. But if you have something likegrid-template-columns: repeat(auto-fill, 1fr)
it's impossible to implement this in IE/Edge;grid-row-gap
,grid-column-gap
,grid-gap
CSS properties). Gaps can be faked using additional grid tracks;grid-auto-columns
,grid-auto-rows
CSS properties);grid-area
,grid-template-areas
CSS properties).You just have to forget about this possibilities for IE.
Also some values of supported IE properties are not supported
Autoplacement
There is no auto-placement behaviour in IE/Edge implementation. This means that you have to position everything rather than use the auto-placement ability of grid.
If you don’t position items they will stack up in the first cell of the grid. That means that you have to set position explicitly for every single grid item or it will reside in first cell. If you have markup like this:
You'll see something this in IE/Edge
So basically you have two options (methodologies) when developing CSS Grid for IE/Edge (if you know that layout in your case can be transformed):
Generate different markup for IE/Edge browser and other browsers. In this case you don't care about markup similarity (by the way your value of
grid-template-rows: repeat(150px, 50px)
is invalid, so I assume you wantedgrid-template-rows: 150px 50px
). Demo for you caseGenerate very similar markup for IE/Edge browsers. In this case markup for all browsers will look very similar. This might be more maintainable because you shouldn't care about different approaches. Demo for you case:
The answer by Vadim is pretty much what you should do. But there are a few more CSS tricks you can use to ease your pain.
0. Be sure to read this blog post to decide whether you want to use grids for websites which support IE: https://rachelandrew.co.uk/archives/2016/11/26/should-i-try-to-use-the-ie-implementation-of-css-grid-layout/
If your answer to the previous question is "Yes", read on:
A very trusty companion in cases when you want to write spec-compliant CSS, but still support IE is the
@supports()
at-rule. I use it to use the more advanced grid properties such asgrid-gaps
, etc: