See Every Element as a Box

The garden’s website already has its colors and its type. But look at the events page, and nothing has room to breathe. Each of its two events has a heading and a paragraph, and the browser’s defaults determine every gap on the page.

The fix starts with how the browser lays out a page. It draws every element as a box, and every box has the same four layers. In this lesson, you’ll name those layers, use them to turn the two events into cards, and read the shorthand values that set all four sides of a box at once. Start by getting the garden’s site open in front of you.

Set Up the Garden Site

Over this course, you’ll give the garden’s site a real design: spacing, sizes, borders, photos that fit, and readable type, all from a well-organized stylesheet. The course assumes you can write CSS rules in a linked stylesheet, choose colors, size text in rem, store values in custom properties, and find a rule in DevTools’ Styles pane.

There’s no JavaScript, framework, or build step. Layout tools such as flexbox and grid come later on the path, so for now, the event cards you build stack one below the other. If you’d like to see where these skills lead, HTML and CSS are the first stage of the front-end developer roadmap.

Keep the page open beside the lesson, and type the CSS rather than pasting it. Then change values to see what happens. Watch the page to check that your CSS is taking effect. Each section ends with exercises in the browser, and the course ends with a project that moves the whole design into the site’s own stylesheet.

Skip any step you’ve already done:

  1. Get a current version of Chrome or Edge. Every screenshot comes from Chrome, but you can follow along in Edge.
  2. Install Visual Studio Code (VS Code) from its website. Then open its Extensions view with Ctrl+Shift+X, or Cmd+Shift+X on macOS, search for Live Preview, and click Install on the one published by Microsoft.
  3. Get the garden/ folder. Use your own if you styled the garden’s site with colors, selectors, and custom properties before this course. If you’re arriving directly, or your folder differs, download the course’s sample code ZIP, unzip it, and copy its 00-intro/garden/ folder to where you keep your projects.
  4. Add the garden’s photos. Download garden-photos.zip from the course’s supporting materials, unzip it, and copy the six photos from its garden-photos/ folder into garden/images/. The events page shows the seed swap photo as seed-swap-2026.jpg, so if that file isn’t in your images/ folder yet, save a copy of seed-swap.jpg under that name.
  5. Open the folder in VS Code with File → Open Folder, then open events/upcoming.html.
  6. Start the preview. With the page as the active tab, open the Command Palette with Ctrl+Shift+P, or Cmd+Shift+P on macOS, and run Live Preview: Show Preview (External Browser). If the page opens in another browser, paste its address into Chrome or Edge.

You’ll try out ideas on the events pages with the practice stylesheet, events/events.css. The <head> of upcoming.html should already link it with <link rel="stylesheet" href="events.css">. If yours doesn’t, add that line right after the <title>.

To give each event a box you can style, each one needs an element of its own, with a class your CSS can select. Look inside <main> in upcoming.html, and follow the step that matches what you find:

  • A <section> headed Coming Up, with the two events as <article> elements: Add class="event-card" to both <article> opening tags, unless they have it already, as in the starter folder. Then skip ahead to saving the page.
  • The two events as headings and paragraphs, separated by leaf dividers: Replace everything in <main> from the first leaf divider down to the end of the <figure> with a section that holds the events as articles.

Here’s the section’s first half, its heading and the work day:

Language: HTML
<section>
  <h2>Coming Up</h2>
  <article class="event-card">
    <h3>Next Work Day</h3>
    <p>
      <time datetime="2026-11-28T09:00">Saturday,
        November 28, from 9 a.m.</time> to noon.
      <a href="work-day.html">The work day's jobs</a>
    </p>
  </article>

The <section> groups the events under one heading, Coming Up, so the event headings move down a level to <h3>. Each event makes sense on its own, so each one is an <article>. The class event-card says what each article is, and it’s how your CSS will select them. The second half holds the seed swap, with its photo moved inside the article:

