Increasing the size of any element in a WEB page is due to the use of padding , the padding is nothing more than the inner space of the element, ie when padding of 50px
, the element will suffer an increase of 50px para top, rigth, bottom e left.
There are two ways to keep the aspect ratio when adding a padding, I'll be quoting below:
First
Choose an ending value for my div 300px por 100px
by defining the internal spacing, taking into account that the spacing will increase on all sides the main element, ie if the element is 300 wide, then it will be worth 400, since 50 of top and 50 of base right 400, the same is valid for the height, then we must subtract 50 of the value we want, in this case we want a div com 300px e 100px
, it will be like this.
div.d1 {
width: 200px;
height: 0px;
/*É necessário por o height como 0px, pois se deixa vazio, ele assumirá um valor automático, e estragará o trabalho, eu deixei 0px
pois ele assume 50 de padding, o que já soma 100px da original*/
padding: 50px;
border: 1px solid black;
}
div.d2 {
width: 300px;
height: 100px;
box-sizing: border-box;
padding: 50px;
border: 1px solid red;
}
<div class="d1">Ajsutando manualmente.</div>
<br>
<div class="d2"> Com Border-Box </div>
<br>
Second
box-sizing: border-box
, which is a style applied to elements, maintaining the original aspect ratio of each, thus not re-adjusting its size with padding.
Note: Note that border: 1px solid black
consumes 1px on each side of the element, ie the width becomes 302px;