I have 2 divs, an outer div which is a parent and a child div. The outer div is position relatives with padding 20px for left and right while the child is position absolute with 100% widthg. How can I force the child which is position absolute to be within the parent, ie respecting the padding 20px (inside parent and within the 20px padding)
I've done the example here: http://www.bootply.com/nyGZzTVY8v
I've read about box-sizing but I still don't understand how to implement it correctly. Tried putting it on the box1 class and nothing happen, tried putting on the box2 class and nothing happen also.
thanks in advance.
Additional Note: It has to be responsive ie I do not know the size of the parent div thus the 100% for the child div.
Method 2: We can make the display attribute of the child container to table-row and display attribute of parent container to table, that will take all the height available from the parent div element. To cover all the width, we can make the width of parent div to 100%.
In this case, we set the child element's width to be 100% of the viewport width by using a percentage viewport unit (vw), then, we move it to the left side (by the distance of the viewport's half, minus 50% of the width of the parent element) with the left property.
It is because absolutely positioned elements do not behave as block level elements and do not flow after each other like normal <div>does.
Set the position of div at the bottom of its container can be done using bottom, and position property. Set position value to absolute and bottom value to zero to placed a div at the bottom of container.
Just set left: 20px;
and right: 20px;
and remove width: 100%
Live Demo
.box2 {
position: absolute;
padding: 50px 0;
color: #000;
background: #fff;
left: 20px;
right: 20px;
border: solid thin #06F;
}
or add left: 20px;
and the calc function width: calc( 100% - 40px )
.box2 {
position: absolute;
width: calc( 100% - 40px );
padding: 50px 0;
color: #000;
background: #fff;
left: 20px;
border: solid thin #06F;
}
live Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With