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

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,486 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.