CSS Grid vs Flexbox
CSS Grid vs Flexbox Interview with follow-up questions
1. What are the main differences between CSS Grid and Flexbox?
CSS Grid and Flexbox solve different layout problems and are best understood as complementary, not competing.
Dimensionality Flexbox is a one-dimensional layout model: it arranges items along a single axis (either a row or a column). Grid is two-dimensional: it controls both rows and columns simultaneously, letting you place items at specific intersections.
Layout direction With Flexbox, the layout is driven by the content — items flow along the main axis and wrap as needed. With Grid, the layout is driven by an explicit track definition — you define the grid structure first, then place items into it.
Alignment
Both support alignment on main and cross axes. Grid adds justify-items, align-items, justify-self, and align-self for individual cell placement, as well as place-content for aligning the whole grid within its container.
Item placement
Flexbox items follow document order by default. Grid allows explicit placement with grid-column and grid-row, so you can reorder items visually without changing the DOM.
Typical use cases
- Flexbox: navigation bars, button groups, card content layout, centering a single item, any linear chain of items that may wrap.
- Grid: overall page layouts, image galleries, dashboard panels, any design that requires alignment across both rows and columns simultaneously.
Browser support Both are fully supported in all modern browsers. The old talking point that Grid has weaker browser support is obsolete — the relevant question is now subgrid support (grid children inheriting the parent grid's tracks), which has shipped in all modern browsers as of 2023.
Subgrid
subgrid allows a nested grid item to use its parent's row or column tracks, enabling perfect alignment across nested components. This is a significant addition that Grid has and Flexbox has no equivalent for.
Practical rule of thumb: if you can describe the layout as a straight line of items, reach for Flexbox. If you are thinking about rows and columns at the same time, reach for Grid. In practice most UIs use both — Grid for the macro page structure, Flexbox for component internals.
Follow-up 1
Can you give an example of when you would use Grid over Flexbox?
Grid is particularly useful when you need to create complex grid structures, such as magazine-style layouts or image galleries. It allows you to define both rows and columns, and control the placement of items within the grid. For example, if you have a design that requires a grid of images with captions, where the images need to be a certain size and the captions need to align with the bottom of each image, Grid would be a great choice for this layout.
Follow-up 2
What are the browser compatibility issues with Grid and Flexbox?
Flexbox has better browser support compared to Grid, especially in older browsers. Flexbox is supported by all major browsers, including Chrome, Firefox, Safari, and Edge. However, some older versions of Internet Explorer have limited support for Flexbox.
Grid, on the other hand, has more limited support. It is supported by most modern browsers, including Chrome, Firefox, Safari, and Edge. However, older versions of Internet Explorer do not support Grid. To ensure compatibility, it is recommended to use feature detection and provide fallbacks or alternative layouts for browsers that do not support Grid.
Follow-up 3
How do Grid and Flexbox handle responsiveness?
Flexbox is a great choice for creating responsive designs that need to adapt to different screen sizes. It allows you to easily change the layout and order of items based on the available space. By using flex properties like flex-grow, flex-shrink, and flex-basis, you can control how items grow and shrink to fit the container.
Grid also provides powerful tools for creating responsive layouts. You can use media queries to change the grid structure based on the screen size. Additionally, Grid has properties like grid-template-areas and grid-template-columns that allow you to define different layouts for different screen sizes.
Overall, both Flexbox and Grid provide flexible options for handling responsiveness, but the choice between them depends on the specific requirements of your layout.
2. How would you use CSS Grid for layout design?
CSS Grid is declared on a container element. Its direct children automatically become grid items.
Basic setup
.layout {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: auto 1fr auto;
gap: 1.5rem;
}
fr (fraction unit) distributes available space proportionally after fixed and content-based tracks are sized. gap (shorthand for row-gap and column-gap) replaces the old grid-gap.
Named template areas
One of Grid's most readable features is named areas:
.layout {
display: grid;
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: 60px 1fr 40px;
}
header { grid-area: header; }
aside { grid-area: sidebar; }
main { grid-area: main; }
footer { grid-area: footer; }
Explicit item placement
Items can span tracks or be placed at specific lines:
.featured {
grid-column: 1 / 3; /* span from line 1 to line 3 */
grid-row: 1 / span 2; /* start at row 1, span 2 rows */
}
Responsive grids without media queries
repeat() with auto-fill or auto-fit and minmax() creates intrinsically responsive grids:
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
gap: 1rem;
}
This adds columns when space allows and wraps to fewer columns on narrow viewports — no @media queries needed.
Subgrid
When a grid item is itself a grid container, subgrid lets it inherit the parent's tracks so nested items align to the outer grid:
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}
Alignment
justify-items / align-items align all items within their cells. justify-content / align-content align the entire grid track structure within the container. Individual items use justify-self / align-self.
Gotcha: grid-gap is the deprecated property name — use gap. Also, fr units do not shrink below content size, so combined with min-content or minmax() they handle most sizing edge cases.
Follow-up 1
Can you explain the concept of 'grid lines' in CSS Grid?
In CSS Grid, grid lines are the horizontal and vertical lines that make up the grid. They are used to define the boundaries of the grid cells. The grid lines are numbered starting from 1 and can be referred to using positive integers or negative integers. Positive integers refer to the lines starting from the start edge of the grid, while negative integers refer to the lines starting from the end edge of the grid. For example, grid-column: 1 / 3; will span from the first grid line to the third grid line.
Follow-up 2
How would you create a responsive layout using CSS Grid?
To create a responsive layout using CSS Grid, you can use media queries to change the grid template columns and rows based on the screen size. For example, you can define a grid template with a fixed number of columns and rows for larger screens, and then change the grid template to have a different number of columns and rows for smaller screens. You can also use the auto-fit or auto-fill keywords in the grid-template-columns property to automatically adjust the number of columns based on the available space. Additionally, you can use the minmax() function to set a range of sizes for the grid items, allowing them to grow or shrink based on the available space.
Follow-up 3
What are the advantages of using CSS Grid for layout design?
CSS Grid offers several advantages for layout design:
Flexibility: CSS Grid allows you to create complex and flexible layouts with ease. You can easily define the size and position of grid items, and change the layout dynamically using media queries.
Grid Lines and Tracks: CSS Grid provides a powerful system for defining grid lines and tracks, which allows for precise control over the layout. You can easily create both fixed and flexible grid layouts.
Responsive Design: CSS Grid makes it easy to create responsive layouts by using media queries and adjusting the grid template columns and rows based on the screen size.
Accessibility: CSS Grid is accessible by default, as it follows the natural flow of the document. It also allows for easy reordering of grid items for different screen sizes, improving the accessibility of the layout.
Browser Support: CSS Grid is supported by all modern browsers, making it a reliable choice for layout design.
3. How would you use Flexbox for layout design?
Flexbox is declared on a container element. Its direct children become flex items and are arranged along the main axis.
Setting up the container
.nav {
display: flex;
flex-direction: row; /* or column, row-reverse, column-reverse */
justify-content: space-between; /* main axis alignment */
align-items: center; /* cross axis alignment */
flex-wrap: wrap; /* allow items to wrap to new lines */
gap: 1rem; /* space between items */
}
gap works in Flexbox (all modern browsers) and is preferred over margins on flex items because it does not add space at the start or end of the container.
Key container properties
flex-direction: sets the main axis.rowis the default (horizontal);columnstacks items vertically.justify-content: distributes items along the main axis —flex-start,flex-end,center,space-between,space-around,space-evenly.align-items: aligns items on the cross axis —stretch(default),flex-start,flex-end,center,baseline.flex-wrap:nowrap(default) keeps items on one line;wraplets them flow to the next line.align-content: aligns wrapped lines when there is extra space on the cross axis (only meaningful withflex-wrap: wrap).
Key item properties
.item {
flex: 1 1 200px; /* flex-grow flex-shrink flex-basis shorthand */
align-self: flex-end;
order: -1; /* visual reordering without changing DOM */
}
Common patterns
Centering an element — the most common interview example:
.container {
display: flex;
justify-content: center;
align-items: center;
}
Pushing the last item to the far end:
.nav-item:last-child { margin-left: auto; }
Gotchas interviewers probe:
align-contentonly works when items wrap; it has no effect on a single-line flex container.flex-basistakes precedence overwidth(orheightin a column layout) when set to something other thanauto.orderaffects visual rendering only, not tab order or screen reader order — accessibility concern.flex: 1is shorthand forflex: 1 1 0, which setsflex-basisto0, notauto. This is a frequent source of unexpected sizing when items have varying content.
Follow-up 1
Can you explain the 'flex' property in Flexbox?
The 'flex' property is a shorthand property in Flexbox that combines three individual flex properties: flex-grow, flex-shrink, and flex-basis. It allows you to specify how a flex item should grow, shrink, and size itself within a flex container.
The 'flex' property takes three values:
- flex-grow: specifies the flex grow factor, which determines how much the flex item should grow relative to other flex items. By default, it is set to 0, which means the flex item will not grow.
- flex-shrink: specifies the flex shrink factor, which determines how much the flex item should shrink relative to other flex items. By default, it is set to 1, which means the flex item will shrink proportionally to other flex items.
- flex-basis: specifies the initial size of the flex item before any remaining space is distributed. By default, it is set to 'auto', which means the flex item will be sized based on its content.
Here's an example of using the 'flex' property:
.flex-item {
flex: 1 0 200px;
}
In this example, the flex item will grow with a flex grow factor of 1, not shrink with a flex shrink factor of 0, and have an initial size of 200 pixels.
Follow-up 2
How would you align items in a container using Flexbox?
To align items in a container using Flexbox, you can use the 'align-items' property. The 'align-items' property specifies how flex items are aligned along the cross axis of the flex container.
Here are some values that can be used with the 'align-items' property:
- flex-start: aligns flex items at the start of the cross axis.
- flex-end: aligns flex items at the end of the cross axis.
- center: aligns flex items at the center of the cross axis.
- baseline: aligns flex items such that their baselines align.
- stretch: stretches flex items to fill the container along the cross axis.
Here's an example of using the 'align-items' property:
.flex-container {
display: flex;
align-items: center;
}
In this example, the flex items within the flex container will be vertically centered along the cross axis.
Follow-up 3
What are the advantages of using Flexbox for layout design?
Flexbox offers several advantages for layout design:
Easy and intuitive: Flexbox provides a simple and intuitive way to create flexible and responsive layouts. It allows you to easily control the alignment, ordering, and sizing of elements within a container.
One-dimensional layout: Flexbox is designed for one-dimensional layouts, making it ideal for arranging elements in a row or column. It simplifies the process of creating responsive designs that adapt to different screen sizes.
Flexible and dynamic: Flexbox allows elements to grow and shrink based on available space, making it easy to create layouts that adapt to different content sizes. It also provides powerful alignment and spacing options, giving you fine-grained control over the positioning of elements.
Cross-browser compatibility: Flexbox is supported by all modern browsers, including Internet Explorer 11 and above. This makes it a reliable choice for building cross-browser compatible layouts.
Overall, Flexbox is a versatile and powerful tool for layout design, offering a wide range of features and benefits.
4. Can you explain the concept of 'flex-grow', 'flex-shrink' and 'flex-basis' in Flexbox?
These three properties compose the flex shorthand and together determine how a flex item sizes itself within the available space.
flex-basis
Sets the item's initial main size before the browser distributes free space. It acts like width in a row layout and height in a column layout, but with higher precedence than width/height when set to something other than auto. Common values:
- A length or percentage (
200px,30%) — item starts at that size. auto(default when usingflex-basisexplicitly) — uses the item's ownwidth/heightor content size.0— item starts with no intrinsic size; all space comes fromflex-grow.
flex-grow
A unitless ratio that determines how much of the remaining positive free space an item absorbs. When multiple items have flex-grow, the extra space is divided proportionally:
0(default) — item does not grow beyond itsflex-basis.1— item absorbs its share of leftover space.2next to aflex-grow: 1sibling means it takes twice as much free space.
flex-shrink
A unitless ratio that controls how much an item shrinks when there is not enough space. The shrink amount is weighted by flex-basis, so larger items shrink more in absolute terms even with equal flex-shrink values:
1(default) — item shrinks proportionally.0— item refuses to shrink below itsflex-basis. Useful for fixed-size sidebars or icons.
The flex shorthand
Always prefer the shorthand — it sets sensible defaults for omitted values:
flex: 1→1 1 0— items share all available space equally by ignoring content sizeflex: auto→1 1 auto— items grow/shrink but respect their content/widthflex: none→0 0 auto— rigid: no growing, no shrinking
Common gotcha: flex: 1 sets flex-basis to 0, not auto. This means items ignore their content size when distributing space, which is usually what you want for equal-width columns but can cause unexpected collapses on text-heavy items. Use flex: 1 1 auto when you want items to share remaining space while still respecting their minimum content size.
Follow-up 1
How do these properties affect the layout?
These properties affect the layout of flex items within a flex container in the following ways:
'flex-grow' determines how much the flex item can grow when there is extra space available in the container. If one item has a higher 'flex-grow' value than the others, it will grow more and take up more space.
'flex-shrink' determines how much the flex item can shrink when there is not enough space available in the container. If one item has a higher 'flex-shrink' value than the others, it will shrink more and take up less space.
'flex-basis' sets the initial size of the flex item. If all items have a 'flex-basis' value of 'auto', they will be sized based on their content. If one item has a specific 'flex-basis' value, it will be sized accordingly and any remaining space will be distributed among the other items.
Follow-up 2
Can you give an example of when you would use these properties?
Here's an example where these properties can be useful:
Let's say you have a flex container with three flex items. The first item has a 'flex-grow' value of 1, the second item has a 'flex-grow' value of 2, and the third item has a 'flex-grow' value of 3. This means that when there is extra space available in the container, the third item will grow the most, followed by the second item, and then the first item.
If there is not enough space in the container, the items will shrink proportionally based on their 'flex-shrink' values. So if the first item has a 'flex-shrink' value of 1, the second item has a 'flex-shrink' value of 2, and the third item has a 'flex-shrink' value of 3, the third item will shrink the most, followed by the second item, and then the first item.
The 'flex-basis' property can be used to set the initial size of the items before any remaining space is distributed. For example, if the first item has a 'flex-basis' value of '100px', it will be initially sized to 100 pixels, and any remaining space will be distributed among the other items.
Follow-up 3
What are the default values for these properties?
The default values for these properties are:
- 'flex-grow': 0
- 'flex-shrink': 1
- 'flex-basis': 'auto'
This means that by default, flex items will not grow, will shrink proportionally, and will be sized based on their content.
5. How do you decide when to use CSS Grid and when to use Flexbox?
The honest answer is: use both, at the right level of your layout.
The primary decision axis: one dimension vs. two
Reach for Flexbox when you are arranging items in a single line — a navigation bar, a row of buttons, a stack of cards in a column, centering a single element. The content flow drives the layout.
Reach for Grid when you need to control both rows and columns simultaneously — a page layout with header/sidebar/main/footer, an image gallery that must align in both directions, a dashboard with widgets at specific intersections. The defined structure drives the layout.
Practical patterns that guide the choice
- Macro page layout (shell, regions) → Grid
- Component internals (button + icon, label + input) → Flexbox
- Card grids that reflow by number of columns → Grid with
auto-fill+minmax - A toolbar with items spaced apart → Flexbox with
justify-content: space-between - Aligning items across rows and columns in a table-like structure → Grid (especially with
subgrid) - Single-axis wrapping content → Flexbox with
flex-wrap
They compose well
A Grid controls the page structure; each grid area contains a Flex container that manages its own content. This is the most common real-world pattern — do not feel forced to pick one globally.
Browser support is a non-issue in 2026
The old concern about Grid browser support is obsolete. Both are fully supported. subgrid (grid children inheriting parent tracks) is also now supported across all modern browsers.
Interviewer follow-up: "When would you use Grid for something that seems like a Flex job?"
A common example is a card list where cards in the same row need equal-height sections (e.g., a title, a body, and a footer that all align across cards). Flexbox alone cannot achieve this without JavaScript. Grid with subgrid or align-items: stretch solves it cleanly.
Follow-up 1
Can you give an example of a layout that would be easier to implement with Grid?
Sure! One example of a layout that would be easier to implement with CSS Grid is a grid-based gallery. Let's say you have a set of images that you want to display in a grid, with a fixed number of columns and rows. With CSS Grid, you can easily define the number of columns and rows, and the grid will automatically adjust to accommodate the images. Here's an example:
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 10px;
}
.gallery img {
width: 100%;
height: auto;
}
Follow-up 2
Can you give an example of a layout that would be easier to implement with Flexbox?
Certainly! One example of a layout that would be easier to implement with Flexbox is a horizontal navigation menu. Let's say you have a set of navigation links that you want to display in a horizontal row. With Flexbox, you can easily align the links horizontally and distribute the space evenly. Here's an example:
.nav {
display: flex;
justify-content: space-between;
}
.nav a {
margin-right: 10px;
}
Follow-up 3
Are there any performance differences between Grid and Flexbox?
In terms of performance, both CSS Grid and Flexbox are highly optimized and generally perform well. However, there are some differences to consider:
Rendering: CSS Grid requires the browser to calculate the layout of the entire grid, which can be more computationally expensive compared to Flexbox, especially for large grids. Flexbox, on the other hand, calculates the layout of each flex item individually.
Browser support: CSS Grid has better browser support compared to Flexbox, especially in older browsers. If you need to support older browsers, you may need to use Flexbox as a fallback.
Complexity: CSS Grid is more complex to understand and use compared to Flexbox. It has a steeper learning curve and may require more code to achieve certain layouts.
In general, the performance differences between Grid and Flexbox are minimal for most use cases. It's important to consider the specific requirements of your layout and the browser support needed before choosing between the two.
Live mock interview
Mock interview: CSS Grid vs Flexbox
- Read your scene and goals
- Talk it out; goals tick off live
- Get a score and stronger lines
Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.