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?
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.
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.
The width and height properties include the content, but does not include the padding, border, or margin.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With