div within another occupy 100% of the width of the container and position absolute

1

hello guys I'm sure this question is easy I have 2 divs one is 450px wide and inside that I have another div that I want to occupy 100% of width and have an absolute position, but when you place position absolute you get of the container here my code

.padre{
 
 width: 200px;
 height: 400px;
 background-color:green;

}
.hijo{
 
 width: 100%;
 height:200px;
 background-color: grey;
 position:absolute;
 
}
<div class="padre">
   
<div class="hijo">

</div>     
  
</div>

as you will see the son div leaves the container what I need is that I always occupy the same width of the father div no matter what I hope you can help me thanks!

    
asked by andy gibbs 09.07.2018 в 23:58
source

1 answer

1

You can add position: relative to the parent div like this:

.padre{
 
 width: 200px;
 height: 400px;
 background-color:green;
 position: relative;

}
.hijo{
 
 width: 100%;
 height:200px;
 background-color: grey;
 position:absolute;
 
}
<div class="padre">
   
<div class="hijo">

</div>     
  
</div>
    
answered by 10.07.2018 / 00:08
source