Is there a way to have a child DIV within a parent container DIV that is wider than it's parent. The child DIV needs to be the same width of the browser viewport.
See example below:
The child DIV must stay as a child of the parent div. I know I can set arbitrary negative margins on the child div to make it wider but I can't work out how to essentially make it 100% width of the browser.
I know I can do this:
.child-div{
margin-left: -100px;
margin-right: -100px;
}
But I need the child to be the same width as the browser which is dynamic.
Update
Thanks for your answers, it seems the closest answer so far is to make the child DIV position: absolute, and set the left and right properties to 0.
The next problem I have is that the parent has position: relative, which means that left and right properties are still relative to the parent div and not the browser, see example here: jsfiddle.net/v2Tja/2
I can't remove the position relative from the parent without screwing everything else up.
Use absolute positioning
I used this:
HTML
CSS
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
CSS:
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:
Please let me know if there's any issue you find with this method ;). Hope it helps.
Then solution will be the following.
HTML
CSS
I know this is old but for anyone coming to this for an answer you would do it like so:
Overflow hidden on a element containing the parent element, such as the body.
Give your child element a width much wider than your page, and position it absolute left by -100%.
Heres an example:
Also heres a JS Fiddle: http://jsfiddle.net/v2Tja/288/