CSS Margin
Table of Contents + β
In the last lesson you learned about Text Decoration. That was about lines on your text. Now letβs look at the space around your boxes. This is where margin comes in.
π What is Margin?
Every element on your page sits inside an invisible box. Margin is the empty space on the outside of that box. It pushes other elements away.
Think of a photo frame on a wall. The frame is your element. Now imagine you hang two frames next to each other. If they touch, it looks crowded. So you leave a gap between them. That gap is the margin.
- Margin is space outside the element.
- It has no color. You cannot see it. You only see the gap it makes.
- Its job is to push other elements away so things do not touch.
π€ Why Do We Need Margin?
Here is the real problem. By default the browser puts elements right next to each other. Headings, paragraphs, buttons, boxes. They all stack with very little space between them. So the page looks cramped and is hard to read.
Spacing fixes that. Margin lets you control the space between elements. Letβs look at some everyday cases.
- Want a paragraph to sit away from the one above it? Add a top margin.
- Want a button not to touch the edge of the screen? Add a margin.
- Want a box in the middle of the page? Margin can do that too.
π§© The Syntax
The simplest way is to set the same space on all four sides at once. You use the margin property and give it a value.
This code gives the paragraph 20 pixels of space on every side.
p { margin: 20px;}You can also set each side on its own. There are four separate properties for that.
This code sets a different space on each side of a box.
.box { margin-top: 10px; margin-right: 20px; margin-bottom: 30px; margin-left: 40px;}Here is what each property controls.
| Property | What it does |
margin-top | space above the element |
margin-right | space on the right side |
margin-bottom | space below the element |
margin-left | space on the left side |
β‘ The Shorthand
Writing four lines every time is slow. So CSS gives you a short way. You put all the values on one margin line. The order goes clockwise. That means top first, then right, then bottom, then left.
This sets top to 10px, right to 20px, bottom to 30px, and left to 40px, all in one line.
.box { margin: 10px 20px 30px 40px;}You can also give just two values. The first one is for top and bottom. The second one is for left and right.
This gives 10px on the top and bottom, and 20px on the left and right.
.box { margin: 10px 20px;}Easy way to remember
Clockwise, like a clock. Start at the top and go around: top, right, bottom, left.
π― A Real Example: Centering a Box
Here is a favorite trick that uses margin. You can put a box in the middle of the page. You set the left and right margin to auto. The word auto tells the browser to share the leftover space equally on both sides.
This makes a card sit in the center of the page.
<div class="card">Hello, I am centered!</div>.card { width: 300px; margin: 0 auto;}The 0 sets the top and bottom margin to zero. The auto handles left and right. You must give the box a width for this to work. Without a width the box fills the whole row. Then there is no leftover space to center.
What you see
A box that is 300 pixels wide sits in the middle of the page. There is equal empty space on its left and on its right. As you make the browser window wider, the box stays in the center.
β οΈ Common Mistakes
A few things trip up beginners. Watch out for these.
- Forgetting that
margin: 0 autoneeds a width. No width means the box already fills the row. So there is no space left to center it. - Thinking margin has a color. Margin is always see-through. If you want a colored space inside the box, you need padding, not margin. You will learn padding next.
- Top and bottom margins collapsing. This is called margin collapse. When one box sits above another, their top and bottom margins do not add up. The browser keeps the bigger one only. So if you set 20px below one box and 30px above the next, you get 30px between them, not 50px. This is normal CSS, but it surprises many beginners.
β Best Practices
Keep these habits and your spacing will stay clean.
- Use the shorthand
marginwhen you can. It keeps your CSS short and easy to read. - Pick one spacing scale and stick to it. A spacing scale is a small set of values you reuse, like 8px, 16px, 24px. Mixing random numbers like 7px, 13px, 19px makes the page look messy.
- Use
margin: 0 autoto center fixed-width boxes. It is the simplest way. - Remember the difference. Margin is space outside the box. Padding is space inside. Do not mix them up.
π§© What Youβve Learned
You now know how to control the space around your elements. Here is a quick recap.
- β Margin is the empty, see-through space on the outside of an element.
- β It pushes other elements away so your page is not cramped.
- β You can set all four sides at once, or each side on its own.
- β The shorthand goes clockwise: top, right, bottom, left.
- β
margin: 0 autocenters a box, but the box needs awidth.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does margin control?
Why: Margin is the empty, see-through space on the outside of an element. It pushes other elements away.
- 2
In the shorthand 'margin: 10px 20px 30px 40px;', which side gets 30px?
Why: The order is clockwise: top, right, bottom, left. So the third value, 30px, is the bottom.
- 3
What is needed for 'margin: 0 auto' to center a box?
Why: Without a width the box already fills the whole row, so there is no leftover space to share on the sides.
- 4
When two vertical margins meet, what happens?
Why: This is called margin collapse. The browser keeps the bigger of the two margins, not the sum.
π Whatβs Next?
Now you know the space outside the box. Next you will learn about the space inside it, which is padding.