MarzleyTech Learn

Home / Learn / Web design & UI/UX / Spacing, grids and layout: spacing scales, the box model, grids, Flexbox and CSS Grid layouts, and common page patterns

Spacing, grids and layout: spacing scales, the box model, grids, Flexbox and CSS Grid layouts, and common page patterns

Two websites can use the same colours and fonts, yet one looks polished and the other messy. The difference is usually spacing and layout: consistent gaps, aligned edges, sensible grouping and a clear structure that adapts from phone to desktop. This unit teaches the spacing systems and grids designers use, how they translate into CSS (box model, Flexbox, Grid), and the common layout patterns you'll build for real clients.

Why spacing matters

  • Grouping: space shows what belongs together (proximity principle).
  • Rhythm: consistent gaps make pages feel calm and professional.
  • Readability: breathing room around text and buttons improves scanning and tapping.
  • Speed: a spacing system means fewer decisions while designing and coding.

Spacing scales

Pick a base unit (commonly 4px or 8px) and use multiples only:

TokenValueTypical use
--space-14pxIcon to text
--space-28pxTight groups (label to input)
--space-312pxInside small components
--space-416pxCard padding on phones, paragraph spacing
--space-624pxBetween components
--space-832pxCard padding on desktop
--space-1248pxBetween sections on phones
--space-1664pxBetween sections on desktop

Rule of thumb: space inside a group < space between groups < space between sections.

The box model

Every element is a box:

margin (outside space)
  border
    padding (inside space)
      content
