CSS Grid Max Columns is a game-changer for designers and developers, allowing us to create complex layouts with ease.
By setting a maximum number of columns, we can ensure our grid remains flexible and adaptable to different screen sizes and devices. This is particularly useful for responsive design.
A key benefit of using CSS Grid Max Columns is that it allows us to define a maximum number of columns, while still allowing the grid to automatically adjust to different screen sizes. This is achieved through the grid-template-columns property.
For example, setting grid-template-columns to "repeat(3, 1fr)" creates a grid with a maximum of 3 columns, each taking up an equal amount of space. This is a great starting point for many layouts.
Responsive Grid System
I've used simple fractions for my grid sizes, which keeps the mental overhead really low and makes it easy to figure out how wide a thing is going to be relative to the other columns.
You can create a responsive grid with CSS Grid by wrapping your whole grid system in a media query, typically starting around 40em or 640px viewport size.
I prefer to use a 12-column layout that lets me easily divide things into thirds, fourths, sixths, and so on, which is achieved with the grid-template-columns property and the repeat() method.
The grid-template-rows property is used to define a row with a minimum size of 1fr and an auto-sized maximum.
To add spacing between columns, you can use the column-gap property, but I prefer to control the spacing between rows with utility classes instead.
You can turn any layout element into a CSS grid layout by setting the Display value to grid, making it a Grid Container with direct child elements as Grid Items.
Grid items are laid out in rows by default, covering the full width of the grid container, but you can unlock the true power of CSS grid by starting to define grid columns and rows.
Media queries are essential for responsiveness, allowing you to define specific CSS rules that will be triggered when certain screen widths are met, such as common device categories like mobile phones, tablets, and desktops.
Grid Layout Basics
To create a grid layout, you can set the Display value to grid on any layout element, turning it into a Grid Container. Every direct child element becomes a Grid Item.
A grid overlay becomes visible when editing a grid container, indicating the grid cells. Clicking the little four-square element action icon lets you show or hide this overlay.
Grid items are laid out in rows by default, covering the full width of the grid container.
What Is a 12 Column Grid?
A 12 column CSS grid is a layout system that divides a web page into 12 equal-width columns, providing a framework for creating flexible and responsive designs.
Each column gets a width of 1fr by default, which means it takes up 1 fraction of the container's total width. This results in columns that are 100 pixels wide if a grid is 1200 pixels wide.
This allows for flexibility in designing the layout, as the columns can be combined or resized according to the specific needs of the webpage.
You can have a single element spanning the entire width of the grid by using all 12 columns, or you can divide the space into multiple smaller sections by assigning different numbers of columns to each element.
Container
A grid container is the foundation of a grid layout, and it's created by setting the display value to grid. This element is your Grid Container.
To create a grid container, you can use the display property with the value grid or inline-grid. This establishes a grid formatting context and allows you to organize child elements into a grid structure.
Every direct child element of your Grid container is a Grid Item, with additional settings for Grid column & Grid row to place an item within the grid. This is what makes a grid layout so powerful.
A grid container can be any layout element, such as a section, container, block, or div. This means you can apply a grid layout to most elements on your webpage.
Lines
Grid layout basics involve understanding the grid lines that help position elements. Grid lines are the dividing horizontal and vertical lines created when we define the grid.
Grid lines are numbered starting from 1 at the start edge of the grid. This numbering follows the writing style and direction of the document or component.
Column lines are the lines between columns, while row lines are the lines between rows.
Implicit
The implicit grid is a game-changer for responsive design. It automatically generates additional tracks for grid items that fall outside of your explicitly defined grid.
This is called the implicit grid, and it's generated by the grid container itself. You can define the column & row sizes of this implicit grid via the Grid auto columns (grid-auto-columns) and Grid auto rows (grid-auto-rows) settings.
The implicit grid is a great way to ensure that your grid layout is flexible and adaptable to different screen sizes and orientations. It's especially useful when you're not sure how many grid items you'll have or how they'll be arranged.
Grid Tracks and Gaps
Grid tracks define the columns and rows in a grid container, and each row track represents the space between two row lines, while each column track represents the space between two column lines.
Grid tracks can be created using the grid-template-columns and grid-template-rows properties, which can be set to a length value, a percentage value, or a fractional unit (fr) value. The fr value is a special value representing a fraction of the available space in the grid.
The grid-template-columns property sets the width of each column, and the grid-template-rows property specifies the height of the rows. For example, grid-template-columns: 200px 1fr 2fr creates three columns with a fixed width of 200px, and the remaining space is divided equally between the second and third columns.
We can also use the repeat function to define a repeated pattern for grid tracks in a shorter syntax. For instance, repeat(3, 1fr) is equivalent to 1fr 1fr 1fr, creating three equal columns.
Gaps between grid items can be controlled using the grid-gap property, which allows us to define the gap size between rows and columns in a grid. The grid-gap property serves as a shorthand for the grid-row-gap and grid-column-gap properties.
Here's a summary of the different types of values we can use for grid tracks:
Tracks
Tracks are the building blocks of a grid container, defining the columns and rows. They are made by giving them a size when we create our grid.
Each row track represents the space between two row lines, and similarly, each column track represents the space between two column lines. We can create grid tracks using the grid-template-columns and grid-template-rows properties.
The grid-template-columns property sets the width of each column using a length value, a percentage value, or a fr value. The fr value is a special value representing a fraction of the available space in the grid.
The grid-template-rows property specifies the height of the rows, with each value creating a row track. If we only explicitly define the height for the first two rows, the height of any row after that is determined by the height of its content by default.
We can create an implicit grid to change this behavior. The grid-template-columns property can be set to repeat a pattern using the repeat function, which allows us to define a repeated pattern for grid tracks in a shorter syntax.
Here's a breakdown of the grid-template-columns property:
We can use the minmax function to set a minimum and maximum track size. For example, grid-template-columns: repeat(3, minmax(200px, 1fr)) creates an explicit 3-column grid where each grid item has a minimum width of 200px and a maximum width of 1fr.
Gaps
Gaps are a crucial aspect of grid layout, and they can be controlled using the grid-gap property.
The grid-gap property is a shorthand for the grid-row-gap and grid-column-gap properties, making it easier to define the gap size between rows and columns in a grid.
This property was actually renamed from grid-gap to gap in CSS3, so be sure to use the correct terminology.
Grid-row-gap and grid-column-gap properties are responsible for controlling the size of the gap between rows and columns, respectively.
These properties have also been renamed to row-gap and column-gap, respectively.
Grid Item Placement
To place a grid item onto the grid, we can use the grid lines marked by the start or end of a column or row track. The count starts at 1.
The Grid column and Grid row settings are available for all grid items and can be set to specify the starting and end line numbers. For example, setting the Grid column to "2 / 4" tells the grid to start at column line 2 and end at column line 4.
By using the Grid column and Grid row settings, we can achieve a layout like the one in Example 1, where Grid Item 2 takes up two columns and two rows.
Placing Items
You can place grid items on a grid by using grid lines, which mark the start or end of a column or row track. The count starts at 1.
Grid lines are crucial for placing grid items, and you can use them to specify the position of an item on the grid.
To place a grid item, you need to specify the starting and ending line numbers for both the column and row. This is done using the Grid column and Grid row settings.
The syntax for these settings is: first value specifies the starting line number, followed by a forward slash (/), and then the second value, which specifies the end line number.
For example, in the example above, Grid Item 2 is placed on the grid by setting the Grid column to "2 / 4" and the Grid row to "1 / 3".
This means that Grid Item 2 starts at column line 2 and ends at column line 4, and starts at row line 1 and ends at row line 3.
Alternatively, you can use the "span" keyword to tell the grid layout how many columns or rows the item should span.
For instance, setting the Grid column and Grid row setting to "span 2" would achieve the same layout as setting them to "2 / 4" and "1 / 3" respectively.
How to Create
Creating a responsive grid with CSS Grid is a great way to start. You can turn any layout element into a CSS grid layout by setting the Display value to grid.
To define a grid system, wrap your grid elements in a media query, starting at a viewport size of about 40em (or 640px with the default font size). This will ensure your grid layout adapts to different screen sizes.
The grid-template-columns property is used to define the number of columns in your grid. For a 12-column layout, use the repeat() method to create 12 columns of 1fr (one fractional unit) each. This allows for easy division of elements into thirds, fourths, sixths, and so on.
Grid-template-rows is used to define the rows in your grid, with a minimum size of 1fr and an auto-sized maximum. This will give you a flexible and responsive grid layout.
To add spacing between columns, use the column-gap property. However, for spacing between rows, consider using utility classes instead of relying on the grid-template-rows property.
Grid Size and Limits
You can use the grid-template-columns property to set a minimum and maximum track size with the minmax function, which accepts two arguments: the minimum value and the maximum value of your grid track.
The minmax function can create an explicit grid with a minimum width of 200px and a maximum width of 1fr, as seen in the example grid-template-columns: repeat(3, minmax(200px, 1fr)). This can be useful for creating a grid with some constraints.
However, this kind of explicit grid can be non-responsive and may overflow when the viewport is less than 600px wide, because it's based on a fixed number of columns.
To create a responsive grid, you can use the repeat function with the minmax function, but you'll need to set some restrictions on the minimum and maximum size to prevent the columns from being too small or too large.
Grid Keywords and Units
The CSS grid relative sizing unit fr is used to split rows and/or columns into proportional distances.
Each fr unit is a fraction of the grid’s overall length and width, making it a flexible way to layout content.
If a fixed unit is used along with fr, the fr units will only be proportional to the distance left over, which can be useful in certain design scenarios.
Auto-Fill & Auto-Fit Keywords
We can use the auto-fill or auto-fit keywords to address responsive issues, allowing us to create responsive grid layouts without media queries.
The auto-fill keyword expands grid items to fill the available space.
Grid items using auto-fill will keep the available space, meaning they won't expand to fill it.
We can use auto-fill like this: grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)).
The auto-fit keyword, on the other hand, expands grid items to fill the available space.
Grid items using auto-fit will fill the available space, making them a good choice when you want a more dynamic layout.
Relative Unit
The CSS grid relative sizing unit fr is used to split rows and/or columns into proportional distances.
Each fr unit is a fraction of the grid's overall length and width.
If a fixed unit is used along with fr, then the fr units will only be proportional to the distance left over.
Frequently Asked Questions
How to use 12 column grid in CSS?
To create a 12-column grid in CSS, use the `repeat()` function with `repeat(12, 1fr)` in your grid template columns. This sets up a 12-column layout where each column is an equal fraction of the available space.
How to set the number of columns in grid CSS?
To set the number of columns in grid CSS, use the grid-template-columns property and specify the desired number of width values. Each width value determines a column, and can be in pixels (px) or percentages (%).
Sources
- https://gomakethings.com/how-to-create-a-responsive-grid-system-with-css-grid/
- https://academy.bricksbuilder.io/article/css-grid-layout/
- https://www.lambdatest.com/blog/12-column-css-grid/
- https://adamlynch.com/flexible-data-tables-with-css-grid/
- https://www.codecademy.com/learn/learn-intermediate-css/modules/learn-css-grid/cheatsheet
Featured Images: pexels.com