CSS - Flexbox and Grid

Code SubtleCode Subtle
6 min read

Part 1: Understanding Flexbox

Introduction to Flexbox

Flexbox (Flexible Box Layout) is a one-dimensional layout model designed to provide a more efficient way to distribute space and align items in a container. It's particularly powerful when working with rows or columns of items, even when their sizes are unknown or dynamic.

The Flex Container

Basic Setup

Every flexbox layout begins with a container element that establishes the flex context:

.container {
  display: flex; /* or inline-flex */
}

The choice between flex and inline-flex determines how the container itself behaves:

  • flex: Container becomes a block-level element

  • inline-flex: Container becomes inline-level element

Flex Direction

The flex-direction property establishes the main axis, determining how flex items are placed in the container:

.container {
  flex-direction: row | row-reverse | column | column-reverse;
}

Understanding each value:

  • row: Items flow left to right (default)

  • row-reverse: Items flow right to left

  • column: Items flow top to bottom

  • column-reverse: Items flow bottom to top

Flex Wrap

Controls whether items should wrap onto multiple lines:

.container {
  flex-wrap: nowrap | wrap | wrap-reverse;
}

Key behaviors:

  • nowrap: All items try to fit on one line (default)

  • wrap: Items wrap onto multiple lines, top to bottom

  • wrap-reverse: Items wrap onto multiple lines, bottom to top

Justify Content

Controls alignment along the main axis:

.container {
  justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
}

Detailed breakdown:

  • flex-start: Items packed toward start

  • flex-end: Items packed toward end

  • center: Items centered on line

  • space-between: Items evenly distributed with first item at start, last at end

  • space-around: Items evenly distributed with equal space around them

  • space-evenly: Items distributed with equal space between each item

Align Items

Controls alignment along the cross axis:

.container {
  align-items: stretch | flex-start | flex-end | center | baseline;
}

Understanding alignment options:

  • stretch: Items stretch to fill container (default)

  • flex-start: Items aligned to start of cross axis

  • flex-end: Items aligned to end of cross axis

  • center: Items centered on cross axis

  • baseline: Items aligned by their baselines

Align Content

Controls alignment of multiple lines of content:

.container {
  align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}

Flex Items

Flex Grow

Determines how items grow when extra space is available:

.item {
  flex-grow: 0; /* default */
  flex-grow: 1; /* will grow */
  flex-grow: 2; /* will grow twice as much as items with flex-grow: 1 */
}

Flex Shrink

Controls how items shrink when space is limited:

.item {
  flex-shrink: 1; /* default, will shrink */
  flex-shrink: 0; /* won't shrink */
  flex-shrink: 2; /* will shrink twice as fast */
}

Flex Basis

Sets the initial main size of an item:

.item {
  flex-basis: auto; /* default */
  flex-basis: 0;    /* start from zero */
  flex-basis: 200px; /* start from specific size */
}

The Flex Shorthand

Combines grow, shrink, and basis:

.item {
  flex: 0 1 auto; /* default */
  flex: 1;        /* 1 1 0% */
  flex: auto;     /* 1 1 auto */
  flex: none;     /* 0 0 auto */
}

Common Flexbox Patterns

Navigation Bar

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
}

.nav-links {
  display: flex;
  gap: 20px;
}

Card Layout

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

.card {
  flex: 0 1 calc(33.333% - 20px);
  min-width: 250px;
}

Part 2: Mastering CSS Grid

Introduction to Grid

CSS Grid is a two-dimensional layout system that excels at creating complex layouts with rows and columns simultaneously.

Grid Container Properties

Display Grid

Establishes a grid formatting context:

.container {
  display: grid | inline-grid;
}

Grid Template Columns and Rows

Defines the columns and rows of the grid:

.container {
  /* Fixed sizes */
  grid-template-columns: 200px 200px 200px;

  /* Flexible sizes */
  grid-template-columns: 1fr 2fr 1fr;

  /* Mixed units */
  grid-template-columns: minmax(100px, 1fr) 2fr 1fr;

  /* Repeat patterns */
  grid-template-columns: repeat(3, 1fr);

  /* Auto-fit and auto-fill */
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}

Grid Template Areas

Creates named grid areas for easy placement:

.container {
  grid-template-areas: 
    "header header header"
    "sidebar main main"
    "footer footer footer";
}

.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }

Grid Gap

Sets spacing between grid items:

.container {
  gap: 20px;                    /* Both row and column gap */
  row-gap: 20px;               /* Only row gap */
  column-gap: 20px;            /* Only column gap */
}

Justify Items and Align Items

Controls item alignment within their grid cells:

.container {
  justify-items: start | end | center | stretch;
  align-items: start | end | center | stretch;
}

Grid Item Properties

Grid Column and Row

Controls item placement and spanning:

.item {
  /* Specific line numbers */
  grid-column: 1 / 3;
  grid-row: 2 / 4;

  /* Spanning */
  grid-column: span 2;
  grid-row: span 3;

  /* Named lines */
  grid-column: main-start / main-end;
}

Grid Area

Shorthand for placement using named areas or lines:

.item {
  grid-area: header;  /* Using named area */

  /* row-start / column-start / row-end / column-end */
  grid-area: 1 / 1 / 2 / 3;
}

Advanced Grid Concepts

Auto-Flow Patterns

Controls how auto-placed items flow:

.container {
  grid-auto-flow: row | column | row dense | column dense;
}

Implicit Grid

Handles items placed outside explicit grid:

.container {
  grid-auto-rows: minmax(100px, auto);
  grid-auto-columns: 1fr;
}

Common Grid Patterns

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  padding: 20px;
}

.gallery-item {
  aspect-ratio: 16/9;
  overflow: hidden;
}

Dashboard Layout

.dashboard {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header header header"
    "sidebar main main main"
    "footer footer footer footer";
  min-height: 100vh;
  gap: 20px;
}

Combining Flexbox and Grid

Sometimes the best layouts use both systems together:

.page-layout {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}

.navigation {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
}

.content-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
  padding: 20px;
}

.card {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

This code creates a full-page layout with:

  • A flexible header using Flexbox for navigation

  • A responsive grid of cards in the main content area

  • Cards that use Flexbox for internal layout

Best Practices

  1. Grid for Layout, Flexbox for Components

    • Use Grid for overall page layout

    • Use Flexbox for component-level arrangements

  2. Responsive Design

    • Use minmax() with auto-fit/auto-fill for responsive grids

    • Combine with media queries for complex layouts

  3. Performance Considerations

    • Avoid deep nesting of grid/flex containers

    • Use appropriate units (fr vs fixed units)

    • Consider reflow impact when using auto-sizing

Would you like me to create visual demonstrations for any of these concepts or provide more specific examples for particular use cases?

12
Subscribe to my newsletter

Read articles from Code Subtle directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Code Subtle
Code Subtle

At Code Subtle, we empower aspiring web developers through personalized mentorship and engaging learning resources. Our community bridges the gap between theory and practice, guiding students from basics to advanced concepts. We offer expert mentorship and write interactive, user-friendly articles on all aspects of web development. Join us to learn, grow, and build your future in tech!