0

I'm trying to achieve a scenario in my project wherein I have a parent div which has a margin of some pixels from left and right and then I have a child div inside it. I want the child div to take 100% of the width of the screen.

P.S.: The position of the parent is relative.
I do not want to use negative margins on child div.
Child divs are inside shadowRoot whereas parent div is outside it
I cannot apply position: fixed to child as it will fix it WRT window and I do not want that.

Here is the sample code:

.full-width {
    position: absolute;
    width: 100%;
    left: 0;
    border: 10px red solid
}

.container {
    position: relative;
    max-width: 1440px;
    border: 10px black solid; 
    height:50vh
  
<div class="container">
    <div class="full-width"></div>
</div>  
Prasheel
  • 985
  • 5
  • 22

3 Answers3

0

I hope that I understood your request but the only way is to remove position: relative on your .full-width div

Refer to Is there are way to make a child DIV's width wider than the parent DIV using CSS?

https://jsfiddle.net/83gvjsea/

Rémy Testa
  • 897
  • 1
  • 11
  • 24
0

I believe that you might be looking for a FIXED position instead of ABSOLUTE. Sorry for the colors, but it makes it easy to see how fixed works. However, this might disrupt other features of the site.

Other than this I believe what you are trying to do goes against how layouts are built and how positioning works in the current HTML/CSS web.

body{
  background-color:fuchsia;
}
.full-width {
    position: fixed;
    left:0;
    right:0;
    background-color:yellow;
}

.container {
    position: relative;
    height:200px;
    width:140px;
    background-color:#ddd;
    max-width: 140px;
    padding: 0 10px;
}
<div class="container">
    <div class="full-width">
    hello
    </div>
</div> 
Andrew Adam
  • 1,572
  • 9
  • 22
0

<div class="container">
    <div class="full-width"></div>
</div>  

.full-width { position: absolute; width: calc(100% - 20px); left: 0; border: 10px red solid } .container { position: relative; max-width: 1440px; border: 10px black solid; height:50vh}
<div class="container">
    <div class="full-width"></div>
</div> 

You can give the width of the child container with a calculation based on parent container border width.

.full-width {
 position: absolute;
 width: calc(100% - 20px);
 left: 0;
 border: 10px red solid
}
Silu K
  • 238
  • 2
  • 13