337

Is there a way to have a child DIV within a parent container DIV that is wider than its parent. The child DIV needs to be the same width as the browser viewport.

See example below: enter image description here

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.

3
  • Your requirements don't really make sense. The "child div" must stay as a child of the parent div, and yet the parent div has position: relative? What do you need position: relative for? I guess what I'm asking is: what are you trying to do?
    – thirtydot
    Commented Apr 7, 2011 at 21:28
  • @Camsoft Did you see my comment on my answer? That works for me, Also check my website edocuments.co.uk which does what you are trying to do in a different way
    – Blowsie
    Commented Apr 8, 2011 at 14:41
  • 1
    @Camsoft in addition , there really should be no need for any jquery / js in your solution
    – Blowsie
    Commented Apr 8, 2011 at 14:42

17 Answers 17

367

Here's a generic solution that keeps the child element in the document flow:

.child {
  width: 100vw;
  position: relative;
  left: calc(-50vw + 50%);
}

We set the width of the child element to fill the entire viewport width, then we make it meet the edge of the screen by moving it to the left by a distance of half the viewport, minus 50% of the parent element's width.

Demo:

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  overflow-x: hidden;
}

.parent {
  max-width: 400px;
  margin: 0 auto;
  padding: 1rem;
  position: relative;
  background-color: darkgrey;
}

.child {
  width: 100vw;
  position: relative;
  left: calc(-50vw + 50%);

  height: 100px;
  border: 3px solid red;
  background-color: lightgrey;
}
<div class="parent">
  Pre
  <div class="child">Child</div>
  Post
</div>

Browser support for vw and for calc() can generally be seen as IE9 and newer.

Note: This assumes the box model is set to border-box. Without border-box, you would also have to subtract paddings and borders, making this solution a mess.

Note: It is encouraged to hide horizontal overflow of your scrolling container, as certain browsers may choose to display a horizontal scrollbar despite there being no overflow.

15
  • 4
    What do you know, it does indeed work in IE9. +1 Nice
    – CatShoes
    Commented Jul 29, 2014 at 18:24
  • 14
    This was exactly what I was looking for thank you very mutch. IMO this is a better answer than the accepted one since this one doesn't break document flow
    – Rémi
    Commented Dec 14, 2014 at 15:11
  • 23
    vw is no good. If you have a vertical scrollbar then 100vw will give you a horizontal scrollbar.
    – Sunny
    Commented Jan 26, 2015 at 13:29
  • 2
    It seems that this only works one level down. Is there a way for it to work regardless of how many parent elements a child element has? Commented Aug 14, 2015 at 15:12
  • 3
    This is the right answer to wrap wider child element into a position: relative parent!
    – Hanfei Sun
    Commented Sep 24, 2015 at 4:45
140

Use absolute positioning

.child-div {
    position:absolute;
    left:0;
    right:0;
}
7
  • 14
    Ok, next question, what if the parent or another ancestor has layout i.e. position: relative, see: jsfiddle.net/v2Tja/2
    – Camsoft
    Commented Apr 7, 2011 at 14:00
  • 1
    how about another parent div being position:static, and another div inside with position:relative? jsfiddle.net/blowsie/v2Tja/3
    – Blowsie
    Commented Apr 7, 2011 at 15:20
  • 11
    is there a way to do this, whith the .child-div's height auto and still have the correct placement below? Commented Apr 2, 2013 at 19:28
  • 2
    and don't set the parent div to "overflow:hidden" ^^
    – chris
    Commented Nov 5, 2013 at 10:35
  • 1
    Best answer. No scrollbar problems. High cross-browser compatibility. Thanks!!!
    – jMike
    Commented Sep 20, 2022 at 18:02
27

I've searched far and wide for a solution to this problem for a long time. Ideally we want to have the child greater than the parent, but without knowing the constraints of the parent in advance.

And I finally found a brilliant generic answer here. Copying it verbatim:

The idea here is: push the container to the exact middle of the browser window with left: 50%;, then pull it back to the left edge with negative -50vw margin.

.child-div {
  width: 100vw;
  position: relative;
  left: 50%;
  right: 50%;
  margin-left: -50vw;
  margin-right: -50vw;
}
2
  • 1
    I found if I want it to be, say, 90% of the viewport width, I can use the above but set width to 90vw. Also, it appears that margin-right has no effect in any case. Commented Oct 31, 2017 at 17:45
  • had to change the margin-left and right values cause my wrapper is 80% of viewport width. So in my case it had to be margin-left:-10vw and margin-right:-10vw and then it perfectly worked! thanks! Commented Jul 12, 2019 at 18:53
