CSS Grid Garden is a fantastic resource for learning CSS Grid, and I'm excited to share my complete guide with you.
CSS Grid Garden is a interactive coding environment that lets you experiment with CSS Grid in a fun and interactive way. It's perfect for beginners and experienced developers alike.
With CSS Grid Garden, you can create a garden of grid-based elements, and see how different properties and values affect the layout. For example, did you know that the grid-template-columns property can be used to define the number of columns in a grid?
You can also use the grid-template-rows property to define the number of rows in a grid. And, as I learned from the article, the grid-template-columns property can be used to create a grid with equal-sized columns, like a garden with rows of identical flowers.
One of the most powerful features of CSS Grid Garden is its ability to visualize the grid, making it easy to see how different properties and values affect the layout. This is especially helpful when trying to troubleshoot issues with your grid.
Grid Basics
Most browsers shipped native support for CSS Grid by March 2017, making it a game-changer for web design.
Chrome, Firefox, Safari, and Opera were among the browsers that supported it unprefixed, while Internet Explorer 10 and 11 supported it, but with outdated syntax.
The source order of grid items doesn't matter, which makes it super easy to rearrange your grid with media queries.
You can define a container element as a grid with display: grid, set the column and row sizes with grid-template-columns and grid-template-rows, and then place its child elements into the grid with grid-column and grid-row.
Basics
Most browsers now support CSS Grid, including Chrome, Firefox, Safari, and Opera, with native and unprefixed support as of March 2017.
To get started with CSS Grid, you need to define a container element as a grid with display: grid.
Set the column and row sizes with grid-template-columns and grid-template-rows.
Place its child elements into the grid with grid-column and grid-row.
The source order of the grid items doesn't matter, making it super easy to rearrange your grid with media queries.
Imagine defining the layout of your entire page and then completely rearranging it to accommodate a different screen width all with only a couple lines of CSS.
Grid is one of the most powerful CSS modules ever introduced.
Item
As you start building your grid, you need to understand what constitutes a grid item. A grid item is simply the direct descendant of the grid container, which means it's the child elements that are nested inside the grid container.
The justify-items property is used to align grid items along the inline (row) axis. This means it affects how items are positioned horizontally within their cells.
Cell
A cell is the basic unit of a grid, making up the space between two adjacent row and two adjacent column grid lines.
The space between these lines is what defines a cell, setting it apart from its neighboring cells.
In a grid, cells are the building blocks that help create the overall structure and layout.
A grid cell is a single unit, like the one between row grid lines 1 and 2, and column grid lines 2 and 3.
3
At this level, we're working with grid-column-end property to span water. We count vertical lines to determine where to end the water.
We start counting from the first vertical line, and the water begins after the fifth line. To end the water, we need to span it to the second line.
To write this correctly, we simply span the water again.
Grid Lines
Grid lines are the dividing lines that make up the structure of the grid, and they can be either vertical or horizontal.
A column grid line is an example of a grid line, and it resides on one side of a column. The yellow line in the example is an illustration of a column grid line.
Grid lines are used to position grid items, and they can be controlled using the grid-column and grid-row properties.
Grid Properties
Grid properties allow you to define a grid item's location within the grid by referencing specific grid lines. This is done using properties like grid-column-start, grid-column-end, grid-row-start, and grid-row-end.
To specify a grid line, you can use a number, a name, or the keyword "auto". For example, grid-column-start: 3 refers to the third numbered grid line, while grid-column-start: header refers to a grid line named "header". If you use the keyword "auto", the item will be placed automatically, spanning one track by default.
You can also use the "span" keyword to specify how many grid tracks an item should span. For example, grid-column-end: span 2 means the item will span two tracks, while grid-column-end: span header means it will span until it hits the next line named "header".
Properties for Children
Grid items can be placed within a grid using various properties. These properties determine a grid item's location within the grid by referring to specific grid lines.
grid-column-start and grid-column-end determine the item's vertical position, while grid-row-start and grid-row-end determine its horizontal position.
Grid items can span across multiple grid tracks using the span keyword. This can be specified with a number, such as span 2, or with a named grid line, like span header.
If no grid-column-end or grid-row-end is declared, the item will span 1 track by default.
Items can overlap each other, and their stacking order can be controlled using the z-index property.
Here are some examples of how to specify grid item positions:
- grid-column-start: 2; grid-column-end: 5;
- grid-row-start: header; grid-row-end: footer;
- grid-column-start: 1; grid-column-end: span 3;
Note that grid items can be placed using negative values for the grid-column-end property to count vertical lines from right to left.
Parent Properties
The Parent Properties section of Grid Properties is where things get really interesting. The display property defines the element as a grid container and establishes a new grid formatting context for its contents. This can be set to either grid or inline-grid.
You can think of the display property as the starting point for your grid layout. It's what tells the browser to treat the element as a grid container. The grid property generates a block-level grid, while the inline-grid property generates an inline-level grid.
Grid properties can be passed down through nested elements, but this feature has been moved to level 2 of the CSS Grid specification. This means it's not currently supported in most browsers. To create a grid, you'll need to use the grid-template-columns, grid-template-rows, and grid-template-areas properties.
These properties work together to define the structure of your grid. Grid-template-columns defines the number of columns, while grid-template-rows defines the number of rows. Grid-template-areas defines the areas of the grid where content will be placed.
Here are some of the key parent properties you'll need to know:
- display
- grid-template-columns
- grid-template-rows
- grid-template-areas
- grid-column-gap
- grid-row-gap
- grid-gap
- justify-items
- align-items
- place-items
- justify-content
- align-content
- place-content
- grid-auto-columns
- grid-auto-rows
- grid-auto-flow
- grid
Template-Columns
Grid Properties are flexible and can be customized to fit your needs.
The Grid Properties include Template-Columns, which determine the number of columns in a grid.
You can specify the number of columns using the grid-template-columns property.
Setting grid-template-columns to "repeat(3, 1fr)" creates a grid with 3 columns, each taking up an equal share of the available space.
Grid-template-columns can also be used to create grids with different column sizes.
Template-Rows
Template-Rows are a crucial aspect of Grid Properties. They determine the height of a grid container.
By setting the grid-template-rows property to a specific value, you can control the height of each row in the grid. For example, setting it to 100px will create a row that's 100 pixels high.
Grid-template-rows can also be set to repeat a pattern of rows. This is useful for creating grids with multiple rows of the same height. For instance, setting it to 100px 100px will create two rows, each 100 pixels high.
You can also use the repeat() function to set the grid-template-rows property. This function allows you to repeat a value a specified number of times. For example, repeat(3, 100px) will create three rows, each 100 pixels high.
Row-Gap
Row-gap is a property that controls the gap between rows in a grid container. It's a shorthand for row-gap and row-gap shorthand.
The value of row-gap can be a length, a percentage, or the keyword "normal". In the example of a grid container with a row-gap of 20px, the gap between each row is 20 pixels.
Frequently Asked Questions
What is CSS Grid garden?
CSS Grid Garden is a game that teaches CSS grid layout through interactive exercises. Learn CSS grid layout in a fun and engaging way at cssgridgarden.com.
How to set a grid system in CSS?
To set a grid system in CSS, define a container element as a grid with display: grid and specify column and row sizes with grid-template-columns and grid-template-rows. This establishes the foundation for placing child elements within the grid using grid-column and grid-row.
Is CSS Grid hard?
CSS Grid is a relatively easy technique to use once learned, but it requires a different mindset and understanding than traditional CSS layout methods. Its unique paradigm can be challenging to grasp at first, but with practice, it becomes more intuitive.
Sources
- https://css-tricks.com/snippets/css/complete-guide-grid/
- https://codingfantasy.com/games/css-grid-attack
- https://stephaniewalter.design/blog/fun-places-to-learn-css-layout-part-2-grid-layout/
- https://blog.raidboxes.io/en/webdesign-development/css-grid-layout/
- https://programmingmentor.com/post/playing-css-grid-garden/
Featured Images: pexels.com