HTML · runs live in the interactive lesson
<style>
  .box { margin: 16px; border: 3px solid #0b1b35; padding: 20px; background: #fef3c7; font-family: system-ui; }
  .box span { background: #fff; }
  * { box-sizing: border-box; }
</style>
<div class="box"><span>Content</span> sits inside padding (yellow), inside the border (navy), with margin outside.</div>

Always set box-sizing: border-box so width includes padding and border (much easier layouts).

Layout grids

Designers use column grids:

  • Columns: 12 on desktop (divides into halves, thirds, quarters), 8 on tablets, 4 on phones.
  • Gutters: gaps between columns (e.g. 16–24px).
  • Margins: space at the screen edges (e.g. 16px on phones).
  • Container: content max-width (e.g. 1100–1280px) centred on large screens, so lines don't stretch across huge monitors.

Flexbox: one-dimensional layouts

Use Flexbox for rows or columns of items: navigation bars, button groups, cards in a row, aligning icon + text.

HTML · runs live in the interactive lesson
<style>
  .nav { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 12px 16px; background: #0b1b35; font-family: system-ui; border-radius: 10px; }
  .nav a { color: #fff; text-decoration: none; }
  .nav .links { display: flex; gap: 16px; flex-wrap: wrap; }
  .nav .cta { background: #ffb800; color: #0b1b35; padding: 8px 14px; border-radius: 999px; font-weight: 700; }
</style>
<nav class="nav">
  <a href="#"><strong>Marzley</strong></a>
  <div class="links"><a href="#">Services</a><a href="#">Work</a><a href="#">Learn</a></div>
  <a class="cta" href="#">Get a quote</a>
</nav>

Key properties: display:flex, justify-content (main axis), align-items (cross axis), gap, flex-wrap, flex: 1 (grow to fill).

CSS Grid: two-dimensional layouts

Use Grid for rows and columns together: card grids, page layouts, galleries.

A responsive card grid with no media queries:

HTML · runs live in the interactive lesson
<style>
  .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; font-family: system-ui; }
  .card { border: 1px solid #e2e8f0; border-radius: 12px; padding: 16px; background: #fff; }
  .card h4 { margin: 0 0 6px; color: #0b1b35; }
  .card p { margin: 0; color: #475569; font-size: 14px; }
</style>
<div class="grid">
  <div class="card"><h4>Websites</h4><p>Fast, mobile-first sites.</p></div>
  <div class="card"><h4>M-Pesa</h4><p>STK push checkout.</p></div>
  <div class="card"><h4>SEO</h4><p>Get found on Google.</p></div>
  <div class="card"><h4>Training</h4><p>Learn digital skills.</p></div>
</div>

repeat(auto-fit, minmax(180px, 1fr)) fits as many columns as possible, each at least 180px wide; resize the preview to see cards wrap.

A page layout with named areas:

HTML · runs live in the interactive lesson
<style>
  .page { display: grid; gap: 12px; font-family: system-ui;
          grid-template-areas: "header" "main" "sidebar" "footer"; }
  @media (min-width: 700px) {
    .page { grid-template-columns: 1fr 240px; grid-template-areas: "header header" "main sidebar" "footer footer"; }
  }
  .page > * { padding: 14px; border-radius: 10px; background: #f1f5f9; }
  header { grid-area: header; background: #0b1b35 !important; color: #fff; }
  main { grid-area: main; min-height: 120px; }
  aside { grid-area: sidebar; }
  footer { grid-area: footer; }
</style>
<div class="page"><header>Header</header><main>Main content</main><aside>Sidebar</aside><footer>Footer</footer></div>
Use Flexbox forUse Grid for
Navigation bars, toolbarsOverall page layout
Aligning items in a rowCard and image grids
Centring one thingComplex sections with rows and columns

They work together: a Grid of cards, with Flexbox inside each card.

Containers

CSS
.container {
  width: min(100% - 32px, 1120px);   /* 16px side margins on phones, max 1120px wide */
  margin-inline: auto;               /* centre it */
}

Full-width coloured sections can still keep their content inside a container.

Common page patterns

PatternDescription
Header/navbarLogo left, links, CTA right; collapses to a menu button on phones
HeroBig headline, short text, CTA, image or illustration
Feature/service cards3–4 cards in a grid with icon, title, text
Split sectionText one side, image the other; stacks on phones
TestimonialsQuotes with names and photos
Pricing tablePlans side by side with a highlighted recommended plan
FAQAccordion questions
CTA bandFull-width section with one action
FooterContact details, links, social icons, legal pages

Responsive layout tips

  • Design mobile first: single column, then add columns as space grows (next lessons).
  • Use gap instead of margins between flex/grid items.
  • Avoid fixed widths in pixels for content; use %, fr, minmax(), max-width.
  • Check for horizontal scrolling on phones (a common bug from wide images or fixed widths).
  • Increase section spacing on larger screens.
Think about it: A services page has cards with different padding (10px, 25px, 18px), random gaps between sections, and text touching the screen edges on phones. How would you fix it systematically?Show answer

Adopt an 8px spacing scale as CSS variables; give all cards the same padding (e.g. 16px phones, 24–32px desktop); use consistent section spacing (e.g. 48px/64px); put content in a container with 16px side margins on phones; lay out cards with CSS Grid and gap; and set box-sizing: border-box.

Summary

  • Consistent spacing groups content and creates calm rhythm; use a 4/8px spacing scale.
  • The box model: content, padding, border, margin; use box-sizing: border-box.
  • Grids use columns, gutters, margins and a max-width container.
  • Flexbox handles one-dimensional rows/columns; CSS Grid handles two-dimensional layouts, including auto-fit card grids and named areas.
  • Build common patterns (header, hero, cards, split sections, footer) mobile-first with flexible units.

Check yourself

  1. What base unit do many spacing systems use? (a number in px)

    Show answer

    8

  2. Which box-sizing value makes width include padding and border?

    Show answer

    border-box

  3. Which CSS layout system is best for two-dimensional layouts?

    Show answer

    Grid

  4. Which property adds space between flex or grid items?

    Show answer

    gap

  5. How many columns does a typical desktop design grid have?

    Show answer

    12

Lesson 5 of 13 in Web design & UI/UX · Written by · Course notes