Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS CSS Basics (2014) Enhancing the Design With CSS Web Fonts with @font-face

U Lun Chung
U Lun Chung
2,294 Points

padding or margin?

I really don't know when I should use padding or margin? if i want to saperate two item. PLS help me~

2 Answers

Padding is the space created inside of an element and margin is the space outside of it. Paragraphs for example will use a margin so that they don't stack directly on top of each other. You want to use margin to space two elements apart.

If you look at the text box where you type in comments here, it uses a padding of 9px so that the text doesn't hit the edge of the box.

Margins collapse too which means that if two elements have a margin of 10px, they will be 10px apart, where as padding would make them 20px apart.

This Stack Overflow article gives a very detailed explanation: http://stackoverflow.com/questions/2189452/when-to-use-margin-vs-padding-in-css

Haider Ali
.a{fill-rule:evenodd;}techdegree seal-36
Haider Ali
Python Development Techdegree Graduate 24,728 Points

Hi there. As Luke said, padding is inside of the element whereas margin is space on the outside. You should use padding if you want to make a certain element bigger in size and you should use margin if you want to add space on the outside and in between different elements.