Prior to the existence of the CSS grid, developers leveraged several options such as floats, element positioning, and so on. However, this wasn't sustainable enough to create flexible layouts that respond to different screen sizes. Therefore, they resorted to designing fixed layouts. With CSS grid, developers can conveniently align page content into rows and columns in an orderly and manageable fashion; thereby, enabling them to easily create flexible layouts that respond to changes in the view port size.
In designing complex websites and applications with numerous content, the CSS grid provides a way to repeatedly and automatically insert page content to produce different designs. This limits the lines of code we write and saves us time and stress.
In the CSS grid, there are two values that enable developers to automatically insert page content into layouts: auto-fit and auto-fill. Learning the difference between these values will enable us to reasonably choose the automatic keyword value to design layouts based on the page content.
repeat() functionThe repeat() function is a CSS function that allows us to repeat a value or set of values for multiple columns and rows in a grid. The repeat() function helps us to reduce the lines of code. Instead of writing out a certain value or set of values multiple times to specify multiple columns and rows, we can explicitly state the number of times we want.
The repeat() function can be used with two grid properties—grid-template-columns and grid-template-rows.
repeat(<repeat-count>, <track-list>)
The repeat() function has two parameters: repeat-count and track-list.
auto-fit and auto-fill.A CSS function called the minmax() function, allows us to specify the size range of a grid item. It can be used within the repeat() function.
minmax()functionThe minmax() function states the size range of a grid item and it allows a grid item to change size within the stated size range. This makes the grid item responsive. The minmax() function takes two parameters—min and max.
minmax(min size, max size)
Min size: The min parameter specifies the minimal size of a grid item. It states that the grid item shouldn't change sizes less than the stated minimal size (when the grid container reduces in size).Max size: The max parameter specifies the maximal size of a grid item. By default, a grid item assumes its maximal size.auto-fill keywordThe auto-fill keyword automatically inserts multiple columns and rows into a grid container, until it completely fills the entire size of the grid container. This means that even after exhausting the stated grid items in the code, the auto-fill keyword continues to fill the grid container with empty columns to occupy its entire size. The number of columns or rows inserted into the grid container depends on the size of the grid container and the stated size of the columns and row.
auto-fill keyword should be used when we want to automatically fill the grid container with grid items. It is advisable to use auto-fill when we are sure that the available grid items will completely fill the grid container at the widest viewport to avoid any additional empty column(s).Note: The
auto-fillandauto-fitkeywords should be used alongside theminmax()function, to properly see its effect and to ensure the responsiveness of web pages.
repeat() function. The repeat() function was given an auto-fill keyword and a minmax() function having 100px as its minimal size and 1fr as its maximal size. auto-fit keywordThe auto-fit keyword automatically inserts columns and rows into a grid container but when the grid items in our code cannot occupy the entire row width of the container, the auto-fit keyword stretches (instead of adding empty tracks) each grid item to entirely fit its available space.
repeat() function. The repeat() function was given an auto-fit keyword and a minmax() function having 150px as its minimal size and 1fr as its maximal size. auto-fill and auto-fit auto-fill continually inserts empty columns of the same width and height as the stated grid items until the entire row width is occupied. auto-fit (on the other hand) does not insert empty columns into the row of the grid, instead, it stretches each grid item to completely fit into its entire width, without adding extra tracks. auto-fill creates a fixed grid layout while auto-fit creates a flexible grid layout. It stretches its grid items to occupy the row width of its parent container. The auto-fit and auto-fill keyword values are just two of the handful of ways to automatically inser page content into layouts. Although, the two keyword values behave the same to an extent, there is a slight difference between them. Understanding the difference between them will enable us to preferentially choose between the two to create layouts based on the page content.