KodingKaro
CSS · Beginner · Width, Height, Margin, and Padding

Margin and padding for spacing

Margin and padding both create space, but on opposite sides of the border. Choosing the right one keeps component spacing consistent.

✓ What you will learn in this topic

  • Explain the key idea: Use padding for internal room and margin or layout gap for external separation.
  • Apply the concept: The demo gets internal horizontal and vertical breathing room, and neighboring blocks are separated vertically.
  • Avoid this common mistake:

Concept explanation

Padding belongs to a component: it creates room around the component’s own content. Margin separates the component from neighbors. Logical properties such as padding-inline and margin-block express direction by meaning rather than assuming left-to-right writing.

Shorthand values follow a predictable order. One value applies all around; two values mean block then inline; four values go clockwise from top. Avoid stacking several competing margins when a parent layout’s gap can express the spacing more clearly.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

.demo {
  padding: 1rem 1.5rem;
  margin-block: 1rem;
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
.demo {
  padding: 1rem 1.5rem;
  margin-block: 1rem;
}

Expected result: The demo gets internal horizontal and vertical breathing room, and neighboring blocks are separated vertically.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
.card-list {
  display: grid;
  gap: 1rem;
}
.card {
  padding: 1.25rem;
}

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

A fixed height can clip content when text wraps, zoom increases, or a translation changes the amount of copy.

Best practice

Prefer min/max constraints and intrinsic content sizing; use margin for outside separation and padding for inside breathing room.

Practice exercise

Make a card fit narrow screens while keeping comfortable internal spacing. Add a minimum height only if the design genuinely needs one. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Use padding for internal room and margin or layout gap for external separation.
  • Prefer min/max constraints and intrinsic content sizing; use margin for outside separation and padding for inside breathing room.

Knowledge check

Which property adds space inside a component’s border?

Reveal the explanation

Padding is inside the border; margin separates the element from its surroundings.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
.demo {
  padding: 1rem 1.5rem;
  margin-block: 1rem;
}

Expected result:

The demo gets internal horizontal and vertical breathing room, and neighboring blocks are separated vertically.

Hands-on practice task

Make a card fit narrow screens while keeping comfortable internal spacing. Add a minimum height only if the design genuinely needs one.

In this section

Other lessons in Width, Height, Margin, and Padding

Broaden your knowledge

Related learning topics