I'm starting using CSS grid (pretty amazing!). I'm starting with the most basic layout: mobile layout. Also, I'm using media queries to change the layout as the web page grows.
For now, my layout consists of 3 areas. The exact order is the content area, the sidebar area and the commenting area. The commenting area is optional and may not display at all. There is a gap of 40px between areas.
This is the mobile layout css
.grid {
display: grid;
grid-template-columns: 1fr;
grid-auto-rows: auto;
grid-gap: 40px;
}
If the commenting area doesn't exist, the gap for the commenting area also does not exist, which it's what i want.
https://jsfiddle.net/p54od8ho/
When the page grows, i'm changing the layout, like this:
@media screen and (min-width: 768px) {
.content {
grid-area: content;
}
.sidebar {
grid-area: sidebar;
}
.comments {
grid-area: comment;
}
.grid {
grid-template-columns: 1fr 300px;
grid-template-rows: auto;
grid-template-areas:
"content sidebar"
"comment sidebar";
}
}
https://jsfiddle.net/g20gtd4z/
The gap of 40px exists because the comment area exists.
But, this is what happens when the comment area does not exist:
https://jsfiddle.net/6Lfg55my/1/
If you notice, even if the comment area doesn't exist, the 40px gap exists.
I believe one solution is to create a class, just to remove the comment area.
.grid.no_comment {
grid-template-areas:
"content sidebar"
". sidebar";
}
There has to be a better and more simple way (or maybe not).. Is there a way, using only the grid options, to make it so when the comment area does not exist, the gap is also gone ?