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

HTML How to Make a Website Customizing Colors and Fonts Resize Text

Teacher Russell
Teacher Russell
16,873 Points

navigation styling:margin and padding

I still don't get, for example, why you're adding margin of 20px 0 0; and padding 10px 0; to the nav. I don't see how you're coming to the conclusion that it's necessary, and I don't really see the effects. I just can't make sense of how to use space, especially margin and padding. Everything else about HTML and CSS is so simple:)!

4 Answers

Jason Welsh
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jason Welsh
Treehouse Project Reviewer

This stuff can be tricky. Just be patient and persistent.

Do you know what codepen is? Go to codepen.io you don't even have to sign up. Code up a couple things and when you get to something you can't explain, save it and send me the link. I will take a look and explain what is going on and fix the code so you can see exactly what I am talking about :)

Jason Welsh
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jason Welsh
Treehouse Project Reviewer

I can tell you what helped me conceptualize it, and maybe that will help.

So imagine the web page as space. Now each element in the HTML is essentially a rectangular box by default (you can change the shape but ignore that for now). Ok so you have looked that the box model. Lets work from the inside out. We will start with the content area.

The content area is, well, the content. The text in a <p> element. The link in an <a> element. Whatever we throw into a <div> or a <span>.

Next we have padding. This creates space around the content between the content and the border, or edge of the rectangular box. You can add or remove padding to create space INSIDE the box. It does not necessarily change the position of the box, though it can if there is not enough room for the padding.

Now we have the border. The border is just the edge of the box. It can be visible by adding a border property, or invisible, but it exists either way. Anything inside the border will have the same background property (color, image, etc).

Finally, and perhaps the most confusing at first, is the margin. This is the space BETWEEN the boxes. The space between the rectangular boxes, and separates them from each other. The more margin, the more space between boxes. Remove the margins and the box's borders will touch each other. Set a negative margin and now we can pull boxes over each other.

Take a look at your box model again. Make some elements html. Give everything a border style so you can see the edges. Now start targeting them and playing with margins and padding. You will start to get it.

Cindy Truong
Cindy Truong
9,853 Points

This has been confusing me for the longest time! You have no idea how much this helped. You made it so simple to understand, thank you! :D

Oh my gosh! Thank you so much for this very detailed explanation. This is just what I need. I wish this is explained really really early on in the lesson. It would have been very helpful. This is the best explanation about margin, padding and border. Thank you!

Jason Welsh
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jason Welsh
Treehouse Project Reviewer

The top margin would prevent margin collapsing (google that it is important to understand) as well as create space above the selected element. The padding value adds space around the content inside the selected element, in this case at the top and bottom. Loom up the CSS box model. Once you understand the box model all of this stuff is a lot easier to understand.