LCM Logo
Web Development

Tailwind CSS

Here is a very small selection of useful tailwind classes.

Flexbox vs. Grid: when to use which

  • Flexbox (flex): layout in one dimension — a single row or a single column. Use for navbars, button groups, card footers, or any time items flow in one direction.
  • Grid (grid): layout in two dimensions — rows and columns simultaneously. Use for page layouts, dashboards, image galleries, or any time you need items aligned on both axes.

flex vs. grid rule of thumb

  • If you're arranging items along one axis, use flex.
  • If you need to control placement on both axes, use grid.

Flexbox

flex creates a one-dimensional container. Items are laid out along a single main axis — horizontal by default.

Terminology

TermMeaning
Main axisThe direction items flow (flex-row = horizontal, flex-col = vertical)
Cross axisThe perpendicular axis
justify-*Align along the main axis
items-*Align along the cross axis

Direction

ClassMain axisItems flow
flex-row (default)horizontal →left to right
flex-row-reversehorizontal ←right to left
flex-colvertical ↓top to bottom
flex-col-reversevertical ↑bottom to top

Alignment

Because justify-* and items-* follow the main axis, their visual effect depends on direction:

Goalflex-rowflex-col
Horizontal alignmentjustify-{start,center,end,between}items-{start,center,end}
Vertical alignmentitems-{start,center,end}justify-{start,center,end,between}

Common combo — center both axes: flex items-center justify-center

Wrapping and gaps

ClassEffect
flex-wrapAllow items to wrap to the next line
gap-{n}Uniform gap between items

Grid

grid creates a two-dimensional container. You define columns (and optionally rows) explicitly.

Terminology

TermMeaning
Columns / rowsThe explicit tracks you define
justify-items-*Align each item horizontally within its cell
items-*Align each item vertically within its cell
justify-* (on container)Distribute columns horizontally within the grid
content-*Distribute rows vertically within the grid

Defining the grid

ClassEffect
grid-cols-{n}Equal-width columns (e.g. grid-cols-3)
grid-rows-{n}Explicit row count
gap-{n}Uniform gap between cells
gap-x-{n} / gap-y-{n}Axis-specific gaps

Alignment

In grid, the axes are always horizontal and vertical — they don't swap like in flex.

GoalClass
Center items horizontally in cellsjustify-items-center
Center items vertically in cellsitems-center
Center the whole grid horizontallyjustify-center
Center the whole grid verticallycontent-center
Center everythingplace-items-center (shorthand)

Spanning

ClassEffect
col-span-{n}Item spans n columns
row-span-{n}Item spans n rows

Flexbox vs. Grid alignment cheat sheet

GoalFlexbox (flex-row)Flexbox (flex-col)Grid
Horizontal centerjustify-centeritems-centerjustify-items-center
Vertical centeritems-centerjustify-centeritems-center
Center bothitems-center justify-centeritems-center justify-centerplace-items-center
Space items apartjustify-betweenjustify-betweenn/a (use gap-{n})

Character spacing

tracking-* controls letter-spacing:

ClassValue
tracking-tighter-0.05em
tracking-tight-0.025em
tracking-normal0em
tracking-wide0.025em
tracking-wider0.05em
tracking-widest0.1em

Arbitrary values: tracking-[0.2em]

Line height

leading-* controls line-height:

ClassValue
leading-none1
leading-tight1.25
leading-snug1.375
leading-normal1.5
leading-relaxed1.625
leading-loose2

Fixed values (useful for precise control): leading-{n} where n is a spacing scale value (e.g. leading-6 = 1.5rem / 24px).

Arbitrary values: leading-[1.8]

Resources

On this page