One of the most powerful features in the CSS Grid specification is the ability to create responsive layouts without using media queries. This is done by using the repeat function (covered earlier) along with auto-placement keywords auto-fit or auto-fill. These keywords allow you to place as many grid items of a particular size on a row that will fit within the width of the viewport.
Using the example below, make your web browser narrower and wider to see how using both auto-fit and auto-fill make the page behave.
NOTE: Up to a certain width, both auto-fit and auto-fill will have the same result. The difference between auto-fit and auto-fill can only be seen when there are fewer grid items than could fit on a row. In this example, each grid item is a minimum of 200 pixels and a maximum of 1fr.
You'll notice several things.
Notice the column line numbers in the illustration below. At this particular viewport width (1,215 pixels), a total of six 200 pixel wide grid items could fit on the row. Because there are only four 200 pixel wide grid items on the row, CSS Grid creates two more columns. However, the way auto-fit and auto-fill handle those extra columns differs.
With auto-fit, columns 5 and 6 are actually created, but they are 0 pixels wide and are stacked one on top of the other. You can see this by noting the column line numbers 5, 6, and 7 at the top right. So, even though the cells are actually created, they don't show up.
With auto-fill, columns 5 and 6 are also created, but now they are are created with a 200 pixel minimum width and show up as blank cells at the end of the row.
There is actually very little code needed to make all this happen and it's all done with the grid-template-columns property in the grid container.
This grid-template-columns property uses the auto-fit keyword and sets the minimum width for each grid item to 200 pixels and a maximum width of 1fr (or, one fractional unit).
CSSAdd the CSS seen here to create two grids. The first one is a grid container that uses the auto-fit keyword. The second one is a grid container that uses the auto-fill keyword. The "GENERAL STYLES" section will be used simply to make pretty the individual grid items.
HTMLThe HTML for the first grid is contained inside a <div> grid container with a class called container1. We're using this area to show how auto-fit works.
The HTML for the second grid is contained inside a <div> grid container with a class called container2. We're using this area to show how auto-fill works.
The link below shows you a page where two areas change layout without media queries. Pay attention to the text in the header and the "cards" showing the different types of grapes. This illustration uses the auto-fit keyword for both areas. It also uses "auto" instead of "1fr" in the grid-template-columns property. This way, the layout will center and extra space will be equal on both sides of the layout. Also, the cards don't expand because they are set to a width of 300 pixels.
Check out the HTML source of the page to learn how it was done. The main element is the grid container for the top of the page. The section element is the grid container for the "cards."