How to refactor CSS Grid for IE11 Compatibility - css

I'm not very familiar with CSS and thought I had hit the jackpot with CSS grid however I did not realise that it wasn't very compatible with IE11. I have run into a number of problems trying to fix the following code:
.my-grid-filters {
display: grid;
grid-auto-rows: minmax(50px, 50px);
grid-gap: 5px;
}
.my-grid-filters filter {
display: flex;
align-items: flex-end;
}
#media screen and (min-width: 40em) {
.my-grid-filters {
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
grid-gap: 5px;
}
.my-grid-filters filter:nth-child(1) {
grid-column: 1 / -1;
grid-row: span 2;
}
}
I ran it through the Autoprefixer online tool which produced the following:
.my-grid-filters {
display: -ms-grid;
display: grid;
grid-auto-rows: minmax(50px, 50px);
grid-gap: 5px;
}
.my-grid-filters filter {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: end;
-ms-flex-align: end;
align-items: flex-end;
}
#media screen and (min-width: 40em) {
.my-grid-filters {
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
grid-gap: 5px;
}
.my-grid-filters filter:nth-child(1) {
grid-column: 1 / -1;
-ms-grid-row-span: 2;
grid-row: span 2;
}
}
However this seems to stack all of the grid elements on top of each other. This article includes a lot of information but I'm struggling to apply it to my code.
It seems to suggest that I need to place each element in the grid manually but part of my use case for CSS Grid is that I don't know how many of the repeat elements might be on the screen at once as they are generated by iterative javascript. This also makes it difficult for me to apply separate div ids too them.
Any help would be appreciated

Related

CSS Grid not converting from a single column to a single row as a result of a media query

<div class="container">
<div class="wrapper">
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
</div>
</div>
.circle {
width: 100px;
height: 100px;
border-radius: 50%;
background: red;
}
.wrapper {
display: grid;
grid-template-columns: 1fr;
justify-items: center;
align-items: space-around;
min-width: 300px;
height: 70vh;
background: whitesmoke;
}
#media screen and (min-width: 1100px) {
.container {
height: auto;
display: grid;
grid-template-columns: 1fr repeat(2, minmax(auto, 30rem)) 1fr;
background: pink;
}
.wrapper {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-column: 2/4;
justify-content: center;
background: cyan;
}
}
I've created a column that contains three circles in it, each stacked on top of the other in a column, which all looks fine when the screen is narrow. But when the browser is widened and I add a media query for when the screen gets wider than 1100px, I want the column of circles to flip to become a single row of circles.
But when I do this using CSS Grid, it doesn't work, and two circles appear on one row, and the third circle appears below the first circle on a second row. You can see it at https://codepen.io/HorrieGrump/pen/ZEKxJgv
I can get it to work if I use flexbox instead (as shown below) by swapping out the current .wrapper block in CSS and using this new one with flexbox, but I'd like to know if it's possible to use CSS Grid instead of flexbox to do this.
Can someone please let me know how to get the media query to flip the column into a single row using CSS Grid – and not have to resort to flexbox?
.wrapper {
display: flex;
flex-direction: row;
grid-template-columns: repeat(2,1fr);
justify-content: space-around;
align-items: center;
grid-column: 2/4;
background: cyan;
}
Edit your media query for .wrapper
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
align-items: center;
grid-column: 2/4;
background: cyan;
}

How to make CSS grid working in react with CSS modules stylesheet

We have a css file with grid defined and it was working fine previously. Recently we are trying to use CSS module and changed the file name from sonar-landing-page.css to sonar-landing-page.module.css and import it in the js file like the code sample below then it stopped working, the css module itself seems to be working fine as we added a simple .error style like the one below and the red color is showing correctly but it is just the grid not working, can anyone shed some lights? thanks!
sonar-landing-page.module.css
.sonar-landing-wrapper {
display: grid;
grid-gap: 2rem;
justify-items: stretch;
align-items: stretch;
grid-template-columns: repeat(6, 1fr);
grid-column-gap: 5px;
grid-auto-flow: row;
}
.sonar-history-job-table {
grid-row: 2;
grid-column: 1 / 7;
}
// test style I added to test if the css module itself is working
.error {
color: red;
}
sonar-landing-page.js
import styles from './sonar-landing-page.module.css'
export default function SonarLandingPage() {
return (
<div className={styles.sonarLandingWrapper}>
<div className={styles.sonarNewJobButton}>
...
</div>
</div>
)
}
write your css styling as the same you use in component
example .sonar-landing-wrapper ---> .sonarLandingWrapper
.sonarLandingWrapper {
display: grid;
grid-gap: 2rem;
justify-items: stretch;
align-items: stretch;
grid-template-columns: repeat(6, 1fr);
grid-column-gap: 5px;
grid-auto-flow: row;
}
.sonarHistoryJobTable {
grid-row: 2;
grid-column: 1 / 7;
}
// test style I added to test if the css module itself is working
.error {
color: red;
}

CSS Grid without Media Queries?

I have a simple grid, below, which works but I've been scratching my head on how to eliminate the media queries. Am I overthinking this, or is there a more efficient way to do this without media queries?
.wrap {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-gap: 1em;
grid-auto-rows: minmax(100px, auto);
}
.wrap>div {
padding: 1em;
border: solid orange 1px;
}
#media (max-width: 1000px) {
.wrap {
grid-template-columns: repeat(3, 1fr);
}
}
#media (max-width: 600px) {
.wrap {
grid-template-columns: 1fr;
}
}
There's nothing inherently wrong or inefficient with using media queries with grid. You can avoid them in certain scenarios (eg. if you have a list of the uniform cards) if you wish, by using auto-placement. The code would look somehow like that:
.listing {
grid-auto-flow: dense;
grid-template-columns: repeat(auto-fill,minmax(200px, 1fr));
}
.listing .wide {
grid-column-end: span 2;
}
This code comes from MDN article where you can learn more about this functionality and adapt it to your needs.

CSS responsive grid go from 4 columns to 2

This is a nicely centered grid, but I want the 4 columns to go down to 2 for mobile. What should I add?
body {
display: grid;
align-items: center;
justify-items: center;
background: grey;
max-width: 56em;
}
ul {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 3em;
margin: 0 auto;
max-width: 64em;
}
li {
display: grid;
align-items: center;
justify-content: center;
list-style: none;
}
You have to add changes of css when the screen is narrow. Using:
#media screen and (max-width: ???px){}
And in the block you add rules like in normal css which should change when width of screen is less than given value. I suppose it will be enough for you to deal with this. If you need more information (e.g. new values) write comments and I will try to answer them.
Examples

i cant apply grid-template-columns and grid Gap Css in Miscrsoft Edge (version - 20.10240.17146.0)

I am using Miscrosoft Edge version 20.10240.17146.0
and I am have used
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
grid-gap: 20px;
align-items: stretch;
margin: 15px 15px;
}
this CSS and edge doesnt Support this CSS.
Is there any alternate way so I can see my site better in Edge

Resources