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.
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.
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?#
| Flexbox | Grid | |
|---|---|---|
| Dimensions | One (row or column) | Two (rows and columns) |
| Approach | Content-first: items decide their size | Layout-first: the container defines tracks |
| Best for | Navbars, button groups, toolbars, centering, aligning items in a card | Page layouts, card galleries, dashboards, forms with aligned labels |
| Overlapping items | Not natively | Yes, 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.
.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?#
| Property | Aligns | Common values |
|---|---|---|
justify-content | Items along the main axis | flex-start, center, space-between, space-around, space-evenly |
align-items | Items on the cross axis within a line | stretch (default), center, flex-start, baseline |
align-content | Multiple lines (only with flex-wrap: wrap) | flex-start, center, space-between |
align-self | One item, overriding align-items | center, 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 (autouses 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).
.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.
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?#
/* 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?#
.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#
.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#
.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?#
.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#
.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-fillkeeps creating empty column tracks, so items stay at their minimum-ish width.auto-fitcollapses 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#
Q11. How do you build a sticky footer layout?#
/* 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: 0to flex children with long text. - Images stretching:
align-items: stretchis the default; setalign-items: flex-startor give imagesobject-fit. - Visual vs DOM order:
order,flex-direction: row-reverseand grid placement change only the visual order, not tab or screen-reader order. margin-left: autotrick: in a flex row, it pushes an item (like a logout button) to the far end.space-betweenon the last row: wrapped items spread unevenly; use Grid orgapinstead.- Percentages with gap:
width: 33.33%plus gaps overflows; useflex: 1or Gridfrunits.
Quick revision#
| Need | CSS |
|---|---|
| Center anything | display: grid; place-items: center; |
| Fill remaining space | flex: 1 |
| Fixed-width sidebar | flex: 0 0 250px or grid-template-columns: 250px 1fr |
| Spacing between items | gap |
| Responsive cards, no media query | repeat(auto-fit, minmax(240px, 1fr)) |
| Full-width grid item | grid-column: 1 / -1 |
| Push item to the end | margin-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-contentworks on the main axis,align-itemson the cross axis.flex: 1means grow, shrink and start from zero, so siblings share space equally.- Grid's
fr,grid-template-areasandrepeat(auto-fit, minmax())build responsive layouts with little code. - Watch for flex overflow (
min-width: 0) and keep DOM order logical for accessibility.