Css Grid is a powerful layout system that allows you to create complex layouts with ease. It's perfect for building responsive and flexible game boards.
To start building a game board with Css Grid, you need to define a grid container. This is done by setting the display property to grid on a parent element. For example, the game board in the Tic Tac Toe example uses a grid container to create a 3x3 grid.
Grid templates are used to define the structure of the grid. In the Tic Tac Toe example, the grid template is defined as "repeat(3, 1fr)", which creates a grid with 3 rows and 1 fractional unit per row.
Grid items are the elements that are placed inside the grid container. In the Tic Tac Toe example, each cell in the game board is a grid item. Grid items can be sized and positioned using various properties, such as grid-template-columns and grid-auto-rows.
Grid Basics
Grid Layout is a two-dimensional layout system, optimized for user interface design, that allows children of a grid container to be positioned into arbitrary slots in a predefined flexible or fixed-size layout grid.
Grid Layout is a new layout model for CSS that has powerful abilities to control the sizing and positioning of boxes and their contents. Unlike Flexible Box Layout, which is single-axis–oriented, Grid Layout is optimized for 2-dimensional layouts: those in which alignment of content is desired in both dimensions.
A grid container's content is controlled through the use of a grid, an intersecting set of horizontal and vertical lines that create a sizing and positioning coordinate system for the grid container's contents.
Grid Layout features fixed, flexible, and content-based track sizing functions, explicit item placement via forwards and backwards numerical grid coordinates, named grid lines, and named grid areas.
Grid containers can be nested or mixed with flex containers as necessary to create more complex layouts.
Here are some key grid properties to keep in mind:
Grid Layout allows dramatic transformations in visual layout structure without requiring corresponding markup changes, making it a powerful tool for creating responsive and adaptable layouts.
Grid Properties
Grid Properties are a crucial part of the CSS Grid game. They determine how the grid behaves and is laid out.
The grid-template-columns property is used to define the number and size of columns in a grid. For example, you can use the grid-template-columns property to create a grid with two columns of equal width, like this: grid-template-columns: 1fr 1fr.
The grid-gap property is used to add space between grid items. By setting the grid-gap property to 10px, you can add a 10-pixel gap between each grid item.
Source-Order Independence
Source-Order Independence is a key benefit of grid layout. It allows authors to use the same semantic markup, but rearrange the layout of elements independent of their source order.
This means you can optimize the placement of components for different devices, like a game that adapts to both portrait and landscape orientations. By combining grid layout with media queries, you can achieve the desired layout in both orientations.
Grid layout's ability to name the space occupied by a grid item helps avoid rewriting rules for grid items as the grid's definition changes. This is useful for authors who want to manipulate the visual presentation without affecting the source order.
Note that grid item placement and reordering should not be used as a substitute for correct source ordering, as it can ruin the accessibility of the document.
Grid Properties
Grid properties are a fundamental aspect of CSS grid layout, and understanding them is crucial for building responsive and flexible user interfaces. Grid containers establish an independent grid formatting context for their contents, which is similar to establishing an independent block formatting context, but with grid layout instead of block layout.
Grid containers are not block containers, so some properties that were designed with the assumption of block layout don't apply in the context of grid layout. This means that float and clear have no effect on a grid item.
A grid container is sized using the rules of the formatting context in which it participates. In a block formatting context, it is sized like a block box that establishes a formatting context, with an auto inline size calculated as for non-replaced block boxes.
Here are some key differences in sizing a grid container:
- As a block-level box in a block formatting context, it is sized like a block box that establishes a formatting context.
- As an inline-level box in an inline formatting context, it is sized as an atomic inline-level box (such as an inline-block).
The max-content size of a grid container is the sum of the grid container's track sizes (including gutters) in the appropriate axis, when the grid is sized under a max-content constraint.
Flexible Lengths: Fr Unit
The fr unit is a dimension that represents a fraction of leftover space in a grid container. It's used to create flexible tracks that adapt to available space.
Flexible tracks are called as such because they flex in response to leftover space, similar to how flex items with a zero base size fill space in a flex container.
A track's fr value is a request for some proportion of leftover space, with 1fr meaning 100% of the leftover space.
If tracks request more than 100% in total, their requests are rebalanced to keep the same ratio but use up exactly 100% of the leftover space.
However, if tracks request less than the full amount, they'll each get exactly what they request, with any remaining space left unfilled.
This pattern is required for continuous behavior as fr values approach zero, meaning the tracks want none of the leftover space.
Authors should stick to values ≥ 1, as using smaller values can lead to unexpected behavior if tracks are added or removed.
When available space is infinite, flex-sized grid tracks are sized to their contents while retaining their respective proportions.
Named Areas
Named Areas are a powerful feature in Grid Layout, allowing you to create named grid areas that can be referenced from the grid-placement properties. These areas are not associated with any particular grid item, but can be used to simplify the layout of your grid container.
A grid area can be named explicitly using the grid-template-areas property of the grid container. This property specifies a list of string values that define the named grid areas.
To create a named grid area, you can use a named cell token, which creates a named grid area with the same name. Multiple named cell tokens within and between rows create a single named grid area that spans the corresponding grid cells.
Here's a breakdown of the syntax used to create named grid areas:
All strings must define the same number of cell tokens, and at least one cell token, or else the declaration is invalid. If a named grid area spans multiple grid cells, but those cells do not form a single filled-in rectangle, the declaration is invalid.
Named grid areas can be referenced from the grid-placement properties, such as grid-area, allowing you to place items into the named grid area.
Implicitly-Assigned Line Names
Implicitly-Assigned Line Names are generated by the grid-template-areas property from named grid areas in the template. They behave just like any other line names, except they don't appear in the value of grid-template-rows/grid-template-columns.
For each named grid area, four implicitly-assigned line names are created. Two of them are named foo-start, which name the row-start and column-start lines of the named grid area. Two others are named foo-end, which name the row-end and column-end lines of the named grid area.
Even if an explicitly-assigned line name with the same name is defined, the implicitly-assigned line names are just more lines with the same name. This means they don't override or replace the explicitly-assigned line names.
Sources
- Play Grid Attack – CSS Game to learn CSS Grid (codingfantasy.com)
- see it live here (mdn.github.io)
- the starting point file (github.com)
- line-based placement starting point file (github.com)
- center (csswg.org)
- start (csswg.org)
- Issue 5 (csswg.org)
- Apprendre le positionnement en s’amusant – Partie 2 : Grid Layout (creativejuiz.fr)
- Grid Garden (cssgridgarden.com)
- Grid Critters (gridcritters.com)
- Grid by example (gridbyexample.com)
- Grid Visual Playground (alialaa.github.io)
- CSS Grid Generator (netlify.com)
- CSS Grid Layout Generator (jhpratt.github.io)
- CSS Tricks’ A Complete Guide to Grid (css-tricks.com)
- CSS Tricks (css-tricks.com)
- GRID: A simple visual cheatsheet for CSS Grid Layout (malven.co)
- Video – How to use CSS grid layout in 60 seconds! (youtube.com)
- Layout Land – Videos on CSS Grid (youtube.com)
- cssgrid.io (cssgrid.io)
- Mozilla has a really cool step by step tutorial on CSS Grid Layout (mozilladevelopers.github.io)
- CSS Grid for Designers (nytimes.com)
- Learn CSS Grid (learncssgrid.com)
- The New CSS Layout (abookapart.com)
- Rachel Andrew’s book “Get Ready for CSS Grid Layout” (abookapart.com)
- CSS Grid Garden (cssgridgarden.com)
Featured Images: pexels.com