How can I change the width in child to put it inside parent div. It should be internal to padding. In this case top and left are fine but right is not proper.
https://jsbin.com/tuhohuvoxa/edit?html,css,output
.parent {
padding:20px;
border: 0.1em solid;
height:200px;
}
.child{
position:absolute;
border:0.1em solid;
height:10px;
width:100%;
}
2
Answers
You just need to add this position to your parent & child element :
Then the child with position absolute will adapt to the parent.
If you do not need the child to be absolute, simply removing the
position:absolute
from the.child
class will make the width match the parent without overflow.If you really need the
.child
class to be absolute you can update the.child
and.parent
classes as follows:Setting
position:relative
on the parent will ensure the child is relative to the parent. Settingleft
andright
to 0 will ensure the child matches the parent width, and adding themargin-left
andmargin-right
will ensure the child is inset from the parent.