On this page11 sections
Lecture 006: Borders, Padding & Margin
| Category | 2: The Box Model |
| Lecture | 006 of 057 |
| Estimated video length | 12 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
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
<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
<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 write | Means |
|---|---|
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
<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
<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.
.a { margin-bottom: 30px; }
.b { margin-top: 20px; }
→ the gap between them is 30px, not 50pxThis only happens with top and bottom margins, never left and right.
6. Shadows
box-shadow: x y blur colour;
Example
<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
- Write a 1px solid grey border.
- How do you round the corners by 12px?
- What does
margin: 10px 20px;mean? - What does
padding: 5px 10px 15px 20px;set on the left side? - A box has
width: 300px. How do you centre it horizontally? - Two boxes have
margin-bottom: 40pxandmargin-top: 25px. How big is the gap between them?
Practice Answers
border: 1px solid grey;border-radius: 12px;- Top and bottom margin 10px; left and right margin 20px.
- 20px.
margin: 0 auto;- 40px. Vertical margins collapse to the larger one.
Key Takeaways
border: width style colour; the style is required.border-radiusrounds corners (50%makes a circle).- Shorthand values go clockwise: top, right, bottom, left.
margin: 0 autocentres a block with a width; vertical margins collapse.