Based on your suggestion original suggestion (setting negative margins), I have tried and come up with a similar method using percentage units for dynamic browser width:
HTML
<div class="grandparent">
<div class="parent">
<div class="child">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore neque repellat ipsum natus magni soluta explicabo architecto, molestias laboriosam rerum. Tempore eos labore temporibus alias necessitatibus illum enim, est harum perspiciatis, sit, totam earum corrupti placeat architecto aut minus dignissimos mollitia asperiores sint ea. Libero hic laudantium, ipsam nostrum earum distinctio. Cum expedita, ratione, accusamus dicta similique distinctio est dolore assumenda soluta dolorem quisquam ex possimus aliquid provident quo? Enim tempora quo cupiditate eveniet aperiam.</p>
</div>
</div>
</div>
CSS:
.child-div{
margin: 0 -100%;
padding: 0 -100%;
}
.parent {
width: 60%;
background-color: red;
margin: 0 auto;
padding: 50px;
position:relative;
}
.grandparent {
overflow-x:hidden;
background-color: blue;
width: 100%;
position:relative;
}
The negative margins will let the content flow out of the Parent DIV. Therefore I set the padding: 0 100%;
to push the content back to the original boundaries of the Chlid DIV.
The negative margins will also make the .child-div's total width expands out of the browser's viewport, resulting in a horizontal scroll. Hence we need to clip the extruding width by applying an overflow-x: hidden
to a Grandparent DIV (which is the parent of the Parent Div):
Here is the JSfiddle
I haved tried Nils Kaspersson's left: calc(-50vw + 50%)
; it worked perfectly fine in Chrome & FF (not sure about IE yet) until I found out Safari browsers doesn't do it properly. Hope they fixed this soon as I actually like this simple method.
This also may resolve your issue where the Parent DIV element has to be position:relative
The 2 drawbacks of this workaround method is:
- Require extra markup (i.e a grandparent element (just like the good ol' table vertical align method isn't it...)
- The left and right border of the Child DIV will never show, simply because they are outside of the browser's viewport.
Please let me know if there's any issue you find with this method ;). Hope it helps.
position: relative
? What do you needposition: relative
for? I guess what I'm asking is: what are you trying to do? – thirtydot