Iâ€™ve just read last week that Firefox would implement calc() in their new version of Firefox. Itâ€™s not out in any publicly released version yet but it is coming as they talked about it on their developers blog.
Why should you care?
Ever had an element that needed to have a percentage width with padding? Up until now, because of the way the box model works in
modern browsers, you had to wrap the content in a container to which youâ€™d apply the padding. Thatâ€™s because when you define padding to an element the value is added to the width, which is the proper way of doing it.
Now the thing is that percentage width donâ€™t mix well with fixed padding values, that where calc() comes into play.
Lets say you have a box with the following css rules:
If that box is in a container 1000 pixels wide, the box would be 640 pixels wide total, thatâ€™s not what you want, thatâ€™s not 60% wide! You coud use calc() to achieve the desired result:
width: -moz-calc(60% - 40px);
You can even be wild and do basic calculations in there:
width: -moz-calc(60% - 2 * 20px);
Taking it further
What would be even cooler is if you could get the actual padding values as a variable in calc(). Something like:
width: -moz-calc(60% - (padding-left + padding-right));
This way you could even forget about updating your width value when you play with your padding.
Oh well, one day maybe, now letâ€™s just see if other browser vendors implement something of the like. Letâ€™s not forget this is not part of the W3C CSS3 specs.
Is it a feature you were waiting for?