Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a DIV padding without affecting the width/height ?

Tags:

html

css

I have a div that I want to specify a FIXED width and height for, and also a padding which can be changed without decreasing the original DIV width/height or increasing it, is there a CSS trick for that, or an alternative using padding?

like image 907
CodeOverload Avatar asked Feb 08 '10 23:02

CodeOverload


People also ask

Does padding affect width?

Normally, when an element's size is set, the width and height properties determine the width and height of the element's content box. Any padding added to the element will increase the total computed width and/or height of the element—this is how the default box model works in regards to sizing the element.

Does padding add to the width of an element?

Padding and Element Width The content area is the portion inside the padding, border, and margin of an element (the box model). So, if an element has a specified width, the padding added to that element will be added to the total width of the element.

Does width 100% include padding?

The width and height properties include the content, but does not include the padding, border, or margin.

Does margin increase width?

If your box has a width of '100%' and also has margin, border and padding, they will affect (increase) the width occupied by the object.


1 Answers

Declare this in your CSS and you should be good:

* {      -moz-box-sizing: border-box;      -webkit-box-sizing: border-box;       box-sizing: border-box;  } 

This solution can be implemented without using additional wrappers.

This will force the browser to calculate the width according to the "outer"-width of the div, it means the padding will be subtracted from the width.

like image 139
adswebwork Avatar answered Oct 18 '22 22:10

adswebwork