We make use of Tailwind CSS on most of our projects. We happened to start this before Tailwind released their newest version that included grid support, which meant making our own utilities. I won't dig into creating the grid classes themselves, but I will touch on some helpers we used. First off, we established what our "default" grid should be based on all of our designs. Generally, our base layout was a 4-column grid with 70px of grid-gap.
.grid--default {
@apply grid-columns-4 grid-gap-70;
}
In this case, the markup being generated for each of those tailwind classes is below. In grid 1fr
= 1 part of the available space.
.grid-columns-4 {
grid-template-columns: minmax(0,1fr) minmax(0,1fr) minmax(0,1fr) minmax(0,1fr);
}
.grid-gap-70 {
grid-gap: 4.375rem;
}
The other important utility we added were .grid__span
classes, which we generated with a mixin and loop in our SCSS.
This generates the class .grid__span--#{START_COLUMN}-#{COLUMNS_TO_SPAN}
. In our default 4-column layout (which is used on this page), we want the middle section to span across 2 columns, so we use .grid__span--2-2
. Wrapping it in @responsive
allows users to use it with our Tailwind defined breakpoints.
@mixin grid-span($start, $span) {
grid-column: $start / span $span;
}
@responsive {
@for $i from 1 through 4 {
@for $j from 1 through 4 {
.grid__span--#{$i}-#{$j} {
@include grid-span($i, $j);
}
}
}
}