Normally in HTML, block elements span 100% of the available width and stack vertically, one under he other starting from the top of the viewport. CSS grid styling enables blocks to be arranged and aligned according to a pre-defined grid. The Codebase 6 two-dimensional grid system has up to 4 columns and 4 rows.
Let’s start with some basic concepts and terminology.
A grid consists of vertical and horizontal lines with square or rectangular spaces in between. In web design, we call the lines grid tracks and the spaces grid cells. The grid itself is not actually drawn on the browser screen – though its “guides” can be visualized in a browser’s code inspector (dev tools).
Web designers place HTML elements (called grid items in this context) on the grid. The CSS Grid engine “snaps” the elements to the grid tracks, and you can design them to sit on one grid cell or to span multiple cells.
Web browser screens can be any size from very small (phones) to very large (desktop or wall mounted displays). So, unlike with set grids paper-based poster or magazine page designs, and unlike with squared math or graph paper, CSS Grid can be stretchy, which is good for responsive layouts. Grid cells can have any width, dependant on the width of the browser screen, or dependant on the width of the Grid controlled area within a design. And grid cells can have any height, dependant on how much content (or, the height of the content) in an element that is places on the grid.
Codebase 6’s grid system has utility classes for controlling up to four column tracks and four row tracks.
grid
Grid items are immediate child elements of an element that has the grid CSS class. By default, grid items occupy one grid cell (the smallest rsquare or ectangle). The Codebase grid system has CSS classes for:
gap
equal-*-cols
gap-*
col-*
row-*
cols-{x}-{y}
rows-{x}-{y}
xs:
sm:
md:
lg:
The singular named col- and row- classes are used to instruct the grid which grid cell you want your grid item to be placed on (up to 4 columns and/or 4 rows). The plural named cols- and rows- classes can be used to make grid items to span up to 4 columns and/or 4 rows.
col-
row-
cols-
rows-
The grid class does not specify or predict where you will position or how you will span your grid items. It just supplies the style Grid to initialize your grid area. So far, your layout will still only be in rows. So, this is a grid, defaulting to one column wide and how ever many rows it requires to accommodate the grid items:
<div class="grid"> <div></div> <div></div> <div></div> </div>
You can use the xs:, sm:, md:, and lg: prefixes to cause the grid to take effect at different media query width breakpoint widths. So, with the base grid class, there are 5 grid system tiers.
xs:grid
sm:grid
md:grid
lg:grid
Below these breakpoints, the CSS grid layout will not take effect.
Grid items will automatically wrap onto a new row if your layout requires it. (This is unlike with flexbox, where you need to actually command a flexbox to wrap using a style rule.)
<div class="grid"> <div class="col-1"></div> <div class="col-2"></div> <div class="col-3"></div> <div class="col-1"></div> <div class="col-2"></div> <div class="col-3"></div> </div>
When you have wrapping rows like this, you actually don’t need to add col- classes to grid items that drop to the next row (and third row, etc.) because the CSS Grid engine will figure out what you want. After you have completed your required number of columns on your first row, CSS grid will automatically wrap and position the subsequent row’s grid items consecutively in the same columns as you specified in your first row.
The col- classes on subsequent rows are redundant but harmless – but you can include them if it helps you remember what you are doing, and to explain your intention to other designers in your collaboration.
<div class="grid"> <div class="col-1"></div> <div class="col-2"></div> <div class="col-3"></div> <div class="col-4"></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </div>
grid equal-*-cols specify how many columns your grid will have and. So, do you want equal-width columns, or not?
grid equal-*-cols
Example grid equal-3-cols:
grid equal-3-cols
<div class="grid equal-3-cols"> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </div>
A similar example but without equal-3-cols. This time the number of columns is specified by putting col-* on the first three columns. Different amounts of dummy text has been added to show you how the grid items expand or contract to accommodate it:
equal-3-cols
<div class="grid"> <div class="col-1">Lorem</div> <div class="col-2">ipsum dolor sit</div> <div class="col-3">amet consectetur adipisicing elit.</div> <div>Explicabo enim velit veniam reiciendis</div> <div>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Explicabo enim velit veniam reiciendis, vel inventore sequi quasi fugit sunt aliquam! Incidunt eum alias illo ea laudantium animi possimus amet sunt?</div> <div>Lorem ipsum</div> </div>
gap-* — adds a horizontal and vertical gap (same as for flexbox layouts):
gap-tiny
--s-tiny
gap-1
--s-1
gap-2
--s-2
gap-3
--s-3
gap-4
--s-4
Gap examples:
grid equal-3-cols gap-tiny:
grid equal-3-cols gap-tiny
grid equal-3-cols gap-2:
grid equal-3-cols gap-2
x
y
Not exemplified below: you can even overlap grid items by specifying that they occupy the same grid cell (whether entirely or in part). So, it’s a three dimensional grid system!
The following unlikely example will show you how positioning works.
You can use col- and or row- classes to display grid items in different positions, and even overlap them. However, with this added complexity, you will need to control both columns and rows (be pedantic), so that CSS grid layout can figure out what you wanting to do.
Here, grid items are rearranged into different columns. But I want still want them to be all in one row, therefore I must specify row-1 on each item, so that the CSS Grid engine gets it right.
row-1
<div class="grid"> <div class="col-2 row-1">First item</div> <div class="col-4 row-1">Second item</div> <div class="col-3 row-1">Third item</div> <div class="col-1 row-1">Fourth item</div> </div>
By default, one grid item sits on one grid cell – the grid item spans one column track wide and one row track high. But you can make grid items span up to 6 columns and/or up to 6 rows using the plural named classes cols-{x}-{y} and/or rows-{x}-{y} .
Note: grid items can occupy grid cells to form any square or recangular shape, but cannot form e.g. irregular “L” shape.
Thinking about one row: the first grid item can span up to six columns (columns 1 to 6). The second grid item can span up to five columns (columns 2 to 6). And so on. And it’s similar for spanning rows.
Example: the second column spans three grid cells, from 2 through 4.
<div class="grid equal-4-cols"> <div class="col-1"> ... </div> <div class="cols-2-4"> ... </div> </div>
Spanning multiple rows works the same way.
<div class="grid equal-3-cols"> <div class="col-1 rows-1-3"> ... </div> <div class="col-2"> ... </div> <div class="col-3"> ... </div> <div class=""> ... </div> <div class=""> ... </div> <div class=""> ... </div> <div class=""> ... </div> </div>
The grid-dense utility class has been added, to provide an easy way to pack grid cells by moving later items to fill up any empty cells that have been created earlier in the grid layout. grid-dense reorders the appearance of grid items. using grid-auto-flow: dense;.
grid-dense
grid-auto-flow: dense;
Notes on grid-dense:
Example grid with empty cells:
The same example grid with grid-dense added to the grid wrapper:
Same again, with a gap:
<div class="grid grid-dense equal-4-cols gap-2"> <div>Item 1</div> <div class="cols-1-2">Item 2</div> <div class="cols-2-4">Item 3</div> <div class="col-4">Item 4</div> <div class="col-3">Item 5</div> </div>
The grid class merely sets display: grid, without defining columns or rows or anything. Everything is controlled by gap, equal-, col-, cols-, row- and rows- classes.
display: grid
equal-
Besides the exemplified classes above, each of these grid control classes has four more media query breakpoint tiers in Codebase (xs:, sm:, md:, and lg:).
There's alot you can do simply by multiple tiers of grid columns control. Here’s a simple example: a “gallery” layout with 6 items, displaying as a single column on phones, 2 columns on tablets in portait orientation, and 3 columns on tablets in landscape orientation, and up:
<div class="grid sm:row-gap md:col-gap equal-3-cols"> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </div>
With control of up to 4 columns and control of up to 4 rows over 4 media breakpoints tiers (plus the base tier), this gives you a lot of layout versatility.
Docs