Skip to content

Instantly share code, notes, and snippets.

@karlazz
Last active April 22, 2021 23:55
Show Gist options
  • Save karlazz/0f6ff048eb350857fed6b5e01673fd67 to your computer and use it in GitHub Desktop.
Save karlazz/0f6ff048eb350857fed6b5e01673fd67 to your computer and use it in GitHub Desktop.
Automated reponsive grid from Heydon Pickering and further grid notes
.container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr));
grid-gap: 1rem;
}
@karlazz
Copy link
Author

karlazz commented Apr 22, 2021

And an improvement to avoid overflow in narrow narrow columns:

grid-template-columns: repeat(auto-fill, minmax(min(10rem, 100%), 1fr));

@karlazz
Copy link
Author

karlazz commented Apr 22, 2021

/* and here is a solution for leftover grid items from https://css-irl.info/controlling-leftover-grid-items/ */
.grid {
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-gap: 20px;
}
li {
grid-column: span 2;
}

/* Dealing with 2 orphan items */

li:last-child:nth-child(3n - 1) {
grid-column-end: -2;
}

li:nth-last-child(2):nth-child(3n + 1) {
grid-column-end: 4;
}

/* Dealing with single orphan */

li:last-child:nth-child(3n - 2) {
grid-column-end: 5;
}

@karlazz
Copy link
Author

karlazz commented Apr 22, 2021

And here is a CSS trick with @supports for incompatible browsers for Grid feature:
.main {
width: 100%;
overflow: hidden;
}
.main > div {
width: 30%;
float: left;
}

@supports (display: grid) {
.main {
display: grid;
}
}

@karlazz
Copy link
Author

karlazz commented Apr 22, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment