HomeGuidesHTML & CSSCSS Flexbox Explained — justify-content, align-items & Flex Properties
🎨 HTML & CSS

CSS Flexbox: A Complete Guide for Exams and Interviews

Flexbox is the most-used layout system in modern CSS. Here's everything interviews test.

Examifyr·2026·6 min read

Flex container basics

Setting display: flex on a container makes its direct children flex items. The main axis is horizontal by default.

.container {
    display: flex;              /* enable flexbox */
    flex-direction: row;        /* main axis: horizontal (default) */
    flex-direction: column;     /* main axis: vertical */
    flex-wrap: nowrap;          /* don't wrap (default) */
    flex-wrap: wrap;            /* wrap to next line */
    gap: 16px;                  /* space between items */
}

Alignment properties

justify-content aligns items along the main axis. align-items aligns along the cross axis.

.container {
    /* Main axis (horizontal in row direction) */
    justify-content: flex-start;    /* default */
    justify-content: flex-end;
    justify-content: center;
    justify-content: space-between; /* gaps between items */
    justify-content: space-around;  /* gaps around items */
    justify-content: space-evenly;

    /* Cross axis */
    align-items: stretch;           /* default */
    align-items: flex-start;
    align-items: flex-end;
    align-items: center;
    align-items: baseline;
}
Note: justify-content = main axis (direction flex is going). align-items = perpendicular. This is what interviewers ask.

Flex item properties

flex-grow, flex-shrink, and flex-basis control how items size themselves.

.item {
    /* flex shorthand: grow shrink basis */
    flex: 1;        /* flex: 1 1 0% — fills available space */
    flex: 0 0 200px; /* fixed 200px, won't grow or shrink */
    flex: 2;        /* grows twice as fast as flex: 1 items */

    align-self: center;    /* override container's align-items */
    order: -1;             /* reorder visually (default: 0) */

    flex-grow: 1;   /* how much to grow (0 = don't grow) */
    flex-shrink: 0; /* how much to shrink (1 = can shrink) */
    flex-basis: auto; /* initial size before growing/shrinking */
}

Centering with flexbox

Perfectly centering an element — horizontally and vertically — is trivial with flexbox.

/* Center child both ways */
.parent {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

/* Sticky footer pattern */
body {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}
main {
    flex: 1;  /* main grows to push footer down */
}

Exam tip

The most common flexbox question: "Difference between justify-content and align-items?" — justify-content aligns along the main axis (direction of flex-direction); align-items aligns along the perpendicular cross axis.

🎯

Think you're ready? Prove it.

Take the free HTML & CSS readiness test. Get a score, topic breakdown, and your exact weak areas.

Take the free HTML & CSS test →

Free · No sign-up · Instant results

← Previous
CSS Box Model Explained — margin, padding, border & box-sizing
Next →
CSS Grid Explained — grid-template, fr units, Areas & Auto-fill
← All HTML & CSS guides