Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS grid, remove gap when area is optional

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 ?

like image 549
Marco Avatar asked Sep 07 '17 20:09

Marco


People also ask

How do I stop my grid from expanding?

By default, a grid item cannot be smaller than the size of its content. Grid items have an initial size of min-width: auto and min-height: auto . You can override this behavior by setting grid items to min-width: 0 , min-height: 0 or overflow with any value other than visible .

Is grid column gap obsolete?

'grid-gap' has been deprecated in favor of 'gap'. In a similar vein 'grid-row-gap' and 'grid-column-gap' have been deprecated in favor of 'row-gap' and 'column-gap'.

What is auto fill in CSS grid?

Auto-fill: The auto-fill property fills the rows with as many columns as it can fit. The newly added column may be empty but it will still occupy a space in the given row. It is an important property in the CSS grid that make a responsive layout without writing a media query for each grid.


1 Answers

When the comment area exists, you have two columns and two rows in the container:

.grid {
        grid-template-columns: 1fr 300px;
        grid-template-rows: auto;
        grid-gap: 40px;
        grid-template-areas:
            "content sidebar"
            "comment sidebar";
  }

So, naturally, grid-gap applies between the first and second row (and column).

When the comment area is removed, there are still two columns and two rows in the container. Removing that item doesn't change the value of grid-template-areas.

So, naturally, grid-gap continues to apply between the two rows. Except now, with the comment section gone, there's a bigger space in that grid area.

When you want the comment section removed, instead of removing that grid item, why not remove the entire second row? grip-gap only works between grid items.

.grid {
        grid-template-columns: 1fr 300px;
        grid-template-rows: auto;
        grid-gap: 40px;
        grid-template-areas:
            "content sidebar"
            /* "comment sidebar" */
            ;
  }
like image 198
Michael Benjamin Avatar answered Nov 24 '22 22:11

Michael Benjamin