Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly reset grid-template-columns?

Tags:

html

css

css-grid

I have a two column layout in CSS grid and would like to toggle to a single column layout at 1024px.

.page {
  display: grid;
  grid-template-columns: 50% 50%;
  grid-template-rows: auto;
  grid-column-gap: 5pt;
  grid-row-gap: 5pt;
}

@media (max-width: 1024px){
  .page{
    display: block;
  }
}

Is changing the display type a complete solution for disabling grid-template-rows etc., or should they explicitly reset?

Are there any "gotchas" when setting display types using grid.

like image 518
Matthew Avatar asked Jun 17 '19 16:06

Matthew


People also ask

How do I reset grid columns?

To reset the grid: Right-click any column header, and click Reset Grid Style.

What is the correct format of grid columns?

The grid-column-start longhand is set to the value before the slash, and the grid-column-end longhand is set to the value after the slash. Each <grid-line> value can be specified as: either the auto keyword. or a <custom-ident> value.

How do you repeat in grid-template-columns?

repeat count: the first argument specifies the number of times that the track list should be repeated. It is specified with an integer value of 1 or more, or with the keyword values auto-fill or auto-fit . These keyword values repeat the set of tracks as many times as is needed to fill the grid container.

How do I center everything on the grid?

One of the easiest ways of centering the content of grid items is using Flexbox. Set the display to "grid" for the grid container, and "flex" for grid items. Then, add the align-items and justify-content properties, both with the "center" value, to grid items.


1 Answers

The initial value of grid-template-columns and grid-template-rows is none.

So to reset the properties (meaning there are no explicit tracks created), you would switch to grid-template-columns: none in your media query.

You can also switch to grid-template-columns: 1fr, which creates a grid with one explicit column.

article {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-auto-rows: 100px;
  grid-column-gap: 5pt;
  grid-row-gap: 5pt;
}

section {
  background-color: green;
}

@media (max-width: 600px) {
  article {
    grid-template-columns: 1fr;
    /* OR, change value to 'none' */
  }
  section {
    background-color: orangered;
  }
}
<article>
  <section></section>
  <section></section>
</article>

jsFiddle demo

Spec reference:

  • § 7.2. Explicit Track Sizing: the grid-template-rows and grid-template-columns properties
like image 157
Michael Benjamin Avatar answered Sep 21 '22 17:09

Michael Benjamin