1. Converter Tools > 
  2. Cheatsheets > 
  3. CSS > 
  4. CSS Flexbox Cheatsheet

CSS Flexbox Cheatsheet

Quick reference for Flexbox: container and item properties, with common layout patterns.

CSS Flexbox Cheatsheet

Container: display and direction

Put display: flex on the parent. Children become flex items and sit in a row by default. Use flex-direction: column to stack them vertically. flex-wrap: wrap lets items move to the next line when there’s no room.

.nav { display: flex; gap: 1rem; }
.stack { display: flex; flex-direction: column; gap: 0.5rem; }
.cards { display: flex; flex-wrap: wrap; gap: 1rem; }

Aligning on the main axis (justify-content)

How items are distributed along the row (or column): at the start, end, center, or with space between them.

/* logo left, nav right */
.header { display: flex; justify-content: space-between; align-items: center; }

/* centered block */
.hero { display: flex; justify-content: center; align-items: center; min-height: 100vh; }

/* buttons with equal space between */
.toolbar { display: flex; justify-content: space-between; gap: 0.5rem; }

Values: flex-start, flex-end, center, space-between, space-around, space-evenly.

Aligning on the cross axis (align-items)

When items are in a row, this controls vertical alignment. Default is stretch (items fill the height). Use center to middle them, or flex-start / flex-end.

.card-row {
  display: flex;
  align-items: stretch;  /* same height cards */
  gap: 1rem;
}
.centered-row {
  display: flex;
  align-items: center;  /* vertically centered */
  gap: 0.5rem;
}

Gap

gap: 1rem adds space between items. No need for margin hacks. Use row-gap and column-gap if you want different spacing.

Items: flex grow, shrink, basis

flex: 1 means the item can grow to fill space and shrink if needed. Great for equal-width columns. flex: 0 0 200px means fixed 200px, no grow or shrink (e.g. a sidebar).

/* three equal columns */
.cols { display: flex; }
.cols > * { flex: 1; }

/* sidebar 240px, rest for content */
.layout { display: flex; }
.sidebar { flex: 0 0 240px; }
.main { flex: 1; }

One item can override the parent’s alignment: align-self: center. Change visual order without touching the DOM: order: 1 (higher number = later).

Flex-shrink and overflow. By default flex items have flex-shrink: 1, so they can shrink when space is tight. Long text or a wide image can force the layout. Give the item min-width: 0 (or overflow: hidden and a width) so it can shrink below its content size. Cards in a flex row often need min-width: 0 so the text truncates or wraps instead of bursting the row.

Full examples

Center a single block in the viewport:

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

Row of cards that wrap, with equal height in each row:

.gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}
.gallery .card {
  flex: 1 1 200px;
  min-width: 0;
}

Sticky footer (main fills the space, footer at bottom):

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

Navbar: logo left, links right. The logo doesn’t grow, the links sit at the end. Use margin-inline-start: auto on the nav so it pushes to the right, or wrap the links in a flex container with justify-content: flex-end.

.navbar {
  display: flex;
  align-items: center;
  gap: 1rem;
}
.navbar .links {
  margin-inline-start: auto;
  display: flex;
  gap: 0.5rem;
}

Form row: label and input side by side. Flex the row, give the label a fixed or min width so inputs align.

.form-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.form-row label { min-width: 8ch; }
.form-row input { flex: 1; min-width: 0; }

Wrap behavior. With flex-wrap: wrap, items wrap when they don’t fit. Use flex: 1 1 200px so each item has a minimum width (e.g. 200px) but can grow. Without a basis or min-width, very small items can bunch up. With only flex: 1 and no wrap, they share space equally and never wrap.

Use our CSS Color cheatsheet for colors.

Ad
Converter Tools

  • Home


    • Conversions
      • Length Converter
      • Area Converter
      • Volume Converter
      • Color Converter
      • Weight Converter
      • Temperature Converter
      • Angle Converter
      • Time Converter
      • Data Size Converter
      • Energy Converter
      • Number Base Converter
    • Calculators
      • Lengths
        • Feet and Inches Calculator
        • Pythagorean Theorem Calculator
        • Circle Circumference Calculator
        • Circle Area Calculator
        • Triangle Area Calculator
        • Rectangle Area and Perimeter Calculator
      • Percentages
        • What is X% of Y Calculator
        • X is What Percent of Y Calculator
        • Percentage Change Calculator
        • Add or Subtract Percentage Calculator
      • Time
        • Age Calculator
        • Date Difference Calculator
        • Day of Week Calculator
        • Countdown Calculator
        • Week Number Calculator
      • Body indices
        • BMR Calculator
        • TDEE Calculator
        • Ideal Weight Calculator
        • Body Fat Percentage Calculator
        • Waist-to-Hip Ratio Calculator
        • Calorie Deficit Calculator
        • BMI Calculator
      • Finance
        • Tip Calculator
        • Discount Calculator
        • Simple Interest Calculator
        • Compound Interest Calculator
        • Loan Payment (EMI) Calculator
      • Math
        • Quadratic Equation Solver
        • Factorial Calculator
        • GCD and LCM Calculator
        • Prime Number Checker
    • Dev Tools
      • Base64 Encode and Decode
      • Password Generator
      • QR Code Generator
      • Regex Tester
      • UUID Generator
    • Images
      • Image Conversions
      • Image Compression
      • Image Resize
      • Image Grayscale
      • EXIF Reader
      • Image Filters
      • Edge Detection
      • Image Crop
      • Image Rotate & Flip
      • Image Format Info
      • Image Thumbnail
    • File conversions
      • CSV and Excel Converter
    • PDF editing
      • Merge PDFs
      • Split PDF
      • Extract PDF pages
      • Rotate PDF pages
      • Add watermark to PDF
      • PDF metadata
    • Cheatsheets
      • CSS
        • CSS Color Cheatsheet
        • CSS Common Properties Cheatsheet
        • CSS Flexbox Cheatsheet
        • CSS Grid Cheatsheet
      • HTML
        • HTML Cheatsheet
      • Markdown
        • Markdown Cheatsheet
      • Git
        • Git Cheatsheet
      • Regex
        • Regex Cheatsheet
      • Time
        • Unix Timestamp Cheat
    • About
    • Contact
    • Privacy Policy
    • Terms of Use

        Built with by Hugo

        We use cookies and similar technologies for ads (e.g. Google AdSense) and analytics. By continuing you consent to our Privacy Policy. You can change preferences anytime.