Be able to apply spacing including paragraph (before and after) and line
📚 Topic 14: Styles – Spacing in CSS
1️⃣ Paragraph Spacing (Margin & Padding)
Think of a paragraph as a person at a party. Margin is the personal space they keep from others, while padding is the cushion inside their own space. By adjusting these, you control how close or far a paragraph sits from the page edges and from other elements.
p { margin-top: 20px; margin-bottom: 10px; padding-left: 15px; }
2️⃣ Line Spacing (Line-Height)
Line-height is like the height of a step in a staircase. If the step is too tall, the steps look spaced out; if too short, they feel cramped. Adjusting line-height makes text easier to read.
p { line-height: 1.5; }
3️⃣ Using CSS Shorthand for Spacing
CSS allows you to write spacing in a compact form. The order follows the clock: top, right, bottom, left.
| Shorthand | Values |
|---|---|
| margin: 10px; | All sides = 10px |
| margin: 10px 20px; | Top/Bottom = 10px, Left/Right = 20px |
| margin: 5px 10px 15px 20px; | Top = 5px, Right = 10px, Bottom = 15px, Left = 20px |
4️⃣ Practical Example: A Blog Post Layout
HTML:
<article>
<h2>My First Blog Post</h2>
<p>Lorem ipsum dolor sit amet...</p>
<p>More content here...</p>
</article>
CSS:
article { padding: 20px; background:#ecf0f1; }
article h2 { margin-bottom: 15px; }
article p { margin-top: 10px; margin-bottom: 10px; line-height: 1.5; }
line-height: 1.5 for clear readability. Remember to keep padding consistent inside containers.
5️⃣ Quick Reference Cheat Sheet
| Property | Purpose | Typical Value |
|---|---|---|
| margin-top / margin-bottom | Space outside the element | 10px, 1em, 2rem |
| padding-top / padding-bottom | Space inside the element | 5px, 0.5em, 1rem |
| line-height | Vertical spacing between lines of text | 1.4, 1.5, 1.6 |
Revision
Log in to practice.