Wrapper for working with CSS Grid
CdrGrid is a simple wrapper for working with CSS Grid. Any valid CSS Grid properties can be applied to a CdrGrid or it's grid items, allowing for more flexible layouts to be built using less markup and CSS classes.
CdrGrid applies a default responsive gutter which can be customized using the gutter
prop or overridden completely using CSS. The examples on this page are meant to illustrate some basic usage of CSS grid but are by no means exhaustive. Note that when constructing page layouts your entire page should be wrapped in a single cdr-container to ensure the proper outer margins are maintained.
New to or unfamiliar with CSS Grid? We recommend these resources for getting up to speed with CSS Grid:
Use rows and columns to lay out content by specifying equal widths for all columns. Columns have a minimum width, if columns cannot be spaced equally etc. new line
Define x-axis alignment and distribute space for all columns per row. Containers may have set widths or may be flexible with max widths defined. This applies to all columns with left as the default value.
Define y-axis alignment per row and distribute space across all columns per row. This applies to all columns with stretch as the default value.
Defines gutter size for all columns on a row and maintains gutter size by breakpoint. This applies to all columns. When this value is not set, default sizes are used.
The default gutter
value is medium@xs medium@sm large@md large@lg
.
Scrollable grids can be created using the grid-auto-flow
property set to column
for horizontal scrolling or row
for vertical scrolling.
For accessibility reasons it may make sense to construct your grid using list markup.
Column width can be controlled using the grid-template-columns
property.
Individual items can override their sizing with grid-column
and grid-row
;
Offsets can be created on grid items using the grid-column-start
property.
Grids can be nested to any depth by passing another CdrGrid in as a grid item.
CSS grid layouts using fr
units will be inherently responsive, however additional breakpoint-specific behaviors can be created using media queries.
.responsive-grid-example {
grid-template-columns: '1fr 1fr 1fr';
@include cdr-md-mq-down {
grid-template-columns: '1fr';
}
}
For grid layouts with an unknown number of items you may end up with an incomplete row at the end. These "leftover" or "orphan" columns can be styled using a combination of the last-child
/nth-last-child
and nth-child
selectors depending on how many items are in your grid row.
/* Target leftover grid columns */
.orphan-grid-example-2x2 div:last-child:nth-child(2n + 1) {
grid-column: 1 / span 2;
}
For grid layouts with an odd number of columns per row, make the grid-template and grid-items each twice as large so they can be offset as needed to center them.
/* Make grid columns take up twice as much space */
.orphan-grid-example-3x3 div { grid-column: span 2;}
/* If 2 columns are leftover, center them by moving the second to last element to the right by 1 */
.orphan-grid-example-3x3 div:nth-last-child(2):nth-child(3n + 1) {
grid-column: 2 / span 2;
}
/* If 1 column is leftover, center it by moving it to the right by 2 */
.orphan-grid-example-3x3 div:last-child:nth-child(3n + 1) {
grid-column: 3 / span 2;
}
Use grid-template-areas
to layout grid items named using the grid-area
property.
To ensure that usage of this component complies with accessibility guidelines:
Columns, gutters, and margins scales as a fluid system as the device and viewport increases from a small devices to a large device:
XS (Extra Small) | < 768px | 16px | 16px |
SM (Small) | ≥ 768px | 16px | 16px |
MD (Medium) | ≥ 992px | 32px | 32px |
LG (Large) | ≥ 1232px | 32px | 32px |
To build an effective responsive grid:
gutter
name
string
type
N/A
default
Defines gutter size. Default gutter size is 16px for @xs and @sm (medium) and 32px for @md and @lg (large). Possible values: { ‘none’ | ‘small’ | 'medium' | 'large' }. Also accepts responsive values @breakpoint: none@md’.
tag
name
string
type
div
default
Sets the tag type for the CdrGrid element. Accepts any HTML tag name that can function using `display: grid`. Useful for constructing list based layouts.
Find more information about using Slots in the article Installing Cedar.
default
name
Slot for CdrGrid content.
CdrGrid functions as a grid container, and it's immediate children are grid items.
cdr-container