Code From ZeroCode From Zero
← Back to Course
Lesson 2 of 3
Module 2 · Lesson 2⏱ ~10 minCSS

Spacing and Layout Basics

Controlling the space around and inside elements — the foundation of page layout.



The Box Model

The box model is the single most important concept in CSS layout. Every HTML element — every heading, paragraph, image, and button — is a rectangular box. Understanding how these boxes work is the key to controlling your page layout.

Every box has four layers, working from the inside out:

|          margin (space outside the border)           |
|   |      border (the edge of the element)        |   |
|   |   |   padding (space inside the border)  |   |   |
|   |   |   |        content              |   |   |   |
|   |   |   |  (the text, image, etc.)    |   |   |   |
|   |   |   |____________________________|   |   |   |
|   |   |_____________________________________|   |   |
|   |______________________________________________|   |
|________________________________________________________|
  • Content — the actual text, image, or child elements inside the box. This is what you put between the opening and closing HTML tags.
  • Padding — space inside the border, between the content and the border edge. Padding has the same background color as the content area.
  • Border — the edge of the element. It wraps around the padding and content. Borders can be visible (colored lines) or invisible.
  • Margin — space outside the border, separating this element from its neighbors. Margins are always transparent — you see the background of the parent element through them.

Think of it like a picture in a frame: the picture is the content, the mat board around the picture is the padding, the frame itself is the border, and the space between this frame and other frames on the wall is the margin.


Padding and Margin

Padding and margin are the two properties you'll use most often to create space:

css
div {
  padding: 20px;    /* space inside the box */
  margin: 10px;     /* space outside the box */
}

You can also control each side individually:

css
div {
  padding-top: 20px;
  padding-right: 15px;
  padding-bottom: 20px;
  padding-left: 15px;

  margin-top: 10px;
  margin-right: 0;
  margin-bottom: 30px;
  margin-left: 0;
}

Shorthand saves you from writing four lines. There are several shorthand patterns:

css
/* One value: all four sides the same */
padding: 20px;

/* Two values: top/bottom, left/right */
margin: 10px 20px;

/* Three values: top, left/right, bottom */
padding: 10px 20px 30px;

/* Four values: top, right, bottom, left (clockwise!) */
margin: 10px 15px 20px 25px;

Pro tip: the two-value shorthand — margin: 10px 20px; — is incredibly common. It sets the top and bottom to 10px and the left and right to 20px. You'll see it used all over real stylesheets.

Values are usually in pixels (px), but you can also use em (relative to font size), rem (relative to the root font size), or percentages. For now, pixels are fine.


Borders

Borders give elements a visible edge. The border property takes three values: width, style, and color:

css
div {
  border: 2px solid #cccccc;
}

img {
  border: 1px dashed #999999;
}

Border styles — the most common are solid (a continuous line), dashed (dashes), dotted (dots), and none (no border — the default).

Rounded corners are one of the most popular CSS features. The border-radius property rounds the corners of any box:

css
button {
  border: 2px solid #4F7EF7;
  border-radius: 8px;
  padding: 10px 20px;
}

.avatar {
  border-radius: 50%;   /* perfect circle! */
}

A border-radius of 50% on a square element creates a perfect circle — great for profile pictures and icon backgrounds. Small values like 4px to 12px give buttons and cards a soft, modern look.


Width and Height

You can control how wide and tall an element is:

css
div {
  width: 600px;
}

But a fixed width can cause problems — on a phone screen that's 375px wide, a 600px-wide element would overflow and force the user to scroll sideways. That's where max-width shines:

css
div {
  max-width: 600px;   /* never wider than 600px, but can shrink */
  width: 100%;         /* take up all available space, up to 600px */
}

This combination is extremely common: max-width sets an upper limit, and width: 100% makes sure the element fills the available space on smaller screens. The element will be 600px wide on a big monitor but shrink to fit on a phone — no horizontal scrolling needed.

  • width — sets the exact width
  • max-width — the maximum width (can be narrower on small screens)
  • min-height — ensures an element is at least a certain height, even if the content is short

Best practice: use max-width instead of width for content containers. It makes your pages naturally responsive without any extra work.


Centering Content

Centering is one of the first things every CSS beginner wants to do. There are two kinds of centering, and they use different properties:

Centering text (inline content) — use text-align: center;:

css
h1 {
  text-align: center;
}

This works on headings, paragraphs, links, and any text inside an element. It's also how you center images that are inside a block.

Centering a block element (like a <div> or a card) — use margin: 0 auto;:

css
div {
  max-width: 600px;
  margin: 0 auto;   /* 0 top/bottom, auto left/right = center */
}

The auto value tells the browser to split the remaining horizontal space evenly between left and right margins. The result: the element sits in the center of its parent. This only works when the element has a defined width or max-width — the browser needs to know how wide the element is before it can calculate the margins.

Quick reference:

  • text-align: center; — centers text and inline content (headings, paragraphs, images)
  • margin: 0 auto; — centers a block element (div, section, card) horizontally

Try It Yourself

Take your Module 1 page and apply spacing and layout:

  • Add padding: 20px; to your <main> section
  • Give your content a max-width (try 700px) and center it with margin: 0 auto;
  • Add a visible border and border-radius to your profile image
  • Add margin-bottom to your headings to create breathing room

Notice how much more polished the page feels just by adding space and centering!


Key Terms

Box Model
The way CSS treats every HTML element as a rectangular box with four layers: content, padding, border, and margin. Understanding it is essential for layout.
Padding
Space inside the border, between the content and the border edge. Padding shares the element's background color.
Margin
Space outside the border, separating an element from its neighbors. Margins are always transparent.
Border
The edge of an element, defined by width, style, and color. Can be visible (solid, dashed) or invisible (none).
max-width
A CSS property that sets the maximum width of an element. Unlike width, it allows the element to shrink on smaller screens — key for responsive design.

Check Your Understanding

What are the four layers of the box model, from inside to outside?

Answer: Content, padding, border, margin. Content is what's inside; padding is space between content and border; border is the edge; margin is space outside the border.

What is the difference between padding and margin?

Answer: Padding is space inside the border (between the content and the border). It shares the element's background color. Margin is space outside the border (between this element and its neighbors). Margins are always transparent.

How do you center a block element horizontally?

Answer: Set a width or max-width on the element, then use margin: 0 auto;. The auto value tells the browser to split remaining space evenly on both sides.