<style>
.parent {
width: 300px;
height: 300px;
border: 1px solid red;
padding: 20px;
}
.child {
box-sizing: border-box;
margin: 20px;
width: 100%;
height: 100%;
border: 1px solid blue;
}
</style>
</head>
<body>
<div id="app">
<div class="parent">
<div class="child"></div>
</div>
</div>
</body>
I have a child div nested inside parent div which has a padding attribute. Child div has a margin attribute as well. I expected child div to be in the center like the image below.
however, when I run code, child div is skewed to the right bottom.
I set box-sizing
attribute to border-box to calculate margin: 20px
into the final width and height yet the result is the same. my question is 1)how do I center child div with margin applied 2)why border-box
does not have any effects on child div?