Nearby lessons

9 of 19

↔️ CSS Margin Properties

📌 What are CSS Margin Properties?

CSS Margin Properties control the space around HTML elements, outside of their borders. They are essential for creating separation between elements and improving layout structure.

Margins can be set individually (top, right, bottom, left), or using shorthand notation with 1 to 4 values. You can also use auto or even negative margins to shift elements.

📏 Margin Shorthand (All sides)

The margin property sets all four margins using one value.

Example02
🎨Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🔄 Individual Margins

You can set margin-top, margin-right, margin-bottom, and margin-left separately for more control.

Example03
🎨Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

↕️ Margin - Two Values

The margin property can take two values: vertical (top & bottom) and horizontal (left & right).

Example04
🎨Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧭 Margin - Three Values

With three values: first is top, second is left & right, third is bottom.

Example05
🎨Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧭 Margin - Four Values

Four values are applied in clockwise order: top, right, bottom, left.

Example06
🎨Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🎯 Margin Auto

Use margin: auto; to center block elements horizontally (requires fixed width).

Example07
🎨Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧠 Test Your Knowledge

5 Questions
Progress: 0 / 5