Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →

CSS Flexbox and Grid Interview Questions (With Layout Examples)

12 CSS Flexbox and Grid interview questions with code: when to use each, main and cross axis, justify-content vs align-items, flex-grow, flex-shrink and flex-basis, centering, gap, grid-template-areas, fr units, auto-fit vs auto-fill with minmax, and common layouts.

Sumit KumarUpdated Oct 12, 20266 min readCSS
CSS Flexbox and Grid Interview Questions (With Layout Examples)

Flexbox and Grid are the two modern CSS layout systems, and interviewers expect you to know when to use each and to write common layouts from memory. This post covers 12 Flexbox and Grid interview questions with answers and copy-ready CSS.

Flexbox versus CSS Grid: Flexbox lays items out in one dimension along a row or column and is content-first, while Grid lays items out in two dimensions with rows and columns and is layout-first

For CSS basics like the box model and specificity, start with CSS interview questions for freshers.

Choosing a layout system#

Q1. When should you use Flexbox and when should you use Grid?#

FlexboxGrid
DimensionsOne (row or column)Two (rows and columns)
ApproachContent-first: items decide their sizeLayout-first: the container defines tracks
Best forNavbars, button groups, toolbars, centering, aligning items in a cardPage layouts, card galleries, dashboards, forms with aligned labels
Overlapping itemsNot nativelyYes, items can share cells

Answer: they work together. A common pattern is Grid for the overall page and card gallery, and Flexbox inside each card or navbar.

Flexbox#

Q2. What are the main axis and cross axis?#

Answer: flex-direction sets the main axis. With row (the default) it runs horizontally; with column it runs vertically. The cross axis is perpendicular to it. justify-content aligns along the main axis and align-items along the cross axis, so changing the direction swaps what they control visually.

CSS
.toolbar {
  display: flex;
  flex-direction: row;          /* main axis: horizontal */
  justify-content: space-between; /* spread along the main axis */
  align-items: center;          /* vertically centre on the cross axis */
}

Q3. What is the difference between justify-content, align-items and align-content?#

PropertyAlignsCommon values
justify-contentItems along the main axisflex-start, center, space-between, space-around, space-evenly
align-itemsItems on the cross axis within a linestretch (default), center, flex-start, baseline
align-contentMultiple lines (only with flex-wrap: wrap)flex-start, center, space-between
align-selfOne item, overriding align-itemscenter, flex-end

Q4. Explain flex-grow, flex-shrink and flex-basis. What does flex: 1 mean?#

  • flex-basis: the item's starting size before free space is distributed (auto uses its width or content).
  • flex-grow: how much of the extra space the item takes, relative to siblings (default 0).
  • flex-shrink: how much the item shrinks when space is short (default 1).
CSS
.sidebar { flex: 0 0 250px; }  /* don't grow, don't shrink, 250px wide */
.content { flex: 1; }          /* flex: 1 1 0% — take all remaining space */

.a { flex: 1; }  /* with .b, splits space 1:2 */
.b { flex: 2; }

flex: 1 is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%, so items share space equally regardless of content. flex: auto uses flex-basis: auto, so items with more content get more space.

Common bug

Flex items have min-width: auto, so long text or wide content can stop them shrinking and cause overflow. Add min-width: 0 (or overflow: hidden) to the item to fix it.

Q5. How do you center an element horizontally and vertically?#

