The size increase of any element on a WEB page is due to the use of the padding, the padding is nothing more than the internal space of the element, ie when it applies a padding of 50px
, the element will be increased by 50px para top, rigth, bottom e left.
There are two ways to maintain the element ratio when adding a padding, I’ll be quoting them below:
First
Choose a final value for my div 300px por 100px
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, so it will be worth 400, because 50 top and 50 right base 400, the same is valid for the height, then we must subtract 50 from the value we desire, in this case we want a div com 300px e 100px
, will look 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>
According to
box-sizing: border-box
;which is a style applied to the elements, maintaining the original ratio of each, thus not readjusting its size with the padding.
Observing: It is noteworthy that the border: 1px solid black
, consumes 1px on each side of the element, ie the width becomes 302px;
Would be a
div
internal? Is there any other element?– William Aparecido Brandino
Remember that
padding
adds internal spacing to the element, whilemargim
external. Both maintain the dimensions defined bywidth
andheight
and add spacing. If usedmargim
and inspect, you will see that the dimensions will be maintained.– Woss
@Andersoncarloswoss I understand about, I even have a question here about your inquiry :)
– Marconi
So I don’t know if I understand this question. You want the value set in
padding
is subtracted fromwidth
andheight
? If so, it will be necessary to do it manually by settingwidth: 200px
, because with thepadding: 50px
, the total will be of300px
.– Woss
@Andersoncarloswoss I’ll edit, maybe I’m not expressing myself well.
– Marconi
@Andersoncarloswoss edited.:)
– Marconi