Grid & Layout
Three ways to arrange content, plus a full toolkit for positioning. The single most important thing on this page is the golden rule of RWD in the pitfalls section - the most common mistake in the whole framework. Ready-made layouts: examples.
1. What to choose
| Tool | When | Who decides the column count |
|---|---|---|
.grid-auto |
Repeating tiles: product cards, a service grid, a portfolio list. | The browser. You provide a minimum tile width. |
.grid-cols-* |
A designed layout: 8 columns of text + a 4-column sidebar. | You, separately at every breakpoint. |
.d-flex |
A single row of elements: a toolbar, a card header, a group of buttons. | Not applicable - it's a single axis. |
.bento-grid |
A mosaic of differently sized tiles - a homepage, a dashboard. | You, via each tile's span. |
Two breakpoints, not five
molique only has md (from 768px) and
lg (from 992px). Unprefixed classes work
from zero upward, i.e. they describe the
mobile view. The -md- variant overrides them
from tablet width, and -lg- from desktop width.
Fewer breakpoints means fewer places for the layout to fall apart.
2. Auto-grid
The simplest tool and usually the right choice. You don't declare a column count - you give a minimum tile width, and the browser fits as many as it can. As the window narrows it reduces the column count on its own, no media queries needed.
<div class="grid-auto">
<div class="card">…</div>
<div class="card">…</div>
<div class="card">…</div>
</div>
<!-- Custom minimum width -->
<div class="grid-auto" style="--grid-min: 240px;">…</div>
| Class | Tile threshold | What for |
|---|---|---|
.grid-auto-sm |
200px |
Icons, small stat tiles. |
.grid-auto |
280px |
The default - cards with text. |
.grid-auto-lg |
350px |
Cards with a photo, articles. |
Why it never overflows
The rule is
minmax(min(100%, var(--grid-min)), 1fr).
The min() function is the key here: when the
window is narrower than the tile threshold,
100% wins and the tile shrinks down to the
screen's width. Without it, it would stay at a fixed 280px
and overflow off a phone screen.
3. 12-column grid
For when the layout should be designed, not calculated. The
parent gets the column count, the children get a span. The
classes exist in three flavors: unprefixed (mobile), with
-md-, and with -lg-.
<!-- Mobile: one column (default). Desktop: 8 + 4 -->
<div class="grid-md-cols-12 gap-4">
<article class="col-md-span-8">Content</article>
<aside class="col-md-span-4">Sidebar</aside>
</div>
You don't need grid-cols-1
The .grid-md-cols-* and
.grid-lg-cols-* classes are
a single-column grid by default below their
own threshold - adding .grid-cols-1 changes
nothing. It used to be required, and its absence used to
produce a sneaky symptom: the layout looked fine, but
gap stopped working on a phone, because the
element wasn't a grid at that point. For another column count
on a phone, set it alongside:
grid-cols-2 grid-md-cols-4 gives two columns, and
four from the md breakpoint.
| Family | Range | What it does |
|---|---|---|
.grid |
- | Turns on the grid without setting columns. |
.grid-cols-* |
1-12 |
The parent's column count. Variants:
.grid-md-cols-*,
.grid-lg-cols-* - both single-column by
default below their own threshold.
|
.col-span-* |
1-12 |
How many columns a child occupies. Variants:
.col-md-span-*, .col-lg-span-*.
Don't use it on mobile - see pitfall #1.
|
.col-start-* |
1-12, auto |
Which column an element starts at. Variants:
.col-md-start-*, .col-lg-start-*.
|
.offset-* |
0-11 |
Classic shift by N columns. Variants:
.offset-md-*, .offset-lg-*.
|
.gap-* |
0-5 |
Gap between elements. Variant:
.gap-md-*. Also works in flexbox.
|
col-start or offset
They do the same thing two different ways.
.col-start-4 says "start at the fourth
column" - an absolute position, readable when designing a
layout. .offset-3 says "shift over by three" -
more convenient when porting code from Bootstrap. Pick one
and stick with it within a project.
4. Flexbox
For a single axis: a bar with a logo and buttons, a card header, a footer with actions. Use a grid for page layout.
| Class | What it does |
|---|---|
.d-flex |
Turns on flexbox. Row by default. |
.flex-column |
Lays out as a column. Variants:
.flex-md-column, .flex-md-row -
e.g. a column on mobile, a row on desktop.
|
.flex-shrink-0 |
The element does not shrink when a row runs out of room, e.g. a button next to a long text. |
.flex-row |
Lays children out in a row. Needed where a component has its own direction: .card-body is a column, and .d-flex alone does not change that. .flex-md-row and .flex-md-column win the same way. |
.flex-wrap |
Lets elements wrap onto the next lines. |
.justify-content-start |
Distribution along the main axis. Also:
.justify-content-center,
.justify-content-end,
.justify-content-between.
|
.align-items-center |
Alignment across the cross axis. Also:
.align-items-start,
.align-items-end,
.align-items-stretch.
|
.align-self-center |
The same, but for one child. Also:
.align-self-start,
.align-self-end,
.align-self-stretch.
|
.d-block |
The other display modes:
.d-inline-block, .d-grid,
.d-none.
|
.d-md-none |
Visibility depending on width. Also
.d-md-block, .d-md-flex - e.g. a
menu shown only from tablet width up.
|
.justify-content-md-* |
Alignment along the main axis, from MD up only. Same
endings as the base classes: start,
end, center,
between. Likewise
.align-items-md-* and
.align-self-md-* (with
stretch), plus
.flex-md-wrap /
.flex-md-nowrap.
|
.order-* |
Visual order without touching the DOM:
.order-first (-1),
.order-last (99),
.order-0 through .order-5,
plus -md- and -lg-. Works in
Grid too.
|
Alternating sections without breaking reading order
The rhythm of an offer page - image left, then right. The point: the markup stays in reading order (copy first, image second) and the swap happens visually only, from MD up. On a phone and in a screen reader the order stays the same and makes sense. Narrow the window to see the difference.
Odd section
The copy comes first in the markup.
Even section
Same markup, the image moves visually only.
<div class="d-flex flex-column flex-md-row align-items-md-center gap-3">
<div>Copy - ALWAYS first in the markup</div>
<div class="order-md-first">Image - jumps visually only</div>
</div>
5. Positioning and sizes
| Class | Group | What it does |
|---|---|---|
.position-relative |
Position |
A reference point for children. Also:
.position-absolute,
.position-fixed,
.position-sticky.
|
.top-0 |
Edges |
Pinning to an edge: .bottom-0,
.left-0, .right-0.
.inset-0 pins to all of them at once.
|
.top-50 |
Centering |
Together with .left-50 and
.translate-middle centers an element in
its parent. Individually: .translate-middle-x,
.translate-middle-y.
|
.z-10 |
Layers |
Stacking order within a section: .z-0,
.z-20, .z-30. Global
components use the --z-index-* variables
instead - see pitfall #3.
|
.w-100 |
Width |
Also .w-25, .w-50,
.w-75, .w-auto and
.w-md-* variants. .mw-100
caps the maximum - a fix for photos pushing the layout out.
|
.h-100 |
Height |
Full parent height. .vh-100 and
.min-vh-100 refer to the window instead.
|
.breakout-mobile |
Width |
Pulls an element out of the .container
padding on mobile (below 768px), all the way to the
screen edge - carousels, galleries, hero photos inside
article content. No effect on desktop. Zero
configuration, doesn't need to know the parent's padding value.
|
.text-center |
Text |
Content alignment: .text-start,
.text-end.
|
.fade-bottom |
Effect |
Content fading at the bottom edge instead of a hard
cutoff. Color: --fade-color, height:
--fade-height.
|
<!-- An element perfectly centered in its parent -->
<div class="position-relative" style="height: 200px">
<span class="position-absolute top-50 left-50 translate-middle">Center</span>
</div>
<!-- The carousel touches the screen edges on mobile, returns to
.container width on desktop -->
<div class="container">
<p>A regular paragraph with margins...</p>
<div class="carousel breakout-mobile">
<!-- ... -->
</div>
</div>
6. Bento grid
A mosaic of differently sized tiles - a layout familiar from product pages and dashboards. The parent sets the grid, and you stretch selected tiles across more columns or rows.
<div class="bento-grid">
<div class="card bento-col-2 bento-row-2">Big tile</div>
<div class="card">Small</div>
<div class="card bento-row-2">Vertical</div>
<div class="card bento-col-2">Horizontal</div>
</div>
| Class | What it does |
|---|---|
.bento-grid |
The mosaic grid. Density variants:
.bento-grid-3, .bento-grid-4.
|
.bento-col-2 |
A tile spanning two columns. Wider:
.bento-col-3.
|
.bento-row-2 |
A tile spanning two rows. Taller:
.bento-row-3.
|
7. Pitfalls
1
GOLDEN RULE: .col-span-* on mobile
The most common mistake in the whole
framework. Unprefixed classes work from zero
upward, meaning on the phone too. The
parent has .grid-cols-1 there - a single
column - so .col-span-12 tells the child to
take up twelve columns in a grid that only has one. The
layout blows apart.
In a single-column grid, an element takes up the full
width anyway. Declare the span
only with the -md- and
-lg- variants.
<!-- WRONG - col-span-12 also applies on the phone -->
<div class="grid-md-cols-12">
<div class="col-span-12 col-md-span-8">…</div>
</div>
<!-- RIGHT - the full width on mobile happens on its own -->
<div class="grid-md-cols-12">
<div class="col-md-span-8">…</div>
</div>
2 A rigid grid where auto-grid is enough
A list of cards doesn't need a designed layout. Writing
grid-cols-1 grid-md-cols-2 grid-lg-cols-3
is three decisions to maintain and three places to get
wrong. .grid-auto gives the same effect
with a single class and also adapts to intermediate
widths you never planned for.
3
Custom numbers in z-index
The .z-0-.z-30 classes order
layers within a single section - e.g.
content over an overlay on a photo. Don't use them for
global components: the modal, toast, and dropdown have a
fixed hierarchy in the --z-index-*
variables. Writing z-index: 9999 there as a
shortcut ends with a modal sitting under the navbar
somewhere else in the site entirely.
Full scale: CSS Variables.
4 A photo pushing the grid out
A grid item doesn't shrink below the size of its content
by default. A large photo or a long string with no
spaces (a URL, an account number) will push the column
out and trigger a horizontal scrollbar for the whole
page. For images, .mw-100 is enough.
5
.breakout-mobile nested too deep
The technique relies on margin-left: 50%
being calculated relative to the width of the
direct parent, and that parent needs to
be centered in the window the way .container
is (i.e. have margin-inline: auto). The
element works correctly as a direct child of
.container - but nested deeper, e.g. inside
a .card-body that itself sits inside the
container, it gets calculated relative to the card's
width, not the window's, and ends up shifted/clipped
instead of touching the screen edge. Move the class to
the nearest .container level, or wrap the
problem element in an extra <div>
directly inside it.