Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS

Kendall Chung
Kendall Chung
9,511 Points

When to use padding, border, margin?

Hey guys,

I am just confused about when to use padding border and margin? can someone please show me the standard or give me a guide for which one to use and when?

thanks!

3 Answers

anil rahman
anil rahman
7,786 Points

It all depends on what you want to do if you want to decorate a div to make a square or something and you want a border to do some presentation and colours and stuff. But you dont need a colour around the edges thats up to you.

You would want padding if you wanted to put extra spacing between the stuff in your box and the edge of the box.

Margin is more like positioning. So if two boxes were too close together the margin can help control the positioning/spacing of elements.

Not sure that I can clearly provide an answer but I did find the following helpful.. http://stackoverflow.com/a/2189462 Gives a nice visual to describe all 3 and a clear, consise answer. Hope this helps!

anil rahman
anil rahman
7,786 Points

Or you could make a div give it a border some margin and padding. Then when you trun the html page in chrome, you can go into developer tools and it will actually allow you to see the border and padding and margin and how they are affecting the div. It will be at the bottom panel in the right hand side i think you scroll to the bottom to see that diagram. Just google it if you don't understand developer tools, nice and easy.