CSS
/* Flexbox */
.parent {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

/* Grid: the shortest version */
.parent {
  display: grid;
  place-items: center;
  min-height: 100vh;
}

/* Absolute positioning (when the parent isn't flex/grid) */
.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Also mention margin: auto on a flex child, which centres it in both directions.

Q6. What is the gap property, and how does flex-wrap work?#

CSS
.tags {
  display: flex;
  flex-wrap: wrap;   /* items move to a new line instead of overflowing */
  gap: 8px 12px;     /* row-gap column-gap */
}

Answer: gap adds space between items only, not around the edges, replacing margin hacks like negative margins or :last-child resets. It works in Flexbox, Grid and multi-column layouts. flex-wrap: wrap lets items break onto new lines when they don't fit; nowrap (the default) forces one line and lets items shrink.

Grid#

Q7. What is the fr unit? Build a basic grid#

CSS
.layout {
  display: grid;
  grid-template-columns: 200px 1fr 2fr;  /* fixed, then 1:2 of the remaining space */
  grid-template-rows: auto 1fr auto;
  gap: 16px;
}

.three-equal { grid-template-columns: repeat(3, 1fr); }

Answer: fr (fraction) represents a share of the free space left after fixed tracks and gaps. repeat(3, 1fr) makes three equal columns. Note that 1fr has a minimum of auto; use minmax(0, 1fr) if long content shouldn't stretch a column.

Q8. How do grid-template-areas work? Build a page layout#

CSS
.page {
  display: grid;
  grid-template-areas:
    "header  header"
    "sidebar main"
    "footer  footer";
  grid-template-columns: 240px 1fr;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}
.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main    { grid-area: main; }
.footer  { grid-area: footer; }

@media (max-width: 768px) {
  .page {
    grid-template-areas: "header" "main" "sidebar" "footer";
    grid-template-columns: 1fr;
  }
}

Answer: named areas let you draw the layout as ASCII art. Rearranging the layout for mobile is just a new grid-template-areas in a media query, with no HTML changes. Keep the DOM order logical for screen readers, though, because visual reordering doesn't change reading order.

Q9. How do you place items with line numbers and span?#

CSS
.featured {
  grid-column: 1 / 3;      /* from line 1 to line 3 = spans 2 columns */
  grid-row: span 2;        /* spans 2 rows from its position */
}
.full-width {
  grid-column: 1 / -1;     /* -1 is the last line: full width */
}

Grid lines are numbered from 1, and negative numbers count from the end. Items can also overlap by placing them in the same cells and using z-index.

Q10. What is the difference between auto-fit and auto-fill? Build a responsive card grid#

CSS
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 20px;
}

Answer: this creates as many columns of at least 240px as fit, with no media queries. The difference shows when there are few items:

  • auto-fill keeps creating empty column tracks, so items stay at their minimum-ish width.
  • auto-fit collapses empty tracks to zero, so existing items stretch to fill the row.

Use minmax(min(240px, 100%), 1fr) to avoid overflow on very narrow screens.

Practical layouts#

CSS
/* Flexbox */
body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  margin: 0;
}
main { flex: 1; }   /* pushes the footer down on short pages */

/* Grid */
body {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}

On mobile, consider min-height: 100dvh, which accounts for the browser's collapsing address bar.

Q12. Name common Flexbox and Grid pitfalls#

  • Overflowing flex items: add min-width: 0 to flex children with long text.
  • Images stretching: align-items: stretch is the default; set align-items: flex-start or give images object-fit.
  • Visual vs DOM order: order, flex-direction: row-reverse and grid placement change only the visual order, not tab or screen-reader order.
  • margin-left: auto trick: in a flex row, it pushes an item (like a logout button) to the far end.
  • space-between on the last row: wrapped items spread unevenly; use Grid or gap instead.
  • Percentages with gap: width: 33.33% plus gaps overflows; use flex: 1 or Grid fr units.

Quick revision#

NeedCSS
Center anythingdisplay: grid; place-items: center;
Fill remaining spaceflex: 1
Fixed-width sidebarflex: 0 0 250px or grid-template-columns: 250px 1fr
Spacing between itemsgap
Responsive cards, no media queryrepeat(auto-fit, minmax(240px, 1fr))
Full-width grid itemgrid-column: 1 / -1
Push item to the endmargin-left: auto

Next: responsive web design interview questions and HTML and CSS interview questions.

Key takeaways#

  • Flexbox is one-dimensional and content-first; Grid is two-dimensional and layout-first.
  • justify-content works on the main axis, align-items on the cross axis.
  • flex: 1 means grow, shrink and start from zero, so siblings share space equally.
  • Grid's fr, grid-template-areas and repeat(auto-fit, minmax()) build responsive layouts with little code.
  • Watch for flex overflow (min-width: 0) and keep DOM order logical for accessibility.