Since the edge of an element with margin-left: -10px
crosses the edge of its parent, why doesn’t the same happen with margin-right: -10px
?
example
div { background: red; width: 200px; height: 100px; } p { background: blue; width: 100%; } .left { margin-left: -10px; } .right { margin-right: -10px; }
<div> <p class="left">Hello</p> </div> <div> <p class="right">Hello</p> </div>
Answer 50faea7446ce3ec6a60004cf You cannot see the effect of margin-right because the 'width' property of div causes the margin of div to have more than 10px distance from the right wall of the body. Consequently, it causes the margin-right property not to have any visual effect.
It is highly compatible. Negative margins are wholly supported across all modern browsers (and IE6 in most cases). It reacts differently when floats are applied. Negative margins are not your everyday CSS so they should be applied with care.
The good news is that negative margins do work!
To see negative margins at work, consider the following code snippet:
.wrapper { outline: 1px solid blue; padding: 40px; } .content { outline: 1px solid red; background-color: #D8D8D8; } .content p { outline: 1px dotted blue; background-color: rgba(255, 255, 0, 0.3); margin: 0 0 0 0; text-align: justify; } .content p.lefty { margin-left: -20px; } .content p.righty { margin-right: -20px; }
<div class="wrapper"> <div class="content"> <p>Lorem ipsum dolor sit amet, ...</p> <p class="lefty">Sed ipsum ante, dictum vel rhoncus id, ...</p> <p class="righty">Curabitur aliquam tristique mattis...</p> </div> </div>
I added outline
's to all the div
's and p
's to show the extend of the content boxes.
The first paragraph has zero margins and I offset one paragraph to the left and the other to the right.
If you have enough content to fill the width of the paragraph (or if you show the outlines), you will see the text box flow outside of the content
box. You can also see from the paragraph outline
's that the text does extend to the left and to the right.
However, to see the effect on the right, you need enough content to fill in the full width of the paragraph or you need to set a background color or outline on the child element.
If you start fixing the width and height, on content
, you will see other effects such as the paragraphs flowing outside of content
.
Studying this simple code structure illustrates many facets of the CSS box model and it is illuminating to spend some time with it.
Fiddle Reference: http://jsfiddle.net/audetwebdesign/2SKjM/
If you remove the padding from the wrapper
, you may not notice the right margin shift because the elements will extend to fill the full width of the parent container or the page width depending on the specific details of the HTML/CSS.
Why Did My Example Not Show the Effect!!!???
In your example, you did not see the effect because you fixed the width of the p
elements by specifying width: 100%
, which directs the paragraph to take the width of the parent container (200px in your example).
If you make a simple change to the width from 100%
to auto
:
p { background: blue; width: auto; }
you will see your second paragraph just out to the right as you expected.
Note: Outline vs Border Also, note that the red box is due to the outline
, which encloses the text boxes within the content
, even when the text boxes extend outside of the parent (content
) element. As a result, the outline
box can be much bigger than the border
box of the corresponding element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With