Learning flexbox visually

Authors
  • avatar
    Twitter
    Adam Ladrach
Last updated
 

CSS flexbox revolutionized web layout design when it was introduced, offering a more intuitive way to create responsive layouts compared to traditional CSS positioning methods. But while flexbox solves many layout challenges, it still comes with its own learning curve. Let’s explore what it is, how it works, and visualize it in your head so that you can learn it once and for all.

Image showing flexbox layouts in a navigation

Understanding flexbox fundamentals

Flexbox, or the Flexible Box Layout Module, provides a more efficient way to arrange, align, and distribute space among items in a container. Unlike the old CSS layout strategies that rely heavily on floats and positioning, flexbox is specifically designed for one-dimensional layouts to act as a row or a column.

Key flexbox concepts

To work with flexbox, you need to understand the following main concepts:

  1. Flex Container: The parent element that has display: flex applied to it
  2. Flex Items: The direct children of the flex container
  3. Flex Axis: The axis along which flex items are laid out. It can be horizontal (row) or vertical (column).
  4. Flex Grow/Shrink: The ability of flex items to grow or shrink to fill the available space.
  5. Flex Basis: The initial size of a flex item before any adjustments are made.
  6. Flex Wrap: The ability of flex items to wrap to the next line when there is no space left in the container.
Diagram illustrating the Flex Container and Flex Items relationship, with labels pointing to each component and showing the main axis and cross axis

Let’s look at the basic syntax:

.container {
  display: flex; /* or display: inline-flex */
  flex-direction: row; /* default: row | row-reverse | column | column-reverse */
  flex-wrap: nowrap; /* default: nowrap | wrap | wrap-reverse */
  justify-content: flex-start; /* default: flex-start | flex-end | center | space-between | space-around | space-evenly */
  align-items: stretch; /* default: stretch | flex-start | flex-end | center | baseline */
  align-content: normal; /* default: normal | flex-start | flex-end | center | space-between | space-around | space-evenly | stretch */
  gap: 10px; /* gap between items */
}

.item {
  order: 0; /* default: 0 */
  flex-grow: 0; /* default: 0 */
  flex-shrink: 1; /* default: 1 */
  flex-basis: auto; /* default: auto */
  /* shorthand */
  flex: 0 1 auto; /* grow shrink basis */
  align-self: auto; /* default: auto | flex-start | flex-end | center | baseline | stretch */
}

Common flexbox Use Cases

1. Centering content

Centering elements both horizontally and vertically has always been a challenge in CSS. With flexbox, it’s simple:

.container {
  display: flex;
  justify-content: center; /* horizontal centering */
  align-items: center; /* vertical centering */
  height: 300px; /* container needs a height for vertical centering to work */
}
Result

2. Navigation Bars

Creating flexible navigation bars that work across different screen sizes:

nav {
  display: flex;
  flex-direction: row; /* row is the default value for flex-direction, you can omit it */
  justify-content: space-between; /* space items evenly */
}

/* For mobile layouts */
@media (max-width: 768px) {
  nav {
    flex-direction: column;
  }
}

.nav-links {
  display: flex;
  gap: 20px; /* space between nav items */
}
Responsive navigation bar created with flexbox showing desktop (horizontal) and mobile (vertical) layouts side by side

3. Card Layouts

Flexible card layouts that adjust based on available space:

.card-container {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

.card {
  flex: 1 1 300px; /* grow, shrink, basis */
  padding: 20px;
  border: 1px solid #ccc;
}
Card grid layout showing how cards reflow with flex-wrap

The challenges of creating layouts with flexbox by hand

Despite its power, styling with flexbox manually comes with challenges:

  1. Complex property combinations: The interaction between properties like flex-grow, flex-shrink, and flex-basis can be difficult to visualize.
  2. Nested flexbox layouts: Managing multiple levels of flex containers can become confusing.
  3. Visual feedback: There’s no immediate visual feedback when writing CSS code manually.
  4. Trial and error: Getting layouts just right often involves a lot of tweaking and refreshing.
Developer frustration - centering a text using CSS

Let’s face it—while flexbox is powerful, there’s still a significant learning curve and potential for frustration, especially for those who aren’t CSS experts or prefer visual design approaches.

Visualize your learning

What if you could harness all the power of Flexbox without memorizing properties or writing CSS by hand? This is where Plasmic comes in.

Plasmic is a visual builder that lets you create responsive layouts using Flexbox principles—but with an intuitive drag-and-drop interface instead of manual coding.

Plasmic's interface showing the visual flexbox controls with properties like justify-content, align-items, and flex-direction represented as visual toggles

How no-code tools simplify the process

  1. Visual Flex Controls: Instead of typing CSS properties, you can adjust flex settings visually and see changes in real-time.

  2. Instant Preview: No need to save, refresh, and check—you see exactly how your layout responds as you make changes.

  3. Responsive Testing: Test how your flexbox layouts behave across different screen sizes directly in the editor.

  4. No CSS Knowledge Required: Design visually while Plasmic generates the optimal flexbox CSS behind the scenes.

  5. Code Integration: For developers, no-code tools can generate clean code that integrates with your codebase.

GIF or video thumbnail showing a quick workflow in Plasmic: selecting a container, enabling flex layout, and adjusting properties with instant visual feedback

Example: Creating a Responsive Card Layout

In manual CSS, you might write:

.card-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 20px;
}

.card {
  min-width: 0;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

In Plasmic you simply:

  1. Drag in a horizontal stack component Drag in a horizontal stack component
  2. Add your card components inside Add your card components inside
  3. Adjust spacing, wrapping, and alignment visually Adjust spacing, wrapping, and alignment visually
  4. Ensure the design looks great on all breakpoints, adjust if needed—just click the “Preview” button to see how it looks on different screen sizes. Mobile preview

No CSS needed—just visual design with immediate feedback.

Frequently Asked Questions about flexbox

What is the difference between flexbox and grid?

Flexbox is designed for one-dimensional layouts (either a row or a column), while CSS Grid is designed for two-dimensional layouts (rows and columns together). Flexbox works from the content out, while Grid works from the layout in. Plasmic supports both layout methods with its visual interface.

Is flexbox supported in all browsers?

Yes, flexbox is supported in all modern browsers. However, there are some minor inconsistencies in implementation that can cause headaches. Plasmic handles these cross-browser inconsistencies automatically.

How do I center something vertically with flexbox?

To center an item vertically with flexbox, set the parent container to display: flex and align-items: center. In Plasmic, this is as simple as clicking the center alignment button in the visual interface.

Can I nest flexbox containers?

Yes, you can nest flexbox containers to create complex layouts. However, this can become difficult to manage in code. Plasmic’s visual interface makes nested flexbox containers easy to visualize and edit.

How can I learn more about flexbox?

While there are many resources online for learning flexbox, the fastest way to master it is through visual experimentation. Try Plasmic’s free visual builder to see flexbox in action without writing code.

Follow @plasmicapp on Twitter for the latest updates.