CSS Cheatsheet

Flexbox

Use this CSS reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Enabling Flexbox

.container {
  display: flex;         /* block-level flex container */
  display: inline-flex;  /* inline-level flex container */
}
/* Direct children become flex items */

Flex Direction and Wrap (flex-flow shorthand)

/* flex-direction */
flex-direction: row;            /* default — left to right */
flex-direction: row-reverse;    /* right to left */
flex-direction: column;         /* top to bottom */
flex-direction: column-reverse; /* bottom to top */

/* flex-wrap */
flex-wrap: nowrap;       /* default — all on one line */
flex-wrap: wrap;         /* wrap to next line */
flex-wrap: wrap-reverse; /* wrap to previous line */

/* shorthand */
flex-flow: row wrap;
flex-flow: column nowrap;

Alignment on the Main Axis — justify-content

justify-content: flex-start;    /* default — items at start */
justify-content: flex-end;
justify-content: center;
justify-content: space-between; /* gaps between items */
justify-content: space-around;  /* equal space around items */
justify-content: space-evenly;  /* equal space between and edges */
justify-content: start;
justify-content: end;
justify-content: left;
justify-content: right;
justify-content: stretch;       /* only in multi-line containers */

Alignment on the Cross Axis — align-items

align-items: stretch;      /* default — fill cross-axis size */
align-items: flex-start;
align-items: flex-end;
align-items: center;
align-items: baseline;
align-items: first baseline;
align-items: last baseline;
align-items: start;
align-items: end;
align-items: self-start;
align-items: self-end;

Multi-line Cross-axis Alignment — align-content

Applies only when flex-wrap: wrap and there are multiple lines:

align-content: stretch;       /* default */
align-content: flex-start;
align-content: flex-end;
align-content: center;
align-content: space-between;
align-content: space-around;
align-content: space-evenly;
align-content: start;
align-content: end;

place-content and place-items Shorthands

/* place-content: align-content justify-content */
place-content: center;             /* both axes centered */
place-content: space-between center;

/* place-items: align-items justify-items */
place-items: center;
place-items: start end;

Gap

gap: 1rem;               /* row-gap and column-gap */
gap: 1rem 2rem;          /* row-gap column-gap */
row-gap: 1rem;
column-gap: 2rem;

Flex Item Properties

flex-grow

flex-grow: 0;   /* default — do not grow */
flex-grow: 1;   /* grow to fill available space */
flex-grow: 2;   /* take twice as much space as flex-grow: 1 siblings */

flex-shrink

flex-shrink: 1;  /* default — shrink proportionally */
flex-shrink: 0;  /* do not shrink */
flex-shrink: 2;  /* shrink twice as fast */

flex-basis

flex-basis: auto;   /* default — use width/height, or content size */
flex-basis: 0;      /* ignore content size */
flex-basis: 200px;
flex-basis: 25%;
flex-basis: max-content;
flex-basis: min-content;
flex-basis: fit-content;

flex Shorthand

/* flex: grow shrink basis */
flex: 0 1 auto;   /* default — same as initial */
flex: 1;          /* flex: 1 1 0 — grow and shrink, ignore content size */
flex: auto;       /* flex: 1 1 auto — grow and shrink, respect content size */
flex: none;       /* flex: 0 0 auto — rigid, no grow/shrink */
flex: 0 0 200px;  /* fixed-width column */
flex: 2 1 0;      /* twice the grow factor */

align-self

Overrides align-items for individual items:

align-self: auto;          /* inherits container's align-items */
align-self: flex-start;
align-self: flex-end;
align-self: center;
align-self: stretch;
align-self: baseline;

order

/* Items rendered in ascending order value */
.first  { order: -1; }
.normal { order: 0; }   /* default */
.last   { order: 1; }

Common Flexbox Patterns

Centering (vertically and horizontally)

.center {
  display: flex;
  justify-content: center;
  align-items: center;
}

Sticky Footer

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
main { flex: 1; }

Equal-width Columns

.row {
  display: flex;
  gap: 1rem;
}
.col { flex: 1; }

Sidebar + Main

.layout {
  display: flex;
  gap: 2rem;
  align-items: flex-start;
}
.sidebar { flex: 0 0 260px; }
.main    { flex: 1; }

Wrapping Card Grid

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}
.card {
  flex: 1 1 240px; /* min ~240px, grow to fill */
}

Navigation Bar

nav {
  display: flex;
  align-items: center;
  gap: 1rem;
}
.nav-spacer { flex: 1; } /* push right items to end */

Flex Item Sizing Algorithm (Summary)

  1. Start with flex-basis (or width/height if basis is auto).
  2. If there's leftover space: distribute according to flex-grow ratios.
  3. If there's overflow: shrink according to flex-shrink * flex-basis ratios.
  4. Apply min-width/max-width (or min-height/max-height) clamping.

min-width: 0 on flex items prevents them from overflowing when content is large (flex items default min-width is auto).

.flex-item {
  min-width: 0;    /* allow shrinking below content size */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

Flexbox vs Grid

Use flexbox for one-dimensional layouts (a row OR a column). Use grid for two-dimensional layouts (rows AND columns simultaneously). They compose — a flex item can also be a grid container.