I have a fixed width div. Within that div I have a div that needs to be full width of the page. So I need to somehow break the full width div out of the fixed width div.
Ive tried css hack which I found online, but that adds a horizontal scrollbar to the browser! So I thought Id turn to jquery.
I can add html (via jquery) before and after my full width div by using before() and after() on my full width div. HOWEVER, I cant seem to add an ending div (which ends my fixed with div) and then open it again after my full width div.
I thought the same thing too. Itâs definitely a strange setup. Have you tried using the z-index property. I donât know too much but itâs worth a try. If you find a solution please do let us know. Iâm interested in getting the full scoop of how this getâs solved. You can also check out this tutorial - https://www.tutorialspoint.com/How-to-replace-innerHTML-of-a-div-using-jQuery It focuses on manipulating content within a div tag.
You can give the inner div a 100vw width and then it will be 100% viewport width but of course if your parent div is not at the left edge of the viewport the 100% will push out of the right side of the viewport. Also the parent would need to be overflow:visible or the overflow is cut off.
It would be better if you could create a stand alone demo so we can see what we are dealing with. There may be other options depending what it is exactly you need to achieve.
e.g.
Of course it may still not be possible without a change of mark up.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="stylesheet" href="css/stylesheet.css">
<title>Inner div to 100vw</title>
<!--
https://codepen.io/paulobrien/pen/zQXeXK
PaulOB (plus my mod)
-->
<style>
html {
box-sizing: border-box;
}
*, *::before, *::after {
box-sizing:inherit;
}
html,body {
padding:0;
margin:0;
}
.parent {
max-width:600px;
border:1px solid red;
background:orange;
padding:10px;
margin:10px;
}
.child {
width:calc(100vw - 42px); /* changed from - 30px */
background:rgba(0,0,0,0.5);
border:1px solid #fcc;
padding:10px;
}
</style>
</head>
<body>
<div class="parent">
<div class="child">
<p>I am a child div that will stretch 100% across the viewport even though the parent div is only 300px wide. Of course I will lay over the top of anything in my way that is outside the parent. I am a child div that will stretch 100% across the viewport even though the parend div is only 300px wide. Of course I will lay over the top of anything in my way that is outside the parent. </p>
</div>
</div>
</body>
</html>
I was dabbling with PaulOBâs code before reading your last post. I donât think it will help. I will give it another try. Can you please tell me what the smallest break point is?
Hmmm. OK. Well, your demo seems to be using a breakpoint at 760px. Can I assume that that is the smallest breakpoint before which the child div will stretch outside of the parent div? (Iâm not sure that Iâm following the logic of the layout, but bear with me for one more attempt.)
Can you post the CDN for the bootstrap version that you are using?
Thanks for your help @ronpat
Bootstrap version is: <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
Im not sure if Im understanding correctly. The parent div should always be a set width. (Bootstrap is specifying this width as 1170px. On resolutions less than 1170px the contents should start stacking on tiop of each other in the usual responsive manner). On the child div it should ALWAYS be 100% of the browser width.
The demo, https://codepen.io/dl292/pen/MdRRKr, shows that the child is 100% of the parentâs width at widths larger than 760px. At 760px and narrower the parent becomes 100% of the browser window width and the child becomes about 40px narrower so it fits within the parent so that the parent appears to have borders around the child.
Are you trying to insert some markup that you can use because none exists?
Are you rather than inserting an empty div trying to wrap a div around some existing content? (which begs the question as to why you canât use the original mark up anyway).
It will add a horizontal scrollbar for likely 2 reasons.The first is that the VW unit includes the scrollbar so if you have vertical scrolling the element will be too big (this is a big problem with the VW unit and negates much of its usefulness). The relative positioning may also incur a scrollbar even though you move it back out of the way. I would reverse the procedure to avoid that issue.
The 18px is the average scrollbar width but of course on browsers that don;t show the scrollbar (as in mac systems) you will get a gap.
The only other solution would be to hide the overflow on the body to avoid the scrollbar but thatâs a dangerous thing to do unless you have very tight control over everything.
Yes, the parent is centered. I followed my interpretation of the demo (thus the fixed widths greater than 760px), whereas @PaulOB followed your description. My money is on Paul, but neither one seems to make sense without the rest of the page (which is why we ask for âworking pagesâ to start with), but what the heck
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<title>Inner div to 100% v2</title>
<style>
html {
box-sizing: border-box;
}
*, *::before, *::after {
box-sizing:inherit;
}
html,body {
padding:0;
margin:0;
}
.outer { /* ADDED */
width:1170px;
margin:0 auto;
}
.parent {
background:orange;
padding:10px;
margin:12px;
}
.child {
width:calc(100% + 21px);
background:rgba(0,0,0,0.5);
border:1px solid #fcc;
padding:10px;
}
@media screen and (max-width:1170px) {
.outer {
width:990px;
}
}
@media screen and (max-width:990px) {
.outer {
width:760px;
}
}
@media screen and (max-width:760px) {
.parent {
margin:12px 0;
}
.outer,
.child {
width:auto;
}
}
</style>
</head>
<body>
<div class="outer">
<div class="parent">
<div class="child">
<p>I am a child div that will stretch 100% across the viewport even though the parent div is only 300px wide. Of course I will lay over the top of anything in my way that is outside the parent. I am a child div that will stretch 100% across the viewport even though the parend div is only 300px wide. Of course I will lay over the top of anything in my way that is outside the parent. </p>
</div>
</div>
</div>
</body>
</html>
<div class="fixed-width container">
<p>some content in my fixed width container</p>
<div class="full-width container">
<p>full width content should be in here</p>
</div>
<p>this is some more content in my fixed width container</p>
</div>
I need to change the html to the below (using Jquery or - somehow - CSS)
<div class="fixed-width container">
<p>some content in my fixed width container</p>
</div>
<div class="full-width container">
<p>full width content should be in here</p>
</div>
<div class="fixed-width container">
<p>this is some more content in my fixed width container</p>
</div>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="css/stylesheet.css">
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<title>Inner div 100vw, parent's width stepped</title>
<!--
https://www.sitepoint.com/community/t/using-jquery-to-end-an-existing-div-before-another-div/329871
bolton
https://codepen.io/paulobrien/pen/yWWLXJ
PaulOB (with bolton's change, sorta)
-->
<style>
.fixed-width {background:tan;}
.full-width {background:gold;}
.full-width {
width:calc(100vw - 18px);
position:relative;
right:50%;
margin-right:calc(-50vw + 9px);
}
</style>
</head>
<body>
<div class="fixed-width container">
<p>one sentence in a fixed width container</p>
</div>
<div class="full-width container">
<p>full width content should be in here</p>
</div>
<div class="fixed-width container">
<p>one sentence in a fixed width container</p>
</div>
<div class="fixed-width container">
<p>one sentence in a fixed width container</p>
</div>
<div class="fixed-width container">
<p>this sentence plus the following sentences are in one fixed width container.</p>
<p>in other words, it works either way.</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
<p>this is some more content in my fixed width container</p>
</div>
<script>
/*
$( ".full-width" ).before('</div>');
// the above line doesnt work! :(
$( ".full-width" ).after('<div class="fixed-width container">');
*/
</script>
</body>
</html>
They are indeed within fixed-width âdivsâ. The last div contains a bunch of "p"s just to fill space. All you have to do is put the fixed width divs around the individual "p"s if you want them individually contained⌠or just copy and paste the last âdiv with pâ as many times as desired and delete the groupie âdivâ with the hoard of "p"s.