CSS Grid template columns are a crucial part of responsive design, allowing you to create complex layouts that adapt to different screen sizes.
By using the `grid-template-columns` property, you can define the number of columns and their widths in a grid container. For example, `grid-template-columns: 1fr 2fr` creates two columns, where the first column takes up 1 fractional unit and the second column takes up 2 fractional units.
This property can be used to create a wide range of layouts, from simple two-column designs to complex multi-column grids. In the example from the article, `grid-template-columns: 1fr 2fr` creates a layout where the first column takes up 1/3 of the available space and the second column takes up 2/3 of the available space.
Explore further: Add Css Property to a Predefined Class Javascript
CSS Grid Basics
Most browsers shipped native support for CSS Grid as of March 2017, including Chrome, Firefox, Safari, and Opera.
The time to build with grid is now, thanks to widespread browser support.
To get started, you need to define a container element as a grid with display: grid.
CSS Basics
Most browsers shipped native support for CSS Grid as of March 2017.
You can define a container element as a grid with display: grid.
The source order of grid items doesn't matter, making it easy to rearrange your grid with media queries.
Grid is one of the most powerful CSS modules ever introduced.
To get started, you need to set the column and row sizes with grid-template-columns and grid-template-rows.
You can place child elements into the grid with grid-column and grid-row.
This makes it super easy to rearrange your grid with media queries.
Discover more: Html Table 2 Columns
Grid Container
The grid container is the element on which display: grid is applied. It's the direct parent of all the grid items. In this example, container is the grid container.
The grid container is where the magic happens, and it's what allows you to create a grid layout for your elements. To define a grid container, you simply need to apply the display: grid property to an element.
Here are the key properties to keep in mind when working with the grid container:
- The grid container is the direct parent of all the grid items.
- The grid container is defined by applying display: grid to an element.
Grid Template Columns
Grid Template Columns are defined with a space-separated list of values, where the values represent the track size and the space between them represents the grid line. You can use a length, a percentage, or a fraction of the free space in the grid using the fr unit.
Grid lines are automatically assigned positive numbers, but you can choose to explicitly name the lines. Note the bracket syntax for the line names, which can have more than one name. For example, the second line can have two names: row1-end and row2-start.
Here are some common track size values:
- Length: a fixed size, such as 50px
- Percentage: a percentage of the grid container, such as 25%
- fr unit: a fraction of the free space in the grid, such as 1fr
The fr unit allows you to set the size of a track as a fraction of the free space of the grid container. For example, this will set each item to one third the width of the grid container: 1fr.
Parent Properties
Parent properties play a crucial role in defining the structure of grid template columns.
The parent property of the grid template area is the grid container, which is the element that contains the grid template columns.
The grid container can be defined using the grid container properties, such as grid-template-columns and grid-template-rows.
The grid-template-columns property is used to define the grid template columns, and it can be specified using a template string.
Grid template columns can be defined using a fixed width, such as 1fr, 2fr, or 3fr, or they can be defined using a percentage value, such as 50% or 25%.
The repeat() function can be used to repeat a grid template column pattern, such as repeating a column that is 1fr wide three times.
The grid-template-columns property can also be used to define a grid template column that spans multiple grid tracks, using the span keyword.
Additional reading: 3 Columns Html
Grid-Template-Rows
Grid-template-rows is a crucial part of defining the structure of your grid. It defines the rows of the grid with a space-separated list of values.
The values represent the track size, and the space between them represents the grid line. You can use a length, a percentage, or a fraction of the free space in the grid using the fr unit.
Grid lines are automatically assigned positive numbers, but you can choose to explicitly name the lines. Note the bracket syntax for the line names: [line-name]. A line can have more than one name, which can be useful for referencing specific lines.
Here's an example of how to name lines: If your definition contains repeating parts, you can use the repeat() notation to streamline things. This is equivalent to defining multiple lines with the same name.
For instance, if you want to set each item to one third the width of the grid container, you can use the fr unit like this: 1fr 1fr 1fr. The free space is calculated after any non-flexible items, so the total amount of free space available to the fr units doesn’t include the 50px.
Suggestion: Free One Page Website Template
Column-Gap Row-Gap
Column-Gap and Row-Gap are used to set the size of the grid lines, creating gutters between columns and rows. The gutters are only created between the columns and rows, not on the outer edges.
You can think of it like setting the width of the gutters between the columns and rows. This is done using a length value, such as a number or a percentage.
The grid- prefix will be removed, and grid-column-gap and grid-row-gap will be renamed to column-gap and row-gap. This new naming convention is already supported in some browsers, including Chrome 68+, Safari 11.2 Release 50+, and Opera 54+.
Here are the properties that control the gap:
- column-gap
- row-gap
- grid-column-gap (soon to be renamed)
- grid-row-gap (soon to be renamed)
To illustrate how implicit grid tracks get created, think about this: `grid-template-columns: 1fr 1fr;`. This creates a 2 x 2 grid.
Subgrid
Subgrid is an experimental feature supported in Firefox that allows a grid within a grid to share track lines with the parent grid, making it easier to create consistent track lines without having to re-declare them.
This feature is achieved by declaring the subgrid keyword value, which allows the inner grid to inherit the track lines of the outer grid. For example, consider a nested grid with a child grid that shares the track lines with the parent grid.
The subgrid items will adjust to the track sizing of the parent grid, and the gaps in a subgrid are inherited from the parent, but can be overridden with a different gap value on the subgrid element.
A subgrid has its own line numbers, which start from 1, regardless of the line number of the parent grid. This means that even if a subgrid starts on the third track line of the parent grid, the first track line of the subgrid will still be 1.
You can declare custom grid line names in a subgrid, and use the repeat() function to specify track line names, which merges any track line-names that are next to each other.
Here's a summary of the key points about subgrids:
- Subgrids are experimental and supported in Firefox.
- Subgrids share track lines with the parent grid.
- Subgrid items adjust to the track sizing of the parent grid.
- Gaps in a subgrid are inherited from the parent, but can be overridden.
- Subgrids have their own line numbers, starting from 1.
- You can declare custom grid line names in a subgrid.
- You can use the repeat() function to specify track line names in a subgrid.
Note that in a subgrid, you cannot have any implicit tracks in that dimension, so if you add more grid items to the mix, they will be placed in the existing explicit tracks of the subgrid.
Equal
Equal columns can be achieved using the fr unit to size a grid's columns, but it's not as straightforward as it seems. The available space is distributed after the content decides how much space each column needs.
If you want three equally-sized columns, you might think to do this: 1fr 1fr 1fr. But since the minimum size of a grid layout is auto, it depends on the length of the content to get truly equal columns.
You can take out the minimum size by supplying a zero value in minmax() functions for each column like this: 1fr 1fr 1fr, but that's not the most efficient way to do it.
Recommended read: Html 2 Columns
Sources
- https://css-tricks.com/snippets/css/complete-guide-grid/
- https://css-tricks.com/almanac/properties/g/grid-template-columns/
- https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns
- http://udn.realityripple.com/docs/Web/CSS/grid-template-columns
- https://www.quackit.com/css/css3/properties/css_grid-template-columns.cfm
Featured Images: pexels.com