On this page11 sections
  1. Learning Objectives
  2. 1. Borders
  3. 2. Rounded Corners
  4. 3. The 1-to-4 Value Shorthand
  5. 4. Centring With margin: auto
  6. 5. Margin Collapse
  7. 6. Shadows
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 006: Borders, Padding & Margin

Category2: The Box Model
Lecture006 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelBeginner

Now that you know the four layers of a box, let's control them. Borders give a box its outline and rounded corners; padding and margin set the breathing room. Both use a handy one-to-four-value shorthand.


Learning Objectives

  • Style borders: width, style, colour, and single sides
  • Round corners with border-radius
  • Set padding and margin with the 1-, 2-, 3- and 4-value shorthand
  • Centre a block with margin: 0 auto
  • Add depth with box-shadow

1. Borders

border: width style colour;

Example

html
<style>
  p { padding: 8px; }
  .solid  { border: 2px solid #1c7ed6; }
  .dashed { border: 2px dashed #e8590c; }
  .dotted { border: 3px dotted #2f9e44; }
  .bottom { border-bottom: 3px solid #ae3ec9; }
</style>

<p class="solid">solid</p>
<p class="dashed">dashed</p>
<p class="dotted">dotted</p>
<p class="bottom">only the bottom border</p>

Each side has its own property: border-top, border-right, border-bottom, border-left.


2. Rounded Corners

Example

html
<style>
  div { width: 120px; height: 80px; background: #4c6ef5; color: white; margin: 6px; padding: 8px; display: inline-block; }
  .soft  { border-radius: 8px; }
  .pill  { border-radius: 999px; }
  .round { border-radius: 50%; width: 80px; }
</style>

<div class="soft">8px</div>
<div class="pill">pill</div>
<div class="round">circle</div>

50% on a square box makes a circle. A very large value such as 999px makes a "pill" button.


3. The 1-to-4 Value Shorthand

Padding and margin both accept up to four values, read clockwise from the top:

You writeMeans
padding: 10px;all four sides 10px
padding: 10px 20px;top & bottom 10px, left & right 20px
padding: 10px 20px 30px;top 10px, left & right 20px, bottom 30px
padding: 10px 20px 30px 40px;top, right, bottom, left

Tip: remember TRouBLe, for Top, Right, Bottom, Left.

Example

html
<style>
  .btn { background: #2f9e44; color: white; border-radius: 6px; display: inline-block; }
  .a { padding: 6px; }
  .b { padding: 10px 24px; }
</style>

<span class="btn a">Cramped</span>
<span class="btn b">Comfortable</span>

4. Centring With margin: auto

A block with a set width can be centred by giving it automatic left and right margins:

Example

html
<style>
  .card { width: 260px; margin: 20px auto; padding: 16px; border: 1px solid #ddd; border-radius: 10px; }
</style>

<div class="card">I am centred: margin: 20px auto.</div>

5. Margin Collapse

When two vertical margins meet, they don't add up. The larger one wins.

output
.a { margin-bottom: 30px; }
.b { margin-top: 20px; }
→ the gap between them is 30px, not 50px

This only happens with top and bottom margins, never left and right.


6. Shadows

box-shadow: x y blur colour;

Example

html
<style>
  .card { width: 240px; padding: 16px; margin: 20px; border-radius: 12px; background: white;
          box-shadow: 0 4px 14px rgb(0 0 0 / 0.15); }
</style>

<div class="card">A soft shadow makes this card float.</div>

Common Beginner Mistakes

1. Forgetting the border style. border: 2px red; shows nothing. The style (solid) is required.

2. Reading the shorthand in the wrong order. It's clockwise: top, right, bottom, left.

3. margin: auto without a width. A full-width block has no spare room to centre in.

4. Surprise gaps from collapsing margins. If a gap is smaller than you expected, collapse is usually why.


Practice Questions

  1. Write a 1px solid grey border.
  2. How do you round the corners by 12px?
  3. What does margin: 10px 20px; mean?
  4. What does padding: 5px 10px 15px 20px; set on the left side?
  5. A box has width: 300px. How do you centre it horizontally?
  6. Two boxes have margin-bottom: 40px and margin-top: 25px. How big is the gap between them?

Practice Answers

  1. border: 1px solid grey;
  2. border-radius: 12px;
  3. Top and bottom margin 10px; left and right margin 20px.
  4. 20px.
  5. margin: 0 auto;
  6. 40px. Vertical margins collapse to the larger one.

Key Takeaways

  • border: width style colour; the style is required.
  • border-radius rounds corners (50% makes a circle).
  • Shorthand values go clockwise: top, right, bottom, left.
  • margin: 0 auto centres a block with a width; vertical margins collapse.