The CSS3 box-sizing: border-box
property is probably exactly what you want:
#mainbox {
background-color:#111111;
padding:10px;
margin:0 auto;
width:90px;
border:solid 1px;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
By default, width
only applies to the content, and margin
and padding
is added after the fact. So if your width
is 90px
, and you add a padding of 10px
, your div
will be 100px
.
The box-sizing
property in CSS3 makes the browser include border
, margin
, and padding
with width
, making math a little easier, especially when you don't know the widths of everything beforehand.
Note: the padding-box
property is not widely supported.