Language: HTML
  <article class="event-card">
    <h3>Spring Seed Swap</h3>
    <p>
      <time datetime="2027-03-21T14:00">Sunday,
        March 21, from 2 p.m.</time> to 4 p.m.
      <a href="seed-swap.html">How the swap works</a>
    </p>
    <figure>
      <img
        src="../images/seed-swap-2026.jpg"
        alt="Two long tables covered in seed envelopes"
        width="800"
        height="600"
      >
      <figcaption>
        Tomás and Priya ran last spring's swap.
      </figcaption>
    </figure>
  </article>
</section>

If you had leaf dividers, they’re gone, because the cards you’re about to build will separate the events instead. Save, and open the page at http://127.0.0.1:3000/events/upcoming.html:

The events page with a Coming Up heading, then the work day and the seed swap with its photo, with nothing marking where one event ends and the next begins.
The Coming Up Section Before Any Box Styles

Either way, the events sit under one Coming Up heading, each in an article with a smaller heading of its own. Sections and articles have no look of their own, though, so nothing shows where one event ends and the next begins. That’s what the boxes are for.

Name the Four Layers

Rosa would like each event to be a pale green card, like a notice pinned to a board, with its text set in from the edges and clear space between the cards. To get there, you need to know what CSS can change about a box.

In the box model, the browser draws every element as a rectangular box with the same four layers, nested from the inside out:

  • Content is the text or image the element holds.
  • Padding is space between the content and the border. The element’s background fills it, so padding is room inside the box.
  • Border is a line around the padding. It can be any width, including zero.
  • Margin is space outside the border. It’s always transparent, and it pushes neighboring boxes away, so margin is room between boxes.

The picture shows the four layers around one box, with the background covering the content and padding but not the margin:

Margin, border, padding and content nested from the outside in, with the background filling the padding and content, not the margin.
Four Layers of Every Box, From Content Out to Margin

You’ve met margins already, without the name. The gaps between paragraphs that you saw before writing any CSS are margins from the browser’s default stylesheet, which gives every <p> a top and bottom margin of 1em. The default stylesheet also gives <body> a margin of 8px, which is why nothing on the page touches the window’s edge. Every element has all four layers, even when some of them take up no space.

A framed picture on a wall is a close match. The picture is the content, the mat around it is the padding, the frame is the border, and the space between frames on the wall is the margin. The comparison stops at the mat’s color. A mat is a separate piece of card in any color, but padding is part of the element, so by default it shows the element’s background, which also runs under the border but never into the margin.

If you’re not sure yet which of the two kinds of space to use, the button below opens a chat with the mentor, with the question ready to send:

Turn the Events Into Cards

Each card needs a background, room inside it, and an edge, so start with the three layers that belong to the box itself. Add a rule at the end of events.css:

Language: CSS Filename: events/events.css
.event-card {
  background-color: honeydew;
  padding: 16px;
  border: 2px solid var(--green);
}

The declarations follow the layers from the inside out. The padding property sets the padding on all four sides at once. The border property draws a solid line, 2px wide, in the garden’s green from your tokens, the custom properties on :root. Borders get a lesson of their own later in the course, and that’s all you need about them for now.

Save, and each event’s text moves in from the edges of its own pale green card:

The work day and the seed swap as two pale green cards with green borders, their text set in from the edges, the second card touching the first.
Padding Gives the Text Room Inside Each Card

The text has room now, but the two cards touch, so their borders form one thick line between them. More padding can’t fix that, because padding only adds space inside each box. The space between boxes is the margin’s job. Add a bottom margin to the rule:

Language: CSS Filename: events/events.css
.event-card {
  background-color: honeydew;
  padding: 16px;
  border: 2px solid var(--green);
  margin-bottom: 24px;
}

The margin-bottom property adds space only below each card. Save, and the cards come apart:

The work day and the seed swap as two pale green cards with green borders, with a strip of white between them.
Two Event Cards, Separated by Margin

