At the moment, I have div2 inside div1 and both have the same size. I would like to add borders/margins to div2 but not reduce the size of div2 and instead expand size of div1.
Any idea how to do so?
At the moment, I have div2 inside div1 and both have the same size. I would like to add borders/margins to div2 but not reduce the size of div2 and instead expand size of div1.
Any idea how to do so?
The outer container do not need a width
and height
property. If this is the case the outer container (.div1
) will expand if a child element (.div2
) has margin
. See the code example for better understanding :)
.div1 {
border: 4px solid #111;
width: auto; /* remove width */
height: auto; /* remove height */
display: inline-block; /* make it more flexible */
}
.div2 {
width: 100px;
height: 100px;
background: cyan;
}
.div2.with-margin {
margin: 20px;
}
<div class="div1">
<div class="div2"></div>
</div>
<div class="div1">
<div class="div2 with-margin"></div>
</div>