Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
CSS Layouts: Flexbox, Grid & Responsive
  1. 1.Introduction to CSS & Selectors
  2. 2.The CSS Box Model & Basic Styling
  3. 3.Display Property & Positioning Fundamentals
  4. 4.Units, Values & Responsive Basics
  5. 5.Flexbox Fundamentals
  6. 6.Advanced Flexbox & Real-world Patterns
  7. 7.CSS Grid Fundamentals
  8. 8.Advanced CSS Grid & Area Layouts
  9. 9.Responsive Design Strategies & Mobile First
  10. 10.CSS Transitions, Transforms & Animations
  11. 11.Advanced CSS Concepts & Performance
  12. 12.Layout Techniques for Complex UIs & Interview Prep
CSS Layouts: Flexbox, Grid & Responsive
06

CSS · Lesson 6 of 12 · 4 min

Advanced Flexbox & Real-world Patterns

Interviewers use these patterns to see if you can build layouts that don't break.

After this, you'll know how to build the most common responsive layouts interviewers ask for. You'll also know the specific trade-offs and traps associated with advanced flex properties, which is what separates a senior-level answer from a junior one.

How to Build a Responsive Grid

The most common real-world flexbox task is a grid of items that wraps gracefully on smaller screens. Think product listings, photo galleries, or tag clouds. You need three things: wrapping, flexible sizing, and spacing.

First, enable wrapping. The default is nowrap, which is why flex items shrink or overflow instead of moving to a new line.

.container {
  display: flex;
  flex-wrap: wrap;
}

Second, control how items size themselves. The flex shorthand is key here. A common pattern is flex: 1 1 300px.

  • flex-grow: 1: Allows items to grow and fill any leftover space on their line.
  • flex-shrink: 1: Allows items to shrink if there isn't enough space for their ideal size.
  • flex-basis: 300px: The ideal starting size before growing or shrinking. The browser will fit as many 300px items as it can on one line, then wrap the rest.

Third, add spacing with gap. It's the modern, correct way to space flex items.

The trap: Using margin for spacing. This forces you to write extra code to handle unwanted space on the edges of the container, often with negative margin hacks or complex :child selectors. gap only applies between items, which is what you almost always want. An interviewer who sees you reach for gap first will trust your judgment.

Here is the complete pattern for a responsive card grid.

<div class="card-container">
    <div class="card"><h3>Card 1</h3><p>Content for card 1.</p></div>
    <div class="card"><h3>Card 2</h3><p>Content for card 2 is a bit longer.</p></div>
    <div class="card"><h3>Card 3</h3><p>Card 3 content.</p></div>
    <div class="card"><h3>Card 4</h3><p>Another card with some text.</p></div>
    <div class="card"><h3>Card 5</h3><p>This is card 5.</p></div>
</div>
.card-container {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    padding: 1rem;
    background-color: #f0f0f0;
}

.card {
    /* Grow, shrink, with a 300px ideal basis */
    flex: 1 1 300px;
    box-sizing: border-box;
    background-color: white;
    border: 1px solid #ddd;
    border-radius: 8px;
    padding: 1.5rem;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

How to Control Multi-line Layouts

Once you have multiple lines of flex items, align-content controls how those lines are distributed vertically within the container. This property does nothing if you only have one line of items.

The trap: Confusing align-content with align-items. This is a frequent interview mistake.

  • align-items works on the cross-axis for items within a single line.
  • align-content works on the cross-axis for the lines themselves.

Imagine a tall container with two rows of cards. align-content: space-between; will push the first row to the top of the container and the second row to the bottom. align-items: center; would only center the cards vertically within their own row.

How to Create a Sticky Footer

This is a classic layout problem you will be asked to solve. The footer must stick to the bottom of the viewport on pages with little content but get pushed down naturally on pages with long content.

The solution uses a column-direction flex container that fills the viewport.

  1. Make the container (e.g., body or a main wrapper <div>) a flex container: display: flex; and flex-direction: column;.
  2. Ensure the container is at least as tall as the viewport: min-height: 100vh;.
  3. Tell the main content area to take up all available space: flex-grow: 1;.

That's it. The main content expands to fill any empty space, pushing the footer to the bottom. This is a canonical pattern; know it by heart.

When to Change Visual Order (and When Not To)

The order property lets you change the visual sequence of flex items without touching the DOM. By default, all items have order: 0. Items with lower order values appear first.

A legitimate use case is reordering a sidebar and main content for mobile vs. desktop screens. You might have the sidebar first in the DOM for accessibility, but use order to position it visually on the right side on large screens.

The trap: order only changes the visual display. The DOM order, which dictates tab order and screen reader sequence, is unaffected. Overusing order can create a confusing and inaccessible user experience where a keyboard user tabs through elements in an order that doesn't match what they see.

In an interview, if you suggest using order, you must also state the accessibility trade-off. Failing to do so suggests you don't consider all users, which is a red flag.

Why Flexbox is for One-Dimensional Layouts

This is the core conceptual difference between Flexbox and Grid. Flexbox is designed for laying out content in a single dimension—either a row or a column.

Even when you use flex-wrap, you are not creating a true two-dimensional grid. You are creating a series of independent, one-dimensional rows. The items in the second row have no alignment relationship with the items in the first row.

This is why Flexbox is perfect for component-level layouts like navigation bars, form elements, or card lists. For page-level layouts where you need to align items across both rows and columns, you need a two-dimensional system. That system is CSS Grid.

Try it

Implement a responsive header. On desktop, it should have a logo on the left, navigation links in the center, and a "Sign In" button on the right. On screens smaller than 768px, it should stack them vertically and centered. Use nested flex containers and gap.

Next: CSS Grid Fundamentals, where we move from one dimension to two.

Lock it in — apply this lesson under interview conditions.

Try an AI mock interview
Previous

Flexbox Fundamentals

Next

CSS Grid Fundamentals