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?
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 right
like presented in Width:100% without scrollbars doesn't work in this situation.
div {
min-height: 40px;
box-sizing: border-box;
}
#parent {
width: 400px;
border: 1px solid black;
margin: 0 auto;
}
#something {
border: 2px solid red;
}
#wide-div {
width: 100vw;
margin-left: calc(-50vw + 50%);
border: 2px solid green;
}
<div id="parent">
<div id="something">Red</div>
<div id="wide-div">Green</div>
<div id="something-else">Other content, which is not behind Green as you can see.</div>
</div>
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%;
}
Typically the responsive element, bootstrap or Foundation, allow you to add a "row" element. You can put the "wide-div" outside an element with "row" and it should expand to take up the full width.
Alternatively, you can use absolute positioning for that element which ignores most inherited settings:
.wide-div {
position: absolute;
left: 0;
right: 0;
}
You can now do this
.full-width {
margin-left: calc(50% - 50vw);
margin-right: calc(50% - 50vw);
}
or this
.full-width {
width: 100vw;
position: relative;
left: 50%;
right: 50%;
margin-left: -50vw;
margin-right: -50vw;
}
More details: https://css-tricks.com/full-width-containers-limited-width-parents/
You can use vw
. Demo http://jsfiddle.net/fsLhm6pk/
.parent {
width: 200px;
height: 200px;
background: red;
}
.child {
width: 100vw;
height: 50px;
background: yellow;
}
<div class='parent'>
<div class='child'></div>
</div>
You are right, this won't work with centered div
. Try this instead:
EDIT http://jsfiddle.net/fsLhm6pk/1/
.parent {
width: 200px;
height: 200px;
background: red;
margin: 0 auto;
}
.child {
width: 100%;
left: 0;
right: 0;
position: absolute;
height: 50px;
background: yellow;
}
<div class='parent'>
<div class='child'></div>
</div>
I'm a little surprised no one offered the following in the last 4 years. The css position:fixed
property pulls the item out and scales it in relation to the window. There are some cases where maybe this doesn't work, but if you're Javascripting a modal box or something, this works fine.
.wide-div{
position:fixed;
top:0px;
left:0px;
width:100%;
height:100%; // in case you need to cover the height as well
background-color:rgba(0,0,0,.8); //just so you can see the div is on top of your content
z-index:1; // you may need to adjust the index of your other elements as well
}
ReferenceURL : https://stackoverflow.com/questions/31391459/how-can-i-expand-a-child-div-to-100-screen-width-if-the-container-div-is-smalle
'Programing' 카테고리의 다른 글
SQL Server RODBC Connection (0) | 2020.12.24 |
---|---|
Maven plugins can not be found in IntelliJ (0) | 2020.12.24 |
Remove last 3 characters of string or number in javascript (0) | 2020.12.24 |
Why would one mark local variables and method parameters as “final” in Java? (0) | 2020.12.24 |
ASP.NET MVC ViewResult vs PartialViewResult (0) | 2020.12.24 |