I need to extend a div beyond the parent, only to the right and all the way to the edge of the browser.
The code below shows it working with a percentage width (60%), but change the max-width to something like 360 (something smaller than what the percentage would give you) and it fails.
I’m ok with using JS if necessary but css only is obviously preferred.
Thanks!
body {
margin: 0;
padding: 0;
color: white;
background: grey;
}
.container {}
.center {
width: 60%;
max-width: 3600px;
margin: 0 auto;
background: red;
}
.image-container {
width: 80vw;
height: 300px;
background-image: url("https://picsum.photos/1200/400");
object-fit: cover;
}
<div class="container">
<div class="center">
<p>Banner image snould start where the text starts and extend beyond the parent container all the way to the edge of the browser.</p>
<p>Width set to 60%.</p>
<p>Change the max-width and it no longer works.</p>
<div class="image-container"></div>
<p>More content after that stuff above.</p>
<p>More content after that stuff above.</p>
<p>More content after that stuff above.</p>
</div>
</div>
3
Answers
I had to do this recently, I provide you the code of a simple example only using CSS if it helps you.
In this case I extended and overflowed to the right everything that is inside the .card-container element
Simply add the following to your CSS:
The issue with this code is that the container div, the parent of the center div, has no specified width. This means the center div takes up 100% of the container’s width.
To achieve the desired effect of extending the center div beyond the parent container to the right edge of the browser, you can try using the "position" property with "left" and "right" values to position the center div outside of the container div.