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

nicholasdevereaux
nicholasdevereaux
16,353 Points

How did he come up with -4px?

Was there some type of math involved in coming up with -4px? I achieved the same result with changing the width to 49%. Is that not a good method to use?

3 Answers

Cory Harkins
Cory Harkins
16,500 Points

I believe (someone correct me if I'm wrong)...

The inherent margin between div's due to the browsers CSS rules, applies a 4 px margin to div's, when inline-block.

So if you have 2 divs side by side, the margin between the two = 8px, if you remove the right margin by -4 px, the div beside it becomes flush.

Something janky like that. It's a common result though, so always be aware of it, and that's how you fix it.

Bartlomiej Zabielski
PLUS
Bartlomiej Zabielski
Courses Plus Student 10,363 Points

yeah i dont understand why we need the negative margin property. shouldnt the normalize.css remove browser styling.

Sam Portillo
Sam Portillo
2,640 Points

Hi Nick,

The functionality of having a negative value for the margin declaration is that it provides a way for you to pull selected elements closer in proximity to other elements on the page.

For example, if you had the following layout on your page (evenly spaced):

<h1>Hello</h1> <h2>there</h2> <h3>World!</h3>

Applying a negative margin to the top of the h2 element:

h2{ margin: -4px 0 0 0; }

will move the entire element 4 pixels "closer" to the h1 margin (or reduces the top margin of the h2 element by 4px).

I suppose that if you aren't wanting to change the width of the element, this method would be favorable.

Hope this helps.