Top 50 CSS Flexbox Interview Questions and Practical Coding Challenges
Quick Answer: Flexbox interviews test whether you can reason about a layout, not whether you can recite property names. Work through any question in four steps: find the flex container, name the main and cross axes, check the item sizes (flex-basis, flex-grow, flex-shrink), and check whether there is free space to align or distribute. This article gives you 50 questions with strong short answers and six practical coding challenges with solutions.
S1. Why Do Interviews Focus on Flexbox?
Imagine you are interviewing for a frontend role. The interviewer shares a small piece of broken layout code and asks, "Why is this not centred?"
Almost every website uses Flexbox, so interviewers use it to see how you think. Can you find the container? Do you know which axis a property follows? Can you explain what happens to the free space?
Would a memorised definition be enough? Let's first see what happens when it is not.
S2. The Problem: Answering from Memory
Mental model = the reasoning you use to predict what the browser will do.
Imagine a candidate has memorised one line: "align-items: center centres items vertically." The interviewer shows this code and asks why the box is not in the middle of the page.
Example 1: A layout that looks correct but does not centre
This example is valid CSS, but it does not produce the expected result.
<!-- HTML -->
<div class="stage">
<div class="box">Hello</div>
</div>
/* CSS */
.stage {
display: flex;
justify-content: center;
align-items: center;
}
.box {
padding: 24px;
background: #E2E8F0;
}
Expected output
┌────────────────────────────────────────┐ │ [ Hello ] │ <- centred horizontally └────────────────────────────────────────┘ The page below the box is empty. The box is centred left to right, but not top to bottom. The container is only as tall as the box, so there is nothing to centre within.
The memorised answer fails here. The code is correct, but the container has no extra height on the cross axis.
What goes wrong?
- Memorised rules: "Vertical centring" is true only when there is vertical space.
- No reasoning steps: The candidate never checks the container height.
- Follow-up questions fail: The answer breaks when the direction changes to column.
- Weak explanations: The candidate cannot say why, only what.
- Poor confidence: Small changes in the question cause hesitation.
Now think about fifty different questions. Memorising fifty separate answers is much harder than learning one way to reason.
Remember: The problem is not the candidate's knowledge of properties. The real problem is answering from memory instead of reasoning from the layout.
Animation 1: How memorised answers fail
One memorised line, many questions
The red highlights show where a memorised answer stops working.
The animation highlights the question types where a memorised line breaks down.
S3. The Solution: Reason with a Four-Step Method
Flexbox gives us a simpler way to think.
Four-step method = a fixed checklist you run on every Flexbox question, whether it is a definition, a bug or a coding challenge.
- Step 1, container: Which element has
display: flex, and which elements are its direct children? - Step 2, axes: What is
flex-direction? That tells you the main axis and the cross axis. - Step 3, sizes: What are the
flex-basis,flex-growandflex-shrinkvalues, and the minimum sizes? - Step 4, free space: Is there free space on the axis the property controls?
Run the method on the broken example. Step 4 finds the problem.
Example 2: The same layout, fixed with reasoning
Assumption: This example uses standard CSS supported by all modern browsers.
.stage {
display: flex; /* step 1: .stage is the container */
justify-content: center; /* step 2: row, so the main axis is horizontal */
align-items: center; /* step 2: the cross axis is vertical */
min-height: 100vh; /* step 4: give the cross axis free space */
}
Expected output
┌────────────────────────────────────────┐ │ │ │ │ │ [ Hello ] │ <- centred both ways │ │ │ │ └────────────────────────────────────────┘
Notice the difference. One reasoning step found the missing free space, and one line fixed it.
Animation 2: The reasoning design
One method, every question
Each question type is answered with the same checklist.
The animation highlights each question type and the checklist step it relies on.
Compare both approaches
| Answering from memory | Answering with the method |
|---|---|
| Recites one fixed line per property. | Explains what the property follows. |
| Breaks when the direction changes. | Adapts to row and column. |
| Cannot explain why code fails. | Finds the failing step. |
| Needs fifty separate answers. | Needs one checklist. |
| Sounds uncertain. | Sounds clear and confident. |
The idea to remember
- Container = the parent with
display: flex. Its direct children are the flex items. - Axes = the main axis follows
flex-direction, and the cross axis is perpendicular to it. - Sizes = the starting size comes from
flex-basis, then grow and shrink adjust it. - Free space = alignment and distribution only show an effect when there is room to use.
- Explain, then show = say the reasoning first, then write the code.
S4. Step-by-Step Code Breakdown: The 50 Questions
The questions below are grouped by topic. Each answer is short enough to say in an interview, and each follows the four-step method. Read the group, cover the answer column, and try to answer aloud.
Group 1: Flexbox basics (questions 1 to 8)
| # | Question | Strong answer |
|---|---|---|
| 1 | What is Flexbox? | A one-dimensional layout model that arranges items in a row or a column and distributes space among them. |
| 2 | How do you create a flex container? | Set display: flex (block-level) or display: inline-flex (inline-level) on the parent. |
| 3 | What is the difference between flex and inline-flex? | Both arrange children the same way. flex makes the container block-level and full width. inline-flex makes it inline-level, so it fits its content. |
| 4 | Which elements become flex items? | Only the direct children of the container. Contiguous text directly inside it is wrapped in an anonymous flex item. |
| 5 | Do grandchildren become flex items? | No. They stay in normal flow unless their own parent is also a flex container. |
| 6 | What are the default values of a new flex container? | flex-direction: row, flex-wrap: nowrap, and normal alignment, which behaves like flex-start for justify-content and like stretch for align-items. |
| 7 | Is Flexbox one-dimensional or two-dimensional? | One-dimensional. It lays items out along one axis at a time. Wrapped lines are each laid out on their own. |
| 8 | Do float and vertical-align work on flex items? |
No. They have no effect on flex items, so old float rules should be removed. |
Group 2: Axes and direction (questions 9 to 14)
| # | Question | Strong answer |
|---|---|---|
| 9 | What is the main axis? | The primary direction in which items are placed. flex-direction sets it. |
| 10 | What is the cross axis? | The direction perpendicular to the main axis. |
| 11 | What values does flex-direction accept? |
row, row-reverse, column and column-reverse. |
| 12 | Does row-reverse change the HTML order? |
No. It changes only the visual order. Keyboard and screen reader order still follow the HTML, which can hurt accessibility. |
| 13 | What happens to the axes when the direction is column? |
The main axis becomes vertical and the cross axis becomes horizontal, in the usual writing mode. |
| 14 | How does the writing direction affect a row? | A row follows the inline direction of the writing mode. In a right-to-left language, a row runs from right to left. |
Group 3: Wrapping (questions 15 to 19)
| # | Question | Strong answer |
|---|---|---|
| 15 | What does flex-wrap do? |
It decides whether items stay on one line or move onto new lines when space runs out. |
| 16 | What is the default of flex-wrap? |
nowrap. All items stay on one line and shrink or overflow. |
| 17 | What is the difference between wrap and wrap-reverse? |
With wrap-reverse, cross-start and cross-end swap, so lines are added in the opposite direction. |
| 18 | What is flex-flow? |
A shorthand for flex-direction and flex-wrap, for example flex-flow: row wrap. |
| 19 | Why does the last row of wrapped items sometimes look wider? | Each line is laid out on its own. Items that can grow fill the free space of a short last line. |
Group 4: Alignment (questions 20 to 28)
| # | Question | Strong answer |
|---|---|---|
| 20 | What does justify-content do? |
It positions items along the main axis and distributes the free space. |
| 21 | What does align-items do? |
It aligns items along the cross axis, inside each line. |
| 22 | What does align-content do? |
It distributes the lines of a wrapped container on the cross axis. It has no effect on a single-line container. |
| 23 | What is the difference between space-around and space-evenly? |
With space-around, the edge gaps are half the gaps between items. With space-evenly, all gaps are equal. |
| 24 | How do you centre an element both ways? | On the parent: display: flex, justify-content: center, align-items: center, and give the parent a height. |
| 25 | Why is align-items: center not working? |
Usually there is no extra cross-axis space, because the container has no height. Or the direction is column, so the cross axis is horizontal. |
| 26 | What does align-self do? |
It overrides align-items for one item. |
| 27 | What is baseline alignment? | align-items: baseline lines up the text baselines of the items, which suits mixed font sizes. |
| 28 | What does an auto margin do in a flex container? | It absorbs free space. Auto margins are applied before justify-content, so margin-left: auto pushes an item to the end. |
Group 5: Item sizing (questions 29 to 37)
| # | Question | Strong answer |
|---|---|---|
| 29 | What is flex-grow? |
The share of positive free space an item takes. The default is 0. |
| 30 | What is flex-shrink? |
The share of the shortage an item gives up when items are too large. The default is 1. |
| 31 | What is flex-basis? |
The starting main size of an item. The default is auto, which uses the width or height, or the content size. |
| 32 | What does flex: 1 mean? |
1 1 0%. The item grows and shrinks from a zero starting size, so equal values give equal sizes. |
| 33 | What does flex: auto mean? |
1 1 auto. Items grow and shrink from their content size. |
| 34 | What does flex: none mean? |
0 0 auto. The item keeps its size and neither grows nor shrinks. |
| 35 | What is the difference between flex-basis and width? |
In a row, a flex-basis that is not auto takes priority over width for the starting size. |
| 36 | How is a shortage shared between items? | In proportion to each item's shrink value times its flex basis, so larger items give up more pixels. |
| 37 | Why does long text overflow even though flex-shrink is 1? |
An item will not shrink below its minimum content size by default. Setting min-width: 0 removes that limit. |
Group 6: Spacing and order (questions 38 to 42)
| # | Question | Strong answer |
|---|---|---|
| 38 | What does gap do? |
It adds space between items and between lines. row-gap and column-gap set each direction. It adds no space at the outer edges. |
| 39 | Why use gap instead of margins? |
It is set once on the container, only appears between items, and needs no special rule for the first or last item. |
| 40 | What does order do? |
It changes the visual order of items. The default is 0. Like reverse values, it does not change the HTML or keyboard order. |
| 41 | How do you push the last item to the far end? | Give that item margin-left: auto (in a row). |
| 42 | How do you put a logo on the left and links on the right? | Use justify-content: space-between with two children, or margin-left: auto on the links group. |
Group 7: Real-world patterns (questions 43 to 50)
| # | Question | Strong answer |
|---|---|---|
| 43 | What is the difference between Flexbox and Grid? | Flexbox is one-dimensional and content-first. Grid is two-dimensional and layout-first. |
| 44 | Can you use Flexbox and Grid together? | Yes. A common pattern is Grid for the page structure and Flexbox for components inside each area. |
| 45 | How do you build a sticky footer? | Make the page wrapper display: flex; flex-direction: column; min-height: 100vh, and give the main area flex: 1. |
| 46 | How do you make cards the same height? | Keep the default align-items: stretch and do not set a fixed height on the cards. |
| 47 | How do you keep a button at the bottom of a card? | Make the card a column flex container and give the button margin-top: auto. |
| 48 | How do you make a responsive card row without media queries? | Use flex-wrap: wrap with flex: 1 1 220px on the cards. |
| 49 | Why does an image get squashed in a flex row? | It has the default flex-shrink: 1. Set flex-shrink: 0 on it. |
| 50 | How do you debug a flex layout? | Use the browser DevTools flex overlay, then check the container, the axes, the item sizes and the free space. |
Tip: After each answer, add one short example in code. An answer with a three-line example is stronger than a long definition.
S5. Practical Coding Challenges
Interviewers often ask you to write a small layout live. Each challenge below states the task, gives a solution, and shows the expected result. Try it yourself before you read the solution.
Challenge 1: Centre a card in the viewport
Task: Centre one card horizontally and vertically on the page.
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
The card sits in the exact centre of the screen. min-height gives the cross axis the free space it needs.
Challenge 2: Logo on the left, links on the right
Task: Build a navigation bar with a logo at the start and three links at the end.
/* HTML: header > .logo + nav (with three links) */
header {
display: flex;
align-items: center;
padding: 0 24px;
height: 64px;
}
header nav {
margin-left: auto; /* pushes the links to the end */
display: flex;
gap: 20px;
}
[ Logo ] [ Home ] [ Courses ] [ Contact ] The auto margin takes all the free space between the logo and the links.
Challenge 3: A sticky footer
Task: Keep the footer at the bottom of the screen even when the page has little content.
/* HTML: .page > header + main + footer */
.page {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.page main {
flex: 1; /* takes all remaining height */
}
[ header ] [ main ........................ ] (grows to fill the screen) [ footer ] (always at the bottom)
Challenge 4: Equal-height cards with the button at the bottom
Task: Make a wrapping row of cards that have equal heights, with each button aligned at the bottom.
.cards {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card {
flex: 1 1 220px;
display: flex;
flex-direction: column;
}
.card button {
margin-top: auto; /* pushes the button to the bottom */
}
[ Card A ] [ Card B ] [ Card C ] [ short text ] [ longer ] [ short text ] [ ] [ text here ] [ ] [ [Button] ] [ [Button] ] [ [Button] ] <- aligned at the bottom Items in a line stretch to the same height by default.
Challenge 5: A search row
Task: Make an input that fills the available width, next to a button that keeps its size.
.search {
display: flex;
gap: 8px;
}
.search input { flex: 1; min-width: 0; }
.search button { flex: none; }
[ input .................................. ] [ Search ] The input absorbs the free space. The button keeps its own size.
Challenge 6: An avatar and a truncated name
Task: Show a round avatar next to a name. A long name must end with an ellipsis instead of squashing the avatar.
.person {
display: flex;
align-items: center;
gap: 12px;
width: 240px;
}
.person img {
width: 48px;
height: 48px;
border-radius: 50%;
flex-shrink: 0;
}
.person span {
min-width: 0;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
( avatar ) Alexandria Montgome... The avatar stays round (flex-shrink: 0). The name shrinks to the remaining width and ends with an ellipsis.
Animation 3: Which property solves which challenge
A focused challenge map
Each challenge relies on one main idea from the series.
The animation traces each group of challenges to the idea it practises.
S6. Common Mistakes and Best Practices
Mistake 1: Reciting "justify-content is horizontal"
Wrong: Say that justify-content is horizontal and align-items is vertical.
Why it fails: The statement is true only for a row. The interviewer can change the direction and the answer breaks.
Better: Say that justify-content follows the main axis and align-items follows the cross axis.
Rule: Tie each property to an axis, not to a screen direction.
Mistake 2: Writing code without stating the plan
Wrong: Start typing properties without explaining the layout.
Why it fails: The interviewer cannot see your reasoning, so a small error looks like a lack of understanding.
Better: Say the container, the direction and the sizing plan first, then write the code.
Rule: Explain, then show.
Mistake 3: Forcing Flexbox into a two-dimensional layout
Wrong: Use wrapping and fixed widths to fake aligned rows and columns.
Why it fails: Wrapped lines are sized independently, so columns do not line up.
Better: Say that Grid fits this case, and explain why.
Rule: Choosing the right tool is part of a strong answer.
Mistake 4: Ignoring accessibility of visual reordering
Wrong: Use order or the reverse values and say nothing about the side effects.
Why it fails: The HTML and keyboard order stay the same, so the visual order and the focus order can disagree.
Better: Mention that the source order should still make sense.
Rule: Visual order is not reading order.
Mistake 5: Forgetting height and minimum size in your answers
Wrong: Give a centring or truncation answer without a container height or min-width: 0.
Why it fails: The code looks right but does not work in the browser.
Better: Always check the free space and the minimum size.
Rule: Run step 3 and step 4 before you say "done".
S7. How This Comes Up in Interviews
Question 1: How do you approach a live layout challenge?
What the interviewer wants: They want to see structured thinking, not speed.
Strong answer:
"I first describe the layout in words. I identify the container and its direct children, decide the direction, then decide how each item should size. Then I write the code and check for free space on the axis I am aligning."
Question 2: How do you explain flex: 1 simply?
Strong answer:
"It means grow 1, shrink 1 and a starting size of 0%. Every item starts from zero, so the whole container is shared according to the grow values. Equal values give equal sizes."
Question 3: How do you answer when asked to compare Flexbox and Grid?
Strong answer:
"Flexbox arranges items along one axis and lets the content influence sizes. Grid defines rows and columns first and places items in them. I use Grid for the page structure and Flexbox for components such as navigation bars and card contents."
Question 4: What do you do when you do not know the answer?
Strong answer:
"I reason from what I know. I name the container and the axes, say what I expect to happen, and explain how I would verify it in the browser DevTools."
What should different experience levels explain?
- Junior developer: Answer questions 1 to 28 clearly and complete challenges 1 to 3.
- Senior developer: Answer questions 29 to 50 with reasoning, and complete all six challenges, including truncation and equal-height cards.
- Technical architect: Explain when to choose Flexbox or Grid, which sizing shorthands belong in a design system, and the accessibility effects of reordering.
S8. Hands-On Practice
Beginner: Answer aloud
Read questions 1 to 28. Cover the answer column, say the answer aloud, then compare. Mark the ones you missed.
Expected result: You can answer each question in one or two sentences, using the words main axis and cross axis.
Intermediate: Rebuild the challenges
Build challenges 1 to 6 from scratch in the browser without looking at the solutions. Use the four-step method for each.
Hint: If a layout does not behave, check the free space and the minimum size first.
Advanced: Run a mock interview
Ask a friend to pick ten random questions from the list. Answer each one with a short explanation and a three-line code example. Then ask them to change the direction to column and repeat a question.
Expected result: Your answers stay correct when the direction changes, because you reasoned from the axes instead of from memory.
S9. Next Steps and Community
Previous article: Build a Complete Responsive Web Page Using CSS Flexbox.
Next article: None. This is the final article in the CSS Flexbox series.
Congratulations on reaching the end of the series. You have learned how to create a flex container, how the main axis and cross axis work, how to wrap and align items, how items grow, shrink and get their starting size, how to space them, and how to put it all together in a complete page.
Keep practising. Rebuild the challenges, break them on purpose, and fix them with the four-step method. Share your solutions with the Upgradix community, and help other developers prepare for their interviews.
Find the container, name the axes, check the sizes, and look for the free space.
Upgradix by Shrash Studio
Learn Technology. Build Your Future.
Quality Technology Education. Completely Free.