CSS Grid Auto Columns is a powerful feature that allows you to create flexible and responsive layouts. It automatically calculates the number of columns based on the content, making it perfect for grid systems.
With Auto Columns, you can create a grid with a dynamic number of columns, depending on the content. This feature is especially useful for layouts that need to adapt to different screen sizes.
By using Auto Columns, you can save time and effort in designing and maintaining your grid layouts. It's a game-changer for developers and designers alike.
Grid Auto Columns
Grid Auto Columns are a powerful tool in CSS Grid, allowing you to control the size of implicitly-created grid columns. By default, implicit columns are sized automatically based on the space that's left.
You can use the .grid-auto-columns-{keyword} utilities to control the size of implicit columns. For example, .grid-auto-columns-auto sets the size of implicit columns to auto, while .grid-auto-columns-min sets the size to min-content.
Here are some specific classes you can use to control the size of implicit columns:
By using these classes, you can control the size of implicit columns and create a more flexible and responsive grid layout.
Auto Placement
Auto placement is a game-changer for designers, allowing us to automatically place items on our grid without having to explicitly place each one.
By default, each grid item has a span of 1 on both the row and column axis, so they'll be placed into the next available grid cell.
This means we can use auto placement to our advantage, especially when working with dynamic content like a news feed, where we don't know how many items will be there.
Implicit columns are created when there are more grid items than cells, and this is where the auto-placement algorithm comes in handy.
The algorithm creates additional rows or columns to hold the extra items, which are called implicit tracks.
We can control how the extra items are laid out by setting the auto-placement algorithm to either rows or columns.
For example, if we want the extra items to be laid out in columns, we can set the auto-placement algorithm to column.
The implicit columns are sized automatically based on the space that's left, which is why grid-auto-columns is so useful.
We can give all of those columns a size, and CSS Grid will stop filling columns when it reaches the last element, even if the last column in the last row of the grid falls short of the grid's full width.
Auto
The auto keyword is a great way to size your grid columns, but it's not as straightforward as it seems. It behaves similarly to minmax(min-content, max-content) in most cases.
Auto track sizes can be stretched by the align-content and justify-content properties, which means they'll take up any remaining space in the grid container by default. This can be both a blessing and a curse, as it means your auto-sized columns will adapt to the content inside them.
If all three columns are set to auto, they'll share equal space. But if you drop a fractional unit in the middle, the auto columns will only be as wide as the content inside them, and the 1fr column will take up the remaining space.
Here are some key things to keep in mind when using the auto keyword:
- Auto track sizes can't win a fight against fr units when allocating the remaining space.
- Auto track sizes adjust to the maximum size of their content.
- Auto track sizes can be stretched by the align-content and justify-content properties.
Grid Flow
Grid flow is like a river, with explicitly placed items as boats anchored in the river. Auto-placed items flow around these boats from left to right.
Explicitly placed items on one axis are like boats that are loosely anchored, participating in the grid flow on the remaining axis.
Items placed using a span value alone will flow like others, but are restricted by their own explicit size. An item with a span of 2 will flow onto the next row if there are less than 2 grid columns available.
We can think of these items as being semi-auto placed, and they'll be placed after successive items if we only place them on the column axis.
If we have enough items to fill the grid exactly, but only place them on the column axis, we'll create an implicit track. This doesn't happen if we only place them explicitly on the row axis.
The grid-auto-flow property changes the direction of flow, and therefore how items will be auto-placed. We can set it to row, column, row dense, or column dense to change the order of items in the grid.
Grid-auto-flow defaults to row, so we need to specify it if we want to change the flow of our grid.
Grid Auto Flow Options
Grid auto flow options give you control over how items are placed in your grid. By default, items will be auto-placed in the next available grid cell.
You can change the direction of flow using the grid-auto-flow property, with possible values being row (default), column, row dense, and column dense. This is especially useful for dynamic content like news feeds where you don't know how many items there will be.
The grid-auto-flow property can be used to change the order of items in the grid, giving you more flexibility in designing your layout. This is particularly useful for responsive design where you need to accommodate different screen sizes.
You can also use the .grid-auto-flow-{keyword} utilities to control how the grid auto placement flows. These utilities include classes like .grid-auto-flow-column and .grid-auto-flow-row-dense.
Grid Column Sizing
Grid Column Sizing is a powerful feature of CSS Grid that allows you to control the size of implicit columns. You can use the .grid-auto-columns-{keyword} utilities to do this.
By default, implicit columns are sized automatically based on the space left over. However, you can specify a fixed size for implicit columns using the grid-auto-columns property. For example, you can set the size of implicit columns to 250px.
You can also specify multiple column sizes for grid tracks using the grid-auto-columns property. In this case, the multiple track size works as a pattern and gets repeated if necessary. For instance, if you set the first implicit column size to 50px, the second to 100px, and the third to 200px, the pattern will repeat for any additional implicit columns.
Here are some common values you can use for grid-auto-columns:
You can also use any valid and non-negative CSS length, such as px, em, or rem, to specify the track size of the column.
Fit-Content(<Length-Percentage>)
The fit-content() function is a game-changer for grid column sizing. It uses the space available and accepts a percentage or length value as the maximum size allowed for a grid track while ensuring the track never goes beyond the max-content and never shrinks beyond a minimum.
The minimum is often equal to the min-content, but not always. This function is particularly useful when you want to set a maximum size for a grid track without specifying an exact value.
In practice, this means that if you have a grid track with a lot of content, it will only stretch out to fit its content, rather than expanding beyond its max-content size. For example, if you set a grid track to fit-content(700px), it will only expand to a width of 700px, even if the content inside it is wider.
One thing to keep in mind is that if you set a grid track to a percentage value, the size of the gaps between the tracks will be added to the size of the tracks, which can cause an overflow. To avoid this, consider using the fr unit or auto keyword to size the tracks instead.
Multiple Track Sizes
Multiple track sizes can be specified using the grid-auto-columns property. This property allows you to set the size of implicit columns that are created when an item is positioned outside of the explicit grid.
You can specify multiple column sizes for grid tracks using the grid-auto-columns property. This property works as a pattern and gets repeated if necessary. For example, if you have a grid with two explicit columns (150px 150px) and the last item wants to be in the eighth column (grid-column: 8), six implicit columns are automatically generated in the grid.
The grid-auto-columns property sets the size of the first implicit column to 50px, the second to 100px, and the third column to 200px. If you have more than three implicit columns, the grid repeats this pattern to size them. For instance, if you have one more implicit column, its size would be 150px.
You can also specify a non-negative dimension with the unit fr to specify the track's flex factor. The fr unit represents a fraction of the free space in the grid container. Each flex-sized track takes a share of the remaining space in proportion to its flex factor.
Here's an example of how you can specify multiple values for the grid-auto-columns property:
Grid Items Exceed Cells
CSS Grid's auto-placement algorithm creates additional rows or columns to hold extra items when there are more grid items than cells. This is known as implicit tracks.
If you have more grid items than defined columns, the extra items will be placed in implicit columns. By default, the auto-placement algorithm creates row tracks to hold extra items.
You can ask the grid to lay out the extra items in columns instead of rows by setting the auto-placement algorithm to column. This is useful when you want to create a grid that can hold a variable number of items.
Implicit columns are sized automatically based on the space that's left. This is why grid-auto-columns is so useful - you can give all of those columns a size and CSS Grid will stop filling columns when it reaches the last element.
If you size those implicit grid columns to 250px, no matter how many implicit columns get created, they will all have their size set to 250px.
Sources
- grid placement algorithm (w3.org)
- Grid, content re-ordering and accessibility (rachelandrew.co.uk)
- MDN documentation (mozilla.org)
- CSS Grid LayoutThe definition of 'grid-auto-columns' in that specification. (csswg.org)
- Introducing Grid auto-placement and order (gridbyexample.com)
- CSS Grid Layout specification (w3.org)
- auto-placement algorithm (csswg.org)
- computed track list (csswg.org)
- CSS Grid Layout Module 1 (w3.org)
- CSS Grid Layout Module Level 1 (w3.org)
- defined (w3.org)
Featured Images: pexels.com