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 Basic Selectors ID Selectors

Padding vs. Margin

I'm confused as to the difference between padding and margin. When i set my padding on an element like h1 after setting a border, nothing noticeable seems to happen.

I guess I'm confused about the visual aspect of changing the padding dimensions of the element.

1 Answer

The simplest answer is that margin impacts the space outside the element, and padding defines the space inside the element. If you have an h1 element with the word "test" and use the following style, you should see that the word test is very close to the left edge, and the border is close on the top and bottom, and the whole thing is very close to the top left corner of the browser:

h1 {
    border: solid black 1px;
    padding: 0;
    margin: 0;
}

Now, if you change the padding to padding: 25px; you should see the border expands a little bit, and the word "test" moves down and to the right, and the bottom border is further away from the text now.

To see the change in margin, change that line to margin: 25px; and you should see that the border around the text moves down and to the right from the top left corner, as it makes room for the 25px of margin around the outside of it. If you had text underneath the h1 element, it would also have moved down to make room for the 25px margin at the bottom of the element.

Hopefully, that clears it up for you, but if not, just reply and I'll see if I can clarify.

Thank you so much!! That helped a lot! It makes more sense now :)