Skip to main content

Command Palette

Search for a command to run...

Understanding CSS layouts: Flexbox vs Grid

Updated
2 min read
Understanding CSS layouts: Flexbox vs Grid

css offers two powerful layout systems: Flexbox and Grid. While both help create responsive layouts, they serve different purposes and shine in different scenarios.

Flexbox: One Dimensional layout

Flexbox excels at distributing space and aligning content within a container along a single direction(either row or column)

Key concepts:

  1. Main Axis: Primary direction of flex layout (horizontal/vertical)

  2. Cross Axis: Perpendicular to main axis

  3. Flex container: Parent element with display: flex

  4. flex items: children of flex conatiner.

Let’s learn it with example.

<style>
      .container {
        display: flex;
        min-height: 400px;
        background-color: bisque;
      }

      .sidebar {
        background-color: lightgreen;
        padding: 20px;
      }

      .main {
        flex: 1;
        background: lightred;
        padding: 20px;
      }
    </style>

<div class="container">
      <div class="sidebar">Sidebar Content</div>
      <div class="main">Main Content</div>
    </div>

Grid: Two Dimensional Layout

Grid provides a two-dimensional layout system, working with both rows and columns simultaneously.

Key Concepts:

  1. Grid Container: parent element with display: grid

  2. Grid items: Children of grid container

  3. Grid lines: Dividing lines making up the grid structure

  4. Grid tracks: Rows and columns

  5. Grid cells: Individual units of the. grid

Lets try to build same above example with grid.

<style>
      .container {
        display: grid;
        grid-template-columns: 100px 400px 150px 100px;
        min-height: 400px;
        background-color: bisque;
      }

      .sidebar {
        background-color: lightgreen;
        padding: 20px;
      }

      .main {
        background: lightred;
        padding: 20px;
      }
      .third {
        background: lightblue;
        padding: 20px;
      }
      .forth {
        background: lightcoral;
        padding: 20px;
      }
    </style> 

<div class="container">
      <div class="sidebar">Sidebar Content</div>
      <div class="main">Main Content</div>
      <div class="third">third column</div>
      <div class="forth">Forth Column</div>
</div>

When to use Each

Choose Flexbox when:

  1. Creating navigation bars

  2. Aligning elements in a single row/column

  3. Building component-level layouts

  4. Needing flexible spacing between elements

  5. Working with dynamic or unknown content sizes

Choose Grid when:

  1. Creating full-page layouts

  2. working with both rows and column simultaneously

  3. Building image galleries

  4. needing precise control over item placement

  5. Creating complex grid-based designs

Best Practices:

  1. Flexbox:

    Use Flex-wrap for responsive designs

    Set Flex-basis instead of width/height
    Consider using gap instead of margins
    Use shorthand flex property when possible

  2. Grid:

    Use fr units for flexible tracks
    Name grid areas for clearer layouts
    Implement responsive designs with minmax()
    Use auto-fit/auto-fill for dynamic grids

Conclusion:

While both Flexbox and Grid are powerful layout tools, they serve different purposes. Flexbox is ideal for one-dimensional layout component-level design, while Grid excels at two-dimensional layouts and page-level structure. Often the best approach is using them together, with Grid handling the overall page layout and Flexbox managing component-level alignment.

5 views

More from this blog

Ajay Patel

116 posts