grid: dabble in precision: fundamental principles

Meme

dabble in precision with css grid

CSS grid is one of the newer additions to CSS. It is unique among CSS layout techniques as it places elements using 2d grid coordinates rather than the default document flow.

It splits the "design of the layout" and the "placement of elements" into two distinct steps during development, forcing the developer to design the grid top-down first and then place the elements into it (as opposed to placing the elements into the DOM then working out which styles to apply to get the desired layout).

Grid is like its own layout language inside of CSS. It seems complicated at first, but once you learn the principles it becomes a pliable tool for laying out your designs as you envision (especially responsive designs)

Grid differs from other CSS layout techniques (like normal flow and flex) in that they typically use the size of children to implicitly size the parent or push sibling elements into the right position. Non-grid methods cannot easily have white space or align things along both the X and Y axes. Flex does not have the fr unit, but it can distribute free space to items using flex-grow and flex-shrink. Grid uses fr to distribute free space to tracks which allows further per-item customization. See #grid-vs-flex for details.

display:grid is applied to a parent container, and then all the direct child elements become grid items. Grid items stretch to fill the cell area they are placed into, both width and height wise. If you have not assigned a grid cell area to a child grid item, they will be placed using an auto-placement algorithm (by default it places child elements one per cell left-to-right, and then repeats on the next row down). Grid also allows a auto-creation of rows and columns that are created based on the grid items (no need to statically define the exact number of grid rows and columns).

Can I Use: Grid lists browser support at 98% - it's fine to use. But if you are developing for older browsers you can progressively enhance with @supports (display: grid) { }.

Lines and areas

Example 1 below has a DOM structure like this:

<div class="grid-container">
	<div class="red"></div>
	<div class="blue"></div>
</div>

Click two cells to place a element in that grid area:

📱📱open in tab and rotate phone
open in tab ↗

Gaps

Click white two cells to place a cell, then adjust the gap sizes:

📱📱open in tab and rotate phone
open in tab ↗

Sizing rows and columns (aka tracks)

fr

  • Click the white cells to place a grid item in that grid area - see how the item expands and contracts with the columns.
  • If you are on a desktop: resize your window to make it very narrow (resizes the grid container). Watch the fr values recalculate.
📱📱open in tab and rotate phone
open in tab ↗

minmax

  • When visualizing a set of tracks defined with minmax, consider what your design looks like at these four steps:
    • 1. Minimum widths all set.
      • What do the tracks look like when all the minimums are set?
    • 2. Static maximum widths all reached.
      • What do the tracks look like when all the static maximums are reached?
      • Note: All fr columns are still at their minimums at this step.
    • 3. Optional: fr column ratios are being balanced.
      • What do the tracks look like when the fr ratios are not yet balanced?
      • This is the most complicated step, and it may be easier just to ignore it and instead envision the jump between step 2 and step 4.
      • Minimum values will cause the fr ratios to be incorrect initially.
      • Space is assigned to the column with the smallest px-per-fr first (calculated by taking the current px width of the track and dividing it by its fr).
    • 4. fr column ratios maintained.
      • All fr tracks are in the correct fr ratios, all static maximums are reached.
  • The example below shows you each of these steps.
  • Change the grid container width and select different track configs - try to predict the track sizing before resizing the container. You can also add your own via the text box. Bookmark this as the minmax simulator to test your future designs.
📱📱open in tab and rotate phone
open in tab ↗

Auto track creation and sizing

  • Change "items" to set the total grid items.
    • All items have no grid-area set so will be auto-placed, auto-creating tracks if there is no space in the explicit grid.
  • Item colors:
    • Blue
      • Explicit grid cell found.
    • Green
      • Explicit grid cell not found, auto-placed in a created auto-track.
📱📱open in tab and rotate phone
open in tab ↗

Justify and Align

Test out the different justify and align values. Change the size of the grid container and set the -content values to space-x. Note that the space between the tracks behaves similarly as gap (it is added to the existing gap if it is set). Also note the space can be added to the edges of tracks (unlike gap).

📱📱open in tab and rotate phone
open in tab ↗

repeat: auto-fill vs auto-fit

Change the item count so that the first row is partially empty, then toggle auto-fill and auto-fit. Notice the tracks are stretched in auto-fit.

📱📱open in tab and rotate phone
open in tab ↗

Grid vs Flex