8

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.

0
6

Flexbox can be used to make a child wider than its parent with three lines of CSS.

Only the child’s display, margin-left and width need to be set. margin-left depends on the child’s width. The formula is:

margin-left: calc(-.5 * var(--child-width) + 50%);

CSS variables can be used to avoid manually calculating the left margin.

Demo #1: Manual calculation

.parent {
  background-color: aqua;
  height: 50vh;
  margin-left: auto;
  margin-right: auto;
  width: 50vw;
}

.child {
  background-color: pink;
  display: flex;
}

.wide {
  margin-left: calc(-37.5vw + 50%);
  width: 75vw;
}

.full {
  margin-left: calc(-50vw + 50%);
  width: 100vw;
}
<div class="parent">
  <div>
    parent
  </div>
  <div class="child wide">
    75vw
  </div>
  <div class="child full">
    100vw
  </div>
</div>

Demo #2: Using CSS variables

.parent {
  background-color: aqua;
  height: 50vh;
  margin-left: auto;
  margin-right: auto;
  width: 50vw;
}

.child {
  background-color: pink;
  display: flex;
  margin-left: calc(-.5 * var(--child-width) + 50%);
  width: var(--child-width);
}

.wide {
  --child-width: 75vw;
}

.full {
  --child-width: 100vw;
}
<div class="parent">
  <div>
    parent
  </div>
  <div class="child wide">
    75vw
  </div>
  <div class="child full">
    100vw
  </div>
</div>

1
  • This one works best for me since I wanted to maintain position: sticky; on the child. Also I wanted to get to an actual pixel value, not vw, so in my case, the actual pixels were 850px, and I set child's display: flex; margin-left: -50px; width: 850px;
    – Danny
    Commented Apr 9 at 10:17
4

I used this:

HTML

<div class="container">
 <div class="parent">
  <div class="child">
   <div class="inner-container"></div>
  </div>
 </div>
</div>

CSS

.container {
  overflow-x: hidden;
}

.child {
  position: relative;
  width: 200%;
  left: -50%;
}

.inner-container{
  max-width: 1179px;
  margin:0 auto;
}
3

I tried some of your solutions. This one :

margin: 0px -100%;
padding: 0 100%;

is by far the best, since we don't need extra css for smaller screen.

2

I had a similar issue. The content of the child element was supposed to stay in the parent element while the background had to extend the full viewport width.

I resolved this issue by making the child element position: relative and adding a pseudo element (:before) to it with position: absolute; top: 0; bottom: 0; width: 4000px; left: -1000px;. The pseudo element stays behind the actual child as a pseudo background element. This works in all browsers (even IE8+ and Safari 6+ - don't have the possibility to test older versions).

Small example fiddle: http://jsfiddle.net/vccv39j9/

2
  • This seems to work, but it causes the browser to show a horizontal scrolbar since the width is 4000px. What's the workaround to keep the pseudo element within the viewport's width? Commented Jul 10, 2014 at 3:18
  • Of course you have to set the body or the page wrapping div to overflow-x: hidden.
    – Seika85
    Commented Oct 13, 2014 at 13:46
2

To make child div as wide as the content, try this:

.child{
    position:absolute;
    left:0;
    overflow:visible;
    white-space:nowrap;
}
1

you can try position: absolute. and give width and height , top: 'y axis from the top' and left: 'x-axis'

1

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:

body {
  overflow:hidden;
}

.parent{
  width: 960px;
  background-color: red;
  margin: 0 auto;
  position: relative;    
}

.child {
  height: 200px;
  position: absolute;
  left: -100%;
  width:9999999px;
}

Also heres a JS Fiddle: http://jsfiddle.net/v2Tja/288/

1

Adding to Nils Kaspersson's solution, I am resolving for the width of the vertical scrollbar as well. I am using 16px as an example, which is subtracted from the view-port width. This will avoid the horizontal scrollbar from appearing.

width: calc(100vw - 16px);
left: calc(-1 * (((100vw - 16px) - 100%) / 2));
3
  • 1
    I believe the 16px which you need to subtract because it is causing the horizontal scrollbar is the default margin/padding of the browser, instead of subtracting it just use this html, body{ margin:0; padding:0 } thus yo uwon't need to make extra calculation for the 16px Commented Feb 6, 2016 at 19:51
  • This is when the page content is longer and goes below the fold and the scroll bar appears automatically. Of course, when content is within the fold, this is not needed. I add the 16px to the formula only when I know the page content is going to be long and the scroll bar would indeed appear.
    – A-Zone
    Commented Feb 8, 2016 at 6:41
  • i think it won't work on mobile. mobile has no scrollbar.
    – hjchin
    Commented Dec 29, 2016 at 1:42
1

Assuming the parent has a fixed width, e.g #page { width: 1000px; } and is centered #page { margin: auto; }, you want the child to fit the width of browser viewport you could simply do:

#page {
    margin: auto;
    width: 1000px;
}

