Skip to main content

Command Palette

Search for a command to run...

CSS Grid

Published
3 min readView as Markdown
CSS Grid

CSS Grid is a grid-based system, with rows and columns, allowing placement of elements in a webpage without using floats and positioning.

Grid is 2-dimensional grid based system. Tables, floats, positioning all had some hacks and some functionality which cannot be met. Though flexbox was a great tool to use, but this was a one-dimensional one.

To make an HTML element a grid, we need to set display property to grid or inline-grid.

ex: display:grid

To set the rows and columns, we use grid-template-rows and grid-template-columns. To have the spacing between items, we use gap property and to be more specific regarding which gap, we use row-gap or column-gap accordingly.

The property grid is directly applied to the parent of all grid items.

Flexible Grids

Flexible grids use fr unit. In addition to px and percentage, fr unit is used. fr refers to one fraction of the available space in the grid container.

The fr unit distributes space proportionally and we can specify any positive values.

The first track gets 2fr of the available space and the other two tracks get 1fr, making the first track larger. In this case, the space needed for the fixed tracks is used up first before the remaining space is distributed to the other tracks.

Gaps between tracks

To create gaps between tracks, we use the properties:

  • column-gap for gaps between columns

  • row-gap for gaps between rows

  • gap is used as shorthand for both

Grid with some gaps

Placement of elements in grid

Following are the properties used for placing the elements on the webpage.

  • grid-column-start

  • grid-column-end

  • grid-row-start

  • grid-row-end

These properties can all have a line number as their value. You can also use the shorthand properties:

  • grid-column

  • grid-row

Justify Content

Values:

  • start – aligns the grid to the start edge of the grid container

    %[https://codepen.io/Manasarala/pen/dyqgGNX?editors=1100]

  • end – aligns the grid to the end edge of the grid container

  • center – aligns the grid in the centre of the grid container

    %[https://codepen.io/Manasarala/pen/wvEYMgy?editors=1100]

  • stretch – resizes the grid items to allow the grid to fill the full width of the grid container

    %[https://codepen.io/Manasarala/pen/VwGEepK?editors=1100]

  • space-around – places an even amount of space between each grid item, with half-sized spaces on the far ends

    %[https://codepen.io/Manasarala/pen/abaRdLO?editors=1100]

  • space-between – places an even amount of space between each grid item, with no space at the far ends

    %[https://codepen.io/Manasarala/pen/xxayZPw?editors=1100]

  • space-evenly – places an even amount of space between each grid item, including the far ends

    %[https://codepen.io/Manasarala/pen/XWPxXVr?editors=1100]

Align Content

Values:

  • start – aligns the grid to be flush with the start edge of the grid container

    %[https://codepen.io/Manasarala/pen/gOdBPjv?editors=1100]

  • end – aligns the grid to be flush with the end edge of the grid container

    %[https://codepen.io/Manasarala/pen/rNZqxrN?editors=1100]

  • center – aligns the grid in the center of the grid container

    %[https://codepen.io/Manasarala/pen/MWqPKBM?editors=1100]

  • stretch – resizes the grid items to allow the grid to fill the full height of the grid container

    %[https://codepen.io/Manasarala/pen/LYJgGJb?editors=1100]

  • space-around – places an even amount of space between each grid item, with half-sized spaces on the far ends

    %[https://codepen.io/Manasarala/pen/wvEYMEx?editors=1100]

  • space-between – places an even amount of space between each grid item, with no space at the far ends

    %[https://codepen.io/Manasarala/pen/QWVZyZY?editors=1100]

  • space-evenly – places an even amount of space between each grid item, includes the far ends