Skip to content

[css-grid-2] Example 5 - Multiple issues #11848

Open
@gitspeaks

Description

@gitspeaks

Section 2.3 starts with the following example:

The following example justifies all columns by distributing any extra space among them, and centers the grid in the grid container when it is smaller than 100vh.

main {
  grid: auto-flow 1fr / repeat(auto-fill, 5em);
  min-height: 100vh;
  justify-content: space-between;
  align-content: safe center;
}

Issues:

  1. The example omits setting display: grid on the <main> element, which means the grid properties are not applied.

  2. The example sets the implicit grid rows to 1fr, which causes each row (or even a single row) to expand to fill the available vertical space dictated by min-height: 100vh. Regardless of whether there is one row or multiple rows, all the rows take up the full available height, leaving no extra vertical space for the align-content: safe center property to have an effect.

  3. The declaration repeat(auto-fill, 5em) creates as many 5em-wide columns as will fit along the inline axis, effectively consuming all available inline space. This means there is no leftover free space for justify-content: space-between to distribute among the columns, contrary to what the example’s description implies.

Suggested revision:

The following example vertically centers the grid in the grid container when it is smaller than 100vh.

  main {
    display: grid;
    grid: auto-flow auto / repeat(auto-fill, 5em);
    min-height: 100vh;
    align-content: safe center;
  }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions