ENGIMY.IO - CHEATSHEET
FLEXBOX & GRID × QUICK REFERENCE
REFERENCE v1.0

Flexbox & Grid Quick Reference

Everything you need day‑to‑day – layouts, alignment, and responsive design.

Flexbox Overview

Key Concepts
  • One‑dimensional layout (row or column)
  • Flex container – parent with display: flex
  • Flex items – children of flex container
  • Main axis – direction of flex (row/column)
  • Cross axis – perpendicular to main axis
When to Use Flexbox
  • Navigation bars
  • Card layouts (equal height/width)
  • Centering content (horiz/vert)
  • Component alignment
  • Responsive reordering

Flexbox – Container Properties

display

.container {
    display: flex;          /* block‑level flex container */
    display: inline-flex;   /* inline‑level flex container */
}

flex-direction

.container {
    flex-direction: row;          /* left → right (default) */
    flex-direction: row-reverse;  /* right → left */
    flex-direction: column;       /* top → bottom */
    flex-direction: column-reverse; /* bottom → top */
}

flex-wrap

.container {
    flex-wrap: nowrap;    /* single line (default) */
    flex-wrap: wrap;      /* multiple lines, top → bottom */
    flex-wrap: wrap-reverse; /* multiple lines, bottom → top */
}

justify-content (Main Axis Alignment)

.container {
    justify-content: flex-start;   /* start (default) */
    justify-content: flex-end;     /* end */
    justify-content: center;       /* center */
    justify-content: space-between; /* equal space between items */
    justify-content: space-around; /* equal space around items */
    justify-content: space-evenly; /* equal space between and edges */
}

align-items (Cross Axis Alignment – Single Line)

.container {
    align-items: stretch;    /* fill container (default) */
    align-items: flex-start; /* start of cross axis */
    align-items: flex-end;   /* end of cross axis */
    align-items: center;     /* center of cross axis */
    align-items: baseline;   /* align by text baseline */
}

align-content (Cross Axis Alignment – Multiple Lines)

.container {
    align-content: stretch;      /* stretch lines (default) */
    align-content: flex-start;   /* start of cross axis */
    align-content: flex-end;     /* end of cross axis */
    align-content: center;       /* center of cross axis */
    align-content: space-between; /* equal space between lines */
    align-content: space-around; /* equal space around lines */
}

gap

.container {
    gap: 20px;        /* gap between items (both axes) */
    row-gap: 20px;    /* vertical gap */
    column-gap: 16px; /* horizontal gap */
}

Flexbox – Item Properties

flex-grow

.item {
    flex-grow: 0;   /* no growth (default) */
    flex-grow: 1;   /* takes available space */
    flex-grow: 2;   /* takes twice as much space */
}

flex-shrink

.item {
    flex-shrink: 1;   /* shrink when needed (default) */
    flex-shrink: 0;   /* no shrink (prevents squishing) */
}

flex-basis

.item {
    flex-basis: auto;  /* based on width/height (default) */
    flex-basis: 100px; /* fixed base size */
    flex-basis: 0;     /* size based on available space */
}

flex (Shorthand)

.item {
    flex: 0 1 auto;  /* grow shrink basis (default) */
    flex: 1;         /* flex: 1 1 0 (takes available space) */
    flex: none;      /* flex: 0 0 auto (no flexing) */
    flex: 2 0 100px; /* custom values */
}

align-self (Override align-items)

.item {
    align-self: auto;        /* default (inherits from container) */
    align-self: flex-start;  /* start of cross axis */
    align-self: flex-end;    /* end of cross axis */
    align-self: center;      /* center of cross axis */
    align-self: stretch;     /* stretch to fill */
    align-self: baseline;    /* align by baseline */
}

order

.item {
    order: 0;    /* default order (DOM order) */
    order: -1;   /* move to front */
    order: 1;    /* move to back */
    order: 5;    /* higher number = later */
}

Flexbox – Common Examples

Centering (Horizontal + Vertical)

.container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

Navigation Bar

.nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
}
.nav-links {
    display: flex;
    gap: 24px;
}

Equal Height Cards

.card-container {
    display: flex;
    gap: 20px;
}
.card {
    flex: 1;  /* all cards equal width/height */
}

Responsive (Wrap)

.container {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
}
.item {
    flex: 1 1 200px;  /* min 200px, grow/shrink as needed */
}

CSS Grid Overview

Key Concepts
  • Two‑dimensional layout (rows & columns)
  • Grid container – parent with display: grid
  • Grid items – children of grid container
  • Grid lines – numbered lines separating cells
  • Grid tracks – rows and columns
  • Grid cells – intersection of row & column
When to Use Grid
  • Overall page layout
  • Gallery / masonry layouts
  • Complex grid‑based designs
  • Responsive layouts with explicit control
  • Overlapping elements

Grid – Container Properties

display

.container {
    display: grid;          /* block‑level grid container */
    display: inline-grid;   /* inline‑level grid container */
}

grid-template-columns & grid-template-rows

.container {
    /* Fixed values */
    grid-template-columns: 100px 200px 100px;
    grid-template-rows: 150px 100px;

    /* Fraction units (fr) – flexible */
    grid-template-columns: 1fr 2fr 1fr;   /* 25% 50% 25% */

    /* Repeat function */
    grid-template-columns: repeat(4, 1fr); /* 4 equal columns */
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); /* responsive */

    /* Auto sizing */
    grid-template-columns: auto 1fr auto; /* content, flexible, content */

    /* Named lines */
    grid-template-columns: [col-start] 1fr [col-end] 2fr [col-end];
}

grid-template-areas

.container {
    grid-template-areas:
        "header header header"
        "sidebar main main"
        "footer footer footer";
    grid-template-columns: 200px 1fr 1fr;
    grid-template-rows: 80px 1fr 60px;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }

gap

.container {
    gap: 20px;        /* gap between rows & columns */
    row-gap: 20px;    /* vertical gap */
    column-gap: 16px; /* horizontal gap */
}

justify-items (Horizontal Alignment – Inside Cell)

.container {
    justify-items: stretch;   /* fill cell (default) */
    justify-items: start;     /* left */
    justify-items: end;       /* right */
    justify-items: center;    /* center */
}

align-items (Vertical Alignment – Inside Cell)

.container {
    align-items: stretch;   /* fill cell (default) */
    align-items: start;     /* top */
    align-items: end;       /* bottom */
    align-items: center;    /* center */
}

justify-content (Align Grid – When Smaller Than Container)

.container {
    justify-content: start;     /* left (default) */
    justify-content: end;       /* right */
    justify-content: center;    /* center */
    justify-content: space-between; /* space between */
    justify-content: space-around;  /* space around */
    justify-content: space-evenly;  /* space evenly */
}

align-content (Align Grid – When Smaller Than Container)

.container {
    align-content: start;     /* top (default) */
    align-content: end;       /* bottom */
    align-content: center;    /* center */
    align-content: space-between; /* space between rows */
    align-content: space-around;  /* space around rows */
}

grid-auto-flow (Item Placement)

.container {
    grid-auto-flow: row;       /* fill rows (default) */
    grid-auto-flow: column;    /* fill columns */
    grid-auto-flow: dense;     /* fill gaps aggressively */
}

Grid – Item Properties

grid-column & grid-row

.item {
    /* Span from line 1 to line 3 (col 1-2) */
    grid-column: 1 / 3;
    grid-row: 1 / 3;

    /* Span using keywords */
    grid-column: 2 / span 3;  /* start at 2, span 3 columns */

    /* Using named lines */
    grid-column: col-start / col-end;

    /* Shortcut: start and span */
    grid-column: 2 / span 2;
}

grid-area (Shorthand for grid-row + grid-column)

.item {
    /* row-start / col-start / row-end / col-end */
    grid-area: 1 / 1 / 3 / 5;

    /* Using named areas (from grid-template-areas) */
    grid-area: header;
}

justify-self (Override justify-items)

.item {
    justify-self: stretch;   /* fill cell (default) */
    justify-self: start;     /* left */
    justify-self: end;       /* right */
    justify-self: center;    /* center */
}

align-self (Override align-items)

.item {
    align-self: stretch;   /* fill cell (default) */
    align-self: start;     /* top */
    align-self: end;       /* bottom */
    align-self: center;    /* center */
}

order

.item {
    order: 0;   /* default */
    order: 1;   /* move later */
    order: -1;  /* move earlier */
}

Grid – Common Examples

Basic Page Layout

.container {
    display: grid;
    grid-template-areas:
        "header header header"
        "sidebar main main"
        "footer footer footer";
    grid-template-columns: 250px 1fr 1fr;
    grid-template-rows: 80px 1fr 60px;
    gap: 16px;
    min-height: 100vh;
}

Responsive Gallery

.gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 16px;
}
.gallery-item {
    aspect-ratio: 1 / 1;  /* square */
}

Masonry / Unequal Heights

.grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-auto-rows: 150px;
    gap: 16px;
}
.item-tall {
    grid-row: span 2;  /* spans 2 rows (300px) */
}
.item-wide {
    grid-column: span 2;  /* spans 2 columns */
}

Card Grid with Overlap

.container {
    display: grid;
    grid-template-columns: 1fr 1fr;
}
.card {
    grid-column: 1 / 2;
    grid-row: 1 / 2;
}
.card-featured {
    grid-column: 1 / 3;
    grid-row: 1 / 2;
}

Flexbox vs Grid – When to Use

Use Case Flexbox Grid
Navigation bars
Centering content
Card layouts (equal height)
Page layout (header/sidebar/main/footer)
Multi‑column layouts
Complex grid structures
Overlapping elements
Responsive reordering
Component alignment

Quick Decision

  • Flexbox – for components, single‑dimensional layouts (rows or columns), alignment, centering
  • Grid – for page‑level layouts, two‑dimensional layouts (rows AND columns), complex structures
  • Use both – grid for overall layout, flexbox inside grid cells for alignment
📌 Quick Reference
Flexbox: 1D layout, main axis (flex-direction), cross axis (align-items/align-self), gap
Flex container: display: flex, flex-direction, flex-wrap, justify-content, align-items, align-content, gap
Flex items: flex-grow, flex-shrink, flex-basis, flex (shorthand), align-self, order
Grid: 2D layout, grid-template-columns/rows, grid-template-areas, gap, justify/align-items, justify/align-content
Grid items: grid-column, grid-row, grid-area, justify-self, align-self, order
Use Flexbox for: components, alignment, centering, navigation
Use Grid for: page layout, complex structures, overlapping elements
← Back to All Cheatsheets