Between the cards is a strip of white, the background of <main> showing through, because margin is transparent. The space above each heading is more than 16px, though. That’s because the heading brings its own margin from the browser’s defaults, inside the padding. The next lesson shows you how to tell those layers apart in DevTools.

Why 16px and not a custom property, when your tokens are on :root? While you’re trying values out, a plain number shows exactly what you’re changing, so the lessons write sizes as plain px and rem values. Later in the course, the values that keep repeating become a spacing scale of custom properties, next to the tokens you already have.

Read the Shorthand Values

The cards would look better with more room at their sides than at their top and bottom. You could write four longhand properties, padding-top, padding-right, padding-bottom, and padding-left. Instead, padding and margin both take one to four values, and you’ll see all four forms in other people’s CSS:

  • One value, padding: 16px, sets all four sides.
  • Two values, padding: 16px 24px, set the top and bottom, then the right and left.
  • Three values, padding: 16px 24px 8px, set the top, then the right and left, then the bottom.
  • Four values, padding: 16px 24px 8px 4px, set the top, right, bottom, and left, clockwise from the top.

If you think of the four values as a function’s parameters, the missing ones have defaults, but they aren’t reset to zero. Each one is taken from a value you gave: the right copies the top, the bottom copies the top, and the left copies the right. So margin: 0 0 24px sets the same bottom margin as margin-bottom: 24px, but also sets the top and right to 0, with the missing left copying the right.

In your rule, change padding: 16px; to padding: 16px 24px;, and save. Compared with the cards above, the text moves further in from each card’s left and right edges, while its distance from the top and bottom edges stays the same:

The two pale green event cards again, now with wider space between the text and each card's left and right borders, and the same space at the top and bottom.
Two Padding Values, More Room at the Sides

Padding adds to a card’s height at its top and bottom edges, and you can measure that. If you change the padding to padding: 24px;, the top and bottom padding each grow from 16px to 24px, so each card should get 16px taller. The sides were already 24px, so the text should stay where it is horizontally.

Check it in DevTools. Hover over an <article> line in the Elements panel, and the page highlights the card with a label that shows its width and height in pixels. Note the height, change the padding to 24px, save, and hover again.

The new height is 16px more than the one you noted, 8px from the top padding and 8px from the bottom. Change the padding back to 16px 24px before you go on.

Reading a list of values quickly takes some practice. The button below asks the mentor for a short quiz on it, one value list at a time:

Name Sides by Text Direction

Suppose the garden’s site is translated one day into a language written right to left, such as Arabic. Every line of text would then begin at the right edge, but padding-left would stay on the left. That’s why you’ll also meet padding and margin properties named by the direction text flows instead of by top, right, bottom, and left.

The inline direction is the way a line of text runs, left to right in English. The block direction is the way paragraphs stack, top to bottom. So padding-block: 16px sets the top and bottom padding, and padding-inline: 24px sets the left and right.

The logical properties margin-block and margin-inline work the same way for margins. On a right-to-left page, the inline direction runs right to left, and these properties follow it.

Try logical padding properties on your cards. Replace the padding line with these two lines, and save:

Language: CSS
padding-block: 16px;
padding-inline: 24px;

Nothing on the page moves, because on an English page, these two lines and padding: 16px 24px mean the same thing. Undo the change, so your rule matches the one the next lessons start from.

The logical properties help when the two ends of a line need different amounts of space. Each axis also has a start and an end, and padding-inline-start sets the padding on the side where a line of text begins. On an English page, that’s the left, and on an Arabic page, it’s the right.

So padding-inline-start: 40px puts the extra room before the text in both languages, while padding-left: 40px would put it after the text on the Arabic page. With the logical name, one rule keeps working when the site is translated.

You can now name the four layers of every box and read the values that set them. But when a page has an unexpected gap, like the extra room above each card’s heading, you still need to find out which layer of which box made it. In the next lesson, Find Gaps in the Box Diagram, you’ll use DevTools to do exactly that.

Become a Member to join the conversation.