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
05

CSS · Lesson 5 of 12 · 3 min

Flexbox Fundamentals

Interviewers use Flexbox to test your core CSS layout skills. This is about knowing the container properties cold and explaining the "why" behind your choices.

The Mental Model: Main Axis vs. Cross Axis

Everything in Flexbox happens along two axes. Get this right, and the properties make sense. Get it wrong, and you're just guessing.

The flex-direction property sets the main axis. This is the primary direction your items will flow.

  • flex-direction: row (default): Main axis is horizontal (left-to-right).
  • flex-direction: column: Main axis is vertical (top-to-bottom).

The cross axis is always perpendicular to the main axis.

This is the entire concept. justify-content works on the main axis. align-items works on the cross axis. If you change flex-direction from row to column, the behavior of these two properties effectively swaps.

How to Distribute Space on the Main Axis (justify-content)

This property aligns items along the main axis. You use it to handle the spacing between and around your items.

  • flex-start (default): Packs items at the beginning.
  • flex-end: Packs items at the end.
  • center: Packs items in the middle.
  • space-between: First item at the start, last at the end, with even space between the others.
  • space-around: Equal space around each item. The space at the ends is half the space between items.
  • space-evenly: Equal space everywhere—between items and at the ends.

The interview question: "What's the difference between space-around and space-evenly?" space-around puts equal space around each item. This means the gap between two items is twice the gap at the container's edge. space-evenly makes all gaps, including the ones at the edges, equal. For most "even spacing" designs, you want space-evenly.

How to Align Items on the Cross Axis (align-items)

This property aligns items along the cross axis. It controls where items sit vertically in a row, or horizontally in a column.

  • stretch (default): Items stretch to fill the container's height/width on the cross axis.
  • flex-start: Aligns items to the start of the cross axis.
  • center: Centers items on the cross axis.
  • flex-end: Aligns items to the end of the cross axis.
  • baseline: Aligns items by their text baseline. Useful for aligning form inputs and labels of different font sizes.

The trap: A candidate tries to use align-items: center but nothing happens. Why? Because the flex container has no height. If the container is only as tall as its content, there is no extra space on the cross axis to align anything within. You must give the container a height (e.g., min-height: 100vh) for vertical alignment to be visible.

The most common use case for Flexbox in interviews is centering. To perfectly center a child element, you apply this to the parent container:

.parent {
  display: flex;
  justify-content: center; /* Centers on the main (horizontal) axis */
  align-items: center;     /* Centers on the cross (vertical) axis */
  min-height: 100vh;       /* Give it space to center within */
}

Handling Overflow with flex-wrap

By default, flex items try to fit on one line (flex-wrap: nowrap). They will shrink or overflow if they don't fit.

flex-wrap: wrap is what makes Flexbox responsive. When items run out of space on the main axis, they wrap onto a new line. This is fundamental for creating galleries or tag lists that reflow on smaller screens.

This property is also a key differentiator from CSS Grid. Flexbox is for laying out items in a single dimension—either a row or a column. flex-wrap allows that single dimension to wrap, but you don't get the same two-dimensional control as Grid. Use Flexbox for components like navbars, form rows, and card headers. Use Grid for the overall page layout. Knowing when to use which is a sign of seniority.

A Practical Example

Here is a single container demonstrating the core properties. You can change the commented-out values in a browser's dev tools to see how the layout changes instantly. This is a common pattern for a component header.

<div class="container">
  <div class="item">Logo</div>
  <div class="item">Nav Link 1</div>
  <div class="item">Nav Link 2</div>
  <div class="item">Profile</div>
</div>
.container {
  display: flex;
  padding: 1rem;
  border: 1px solid #ccc;
  min-height: 100px;
  gap: 1rem; /* Modern way to add space between items */

  /* --- CHANGE THESE VALUES --- */

  /* Main Axis Direction */
  flex-direction: row; /* or column */

  /* Main Axis Alignment */
  justify-content: space-between; /* or flex-start, center, space-evenly */

  /* Cross Axis Alignment */
  align-items: center; /* or flex-start, stretch */

  /* Wrapping */
  flex-wrap: wrap; /* or nowrap */
}

.item {
  padding: 1rem;
  background-color: #f0f0f0;
  border: 1px solid #999;
}

Try it

Create a full-page container (height: 100vh, width: 100vw). Use Flexbox to place a single 200x100px div exactly in the center, both horizontally and vertically. This is the "hello world" of Flexbox and a very common screening task.

Next: In "Advanced Flexbox & Real-world Patterns", we will cover how to control individual flex items with flex-grow, flex-shrink, and flex-basis.

Lock it in — apply this lesson under interview conditions.

Try an AI mock interview
Previous

Units, Values & Responsive Basics

Next

Advanced Flexbox & Real-world Patterns