Reputation: 7821
Consider the following HTML and CSS
.outer {
border: 1px solid red;
margin-left: -10px;
}
.inner {
border: 1px solid black;
width: 50px;
margin-left: 10px;
}
body {
border: 1px solid blue;
}
<div class="outer">
<div class="inner">
inner
</div>
</div>
Where is that 1px extra spacing on the left, between the inner and outer div, coming from?
Change the negative margin to -11px, and it works as it should (no gaps).
You can also remove all the margins and compare with the "negative + positive" scenario above, to see how the two are not the same.
Thank you.
After figuring it out, I just wanted to share the below in case helpful to future visitors.
box-sizing: border-box
Upvotes: 1
Views: 676
Reputation: 1796
Instead of using border
fothe outer element. You can use outline
for it.
.outer {
outline: 1px solid red;
}
.inner {
border: 1px solid black;
width: 50px;
margin-left: 10px;
}
body {
border: 1px solid blue;
}
<div class="outer">
<div class="inner">
inner
</div>
</div>
Upvotes: 0
Reputation: 174
Sorry for wrong answer, i was writing in a rush... the math:
(-10pxMargin + 10pxMargin + 1pxBorder) = 1
you need
(-11pxMargin + 10pxMargin + 1pxBorder) = 0
let me know if this is clear enough, the thing is that margin doesnt include borders.
Upvotes: 2
Reputation: 2188
I think it has to do with the .outer
border. You can see below that when the red border is moved 10px
to the left, there is a gap where it would have been if it wasn't moved to the left (when the margin is 0
).
And because the .inner
div is relative to the .outer
div, it doesn't 'fill up' that 1px
gap it creates by moving .outer
10 pixels to the left.
Here you can see the gap. (It looks like 2 pixels because the page is zoomed in by 200%)
Extra "proof" to show that the extra space comes from the .outer
div,
if you remove the 1px
width of the .outer
border, you also remove the gap:
Upvotes: 3
Reputation: 5648
That extra pixel is from the .outer
element.
.outer {
margin-left: -10px;
}
.inner {
border: 1px solid black;
width: 50px;
margin-left: 10px;
}
body {
border: 1px solid blue;
}
<div class="outer">
<div class="inner">
inner
</div>
</div>
Upvotes: 1