.fullwidth {
    margin-left: calc(500px - 50vw); /* 500px is half of the parent's 1000px */
    width: 100vw;
}
1

A lot of great solutions in this thread. Here is a 2024 solution that:

  • does not use position: absolute; so the child stays in the document flow
  • supports a maximum width for extra wide screens
  • uses css custom-properties, min(), max(), and calc() for maximum flexibility and responsiveness
  • supports rtl mode

CodePen: https://codepen.io/ryanfitzer/pen/YzbNZBe

Demo:

/*
  Required Styles
*/
:root {
  --width-wrapper: 15.625rem; /* 250px */
  --width-expand: 31.25rem; /* 500px */
  --width-expand-responsive: min(100vw, var(--width-expand));
  --width-expand-half: calc(var(--width-expand) * 0.5);
  --width-expand-quarter: calc(var(--width-expand) * 0.25);
  --width-wrapper-expand-diff-half: calc((var(--width-expand-half) - var(--width-wrapper)) / 2);
  --left-expand: max(calc(-50vw + 50%), calc(var(--width-expand-quarter) * -1) - var(--width-wrapper-expand-diff-half));
}

#wrapper {
  max-width: var(--width-wrapper);
}
#expand {
  position: relative;
  box-sizing: border-box;
  width: var(--width-expand-responsive);
  inset-inline-start: var(--left-expand);
}

/*
  Demo Styles
*/
body {
  margin: 0;
  padding-block: 20px;
  background: lightgrey;
}
#wrapper {
  color: #010101;
  font-size: 18px;
  padding-top: 10px;
  text-align: center;
  background: yellow;
  margin-inline: auto;
  border: 2px solid #000;
  font-family: sans-serif;
}
#expand {
  padding-top: 10px;
  background: #ff000054;
  border: 2px solid #000;
}
.child {
  height: 20vh;
  margin: 20px;
  padding: 10px;
  align-content: center;
  background: lightgreen;
  border: 2px solid #000;
}
<div id="wrapper" class="text">
  Parent Container
  <div class="child">Normal Container</div>
  <div id="expand" class="text">
    Expandable Container
    <div class="child">Normal Container</div>
  </div>
  <div class="child">Normal Container</div>
</div>

0
.parent {
    margin:0 auto;
    width:700px;
    border:2px solid red;
}
.child {
    position:absolute;
    width:100%;
    border:2px solid blue;
    left:0;
    top:200px;
}
1
  • Clever, but doesn't seem to work (with spans embedded within li's, at least).
    – ruffin
    Commented Nov 21, 2012 at 14:57
0

Then solution will be the following.

HTML

<div class="parent">
    <div class="child"></div>
</div>

CSS

.parent{
        width: 960px;
        margin: auto;
        height: 100vh
    }
    .child{
        position: absolute;
        width: 100vw
    }
1
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Commented Aug 2, 2018 at 15:17
0

body {
  margin: 0;
  overflow-x: hidden;
  /* This ensures that any content overflowing the viewport horizontally is hidden */
}

.parent-container {
  position: relative;
  width: 100%;
  max-width: 1200px;
  /* Set a maximum width for the parent container if desired */
  margin: 0 auto;
  /* Center the parent container */
  background-color: #f0f0f0;
}

.wider-child {
  position: absolute;
  top: 0;
  left: 50%;
  /* Position the left edge at the center of the viewport */
  transform: translateX(-50%);
  /* Adjust to center the child div */
  width: 100vw;
  /* 100% of the viewport width */
  height: 200px;
  /* Adjust the height as needed */
  background-color: #3498db;
  color: #fff;
  text-align: center;
  padding: 20px;
}
<div class="parent-container">
  <div class="wider-child">
    <p>This is a wider child div.</p>
  </div>
  <!-- Other content within the parent container goes here -->
</div>

Not the answer you're looking for? Browse other questions tagged or ask your own question.