Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14
Chapter 3

The Box Model, Flexbox & Grid

Every element is a box — master the model, then lay out with Flexbox (1D) and Grid (2D).


The CSS Box Model

Every element is a box made of four layers, from outside in:

  1. Margin — space outside the border; pushes other elements away.
  2. Border — the visible frame around the element.
  3. Padding — space between the content and the border.
  4. Content — the actual text, image or other content.

Box Model in CSS — box-sizing Best Practice

  • The default box-sizing is content-box — width counts only the content.
  • Setting box-sizing: border-box includes padding + border in the width.
  • 2025 best practice: apply border-box globally in every project.
/* 2025 global reset — every project */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.box {
  width: 300px;
  padding: 20px;            /* inner space */
  border: 2px solid #00B4D8;
  margin: 1rem auto;        /* centered */
}
/* With border-box: total width stays 300px
   Without: 300px + 40px + 4px = 344px */

/* 2025: logical properties */
.card {
  padding-block: 1rem;      /* top + bottom */
  padding-inline: 1.5rem;   /* left + right */
  margin-inline: auto;      /* center */
}

CSS Flexbox — Modern 1D Layout

PropertyValues / Purpose
display: flexTurns a container into a flex container
flex-directionrow (default) | column | row-reverse | column-reverse
justify-contentAligns items along the main axis: flex-start | center | flex-end | space-between | space-evenly
align-itemsAligns items along the cross axis: stretch | center | flex-start | flex-end | baseline
gapSpace between flex items (no margins needed!)
flex: 1Item grows to fill available space
flex-wrap: wrapItems flow to the next row when needed
/* Flex container */
.nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}

/* Flex item sizing */
.sidebar { flex: 0 0 260px; }   /* fixed */
.main    { flex: 1; }           /* fills the rest */

/* Centering — the easiest way in 2025 */
.hero {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

/* Wrapping card grid */
.card-row { display: flex; flex-wrap: wrap; gap: 1.5rem; }
.card { flex: 1 1 250px; }      /* grow, shrink, min 250px */

CSS Grid — Modern 2D Layout

FeaturePurpose
display: gridCreates a 2D grid container
grid-template-columns / -rowsDefine tracks
fr unitFractional share of available space — 1fr 2fr is 33% / 67%
gapRow and column gaps
grid-areaName areas for semantic placement
repeat(auto-fill, minmax(250px, 1fr))Responsive grid with NO media queries
subgrid2025: nested items align to parent grid tracks
/* Basic grid */
.layout {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: auto 1fr auto;
  gap: 1rem;
}

/* Named areas — like a blueprint */
.page {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
}
.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main    { grid-area: main; }

/* Responsive grid — NO media queries! */
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 1.5rem;
}

Activity — complete the code

Complete the code correctly to mark this lesson as complete automatically.

Complete the global reset and the flex container.

*, *::before, *::after {
box-sizing: ;
}
 
.nav {
display: ;
justify-content: ;
gap: 1rem;
}