Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I expand a child div to 100% screen width if the container div is smaller?

The parent element of the whole page is a centered div limited to a max-width of 960px. All other elements on the page are children of that parent div. The simplified structure is the following:

<div id="parent">   <div id="something"></div>   <div id="wide-div"></div>   <div id="something-else"></div> </div> 

While the parent div shouldn't expand beyond a width of 960px, the div I called "wide-div" here should fill the entire width of the screen. It contains a single image that is wider than the 960px, and it should set a different background color for the entire width of the screen.

I can't easily take that div out of the parent div, it would mess up other parts of my layout and it would make the whole thing rather awkward.

I found a few tricks on how you can achieve this, but none seemed to fit my requirements. My design is responsive, or at least I'm trying to achieve that. The tricks I found relied on knowing the size of the involved elements, which is not fixed in my case.

Is there a way to expand the inner div to the full screen width in a responsive layout?

like image 719
Mad Scientist Avatar asked Jul 13 '15 19:07

Mad Scientist


People also ask

How do I make my kids div full width?

Method 2: We can make the display attribute of the child container to table-row and display attribute of parent container to table, that will take all the height available from the parent div element. To cover all the width, we can make the width of parent div to 100%.

How do you make a child div wider than a parent div?

In this case, we set the child element's width to be 100% of the viewport width by using a percentage viewport unit (vw), then, we move it to the left side (by the distance of the viewport's half, minus 50% of the width of the parent element) with the left property.

How do I create a 100% screen width div inside a container in bootstrap?

If you want to make it stretch 100% to the screen either you make the "full-width-div" position fixed or use the "container-fluid" class instead of "container".

How to set the height of container div to 100% of window?

Answer: Set the style rule "height:100%;" for parents too. If you will try the set the height of container div to 100% of the browser window using height: 100%; it doesn't work, because the percentage (%) is a relative unit so the resulting height is depends on the height of parent element's height.

How to make a child <div> element wider than the parent element?

Making a child <div> element wider than the parent <div> can be done with some CSS properties. In the example below, we set the position of the parent <div> to “static”, which means that elements are placed according to the document’s normal flow. Here, we add the width of the parent container and specify its background-color and margin as well.

How do I set the width of a fixed width Div?

You can set the width based on the vw (viewport width). You can use that value too using the calc function, to calculate a left-margin for the div. This way you can position it inside the flow, but still sticking out on the left and right side of the centered fixed-width div.

What is the maximum width of a Div in HTML?

The parent element of the whole page is a centered div limited to a max-width of 960px. All other elements on the page are children of that parent div. The simplified structure is the following: While the parent div shouldn't expand beyond a width of 960px, the div I called "wide-div" here should fill the entire width of the screen.


2 Answers

You can set the width based on the vw (viewport width). You can use that value too using the calc function, to calculate a left-margin for the div. This way you can position it inside the flow, but still sticking out on the left and right side of the centered fixed-width div.

Support is pretty good. vw is supported by all major browsers, including IE9+. The same goes for calc(). If you need to support IE8 or Opera Mini, you're out of luck with this method.

-edit-

As mentioned in the comments, when the content of the page is higher than the screen, this will result in a horizontal scrollbar. You can suppress that scrollbar using body {overflow-x: hidden;}. It would be nice though to solve it in a different way, but a solution using left and rightlike presented in Width:100% without scrollbars doesn't work in this situation.

-edit 2021-

Another work-around for the scrollbars, which may be acceptable or not depending on your situation:
By making the green div a little bit smaller, say 20px, you can keep a bit of space for the scrollbar. Half that reserved width can be added to the margin, to keep the wide div centered:

#wide-div {   width: calc(100vw - 20px);   margin-left: calc(-50vw + 50% + 10px); 

div {   min-height: 40px;   box-sizing: border-box; } #container {   position: relative; } #parent {   width: 400px;   border: 1px solid black;   margin: 0 auto; }  #something {   border: 2px solid red; }  #wide-div {   width: calc(100vw - 20px);   margin-left: calc(-50vw + 50% + 10px);   border: 2px solid green; }
<div id="container"> <div id="parent">   <div id="something">Red</div>   <div id="wide-div">Green   <br>Green <br>Green <br>Green <br>Green <br>Green <br>Green <br>Green </div>   <div id="something-else">Other content, which is not behind Green as you can see.</div> </div> </div>
like image 194
GolezTrol Avatar answered Sep 28 '22 02:09

GolezTrol


After much research, I found this solution: Creating full width (100% ) container inside fixed width container. I think that it is the best solution because it does not depend on any external factor, only the div that you want to expand.

<div class="container" style="width: 750px; margin: 0 auto;">    <div class="row-full">      --- Full width container ---    </div>    </div>  .row-full{      width: 100vw;      position: relative;      margin-left: -50vw;      left: 50%; } 
like image 44
crico_aven Avatar answered Sep 28 '22 02:09

crico_aven