Experimental auto-centering css grid helper → Demo
import Monogrid from 'monogrid'
const grid = new Monogrid(document.querySelector('.monogrid'))
// center grid, re-center on resize
grid.observe()
// stop resize observer
grid.unobserve()
css grid is used for laying out elements on a true grid with coherent column and row gutters. These grids often expect explicit column start positions, and for that reason, do not handle auto centering (unlike flex’s justify-content
). Certain layouts are not possible: try centering a 5-column block on a 12-column css grid.
monogrid
unlocks flex style auto centering for css grids. Each row width is measured, and if it does not match the parent grid width, the row will be nudged to the center.
Initialize a monogrid
instance with a display:grid
dom element.
Measure each row and center those which do not fill the width of the grid.
Boot up a ResizeObserver
to re-center the grid on element resize.
Stop observing the element.
Override monogrid
behavior on a per-element basis by explicitly setting grid-column-start
. In the example below, the highlighted item has grid-column-start: 1
defined, so the row will not be centered and remain locked to the left.
-
on('render')
event -
once('render')
event