The reason for your problem is a strange behaviour of Firefox and Safari, thats clearly against the W3C specification of getComputedStyle() and should be reported as a bug.
You have set the margin
of your #content to: 0px auto
. Thats a common way to center a blocklevel element with given width in its containing block. Browsers take the width of the parent, subtract the width of the the element and divide by two. The resulting value is is used for marginLeft and marginRight and should be returned as computedValue for both properties. After calculation the element gets rendered (correctly centered). But Firefox and Safari see the keyword auto and set computedStyle to 0px (the default value for auto) before doing calculation and rendering.
Don't believe you can always rely on the values other browsers return. At that moment the parent or grandparent itself have some margin or padding or the boxSizing property is changed somewhere, they get confused and return wrong values even when the rendering is correct.
If you try to detect such cases by looking for the (shorthand) property margin
, you'll fail again: Firefox, InternetExplorer and Safari answere with silence (empty string). Chrome and Opera return the margin shorthand, but the values there are often different from what you get with marginLeft and marginRight.
A workaround for marginLeft
uses the difference between the left offset of the element and its parent (or its offset-parent, when element has position: 'absolute'). Refering to your code it looks like:
$(document).ready(function () {
var con = $("#content"), cpos = con.css('position'), par, w;
if (cpos === 'static' || cpos === 'relative') par = con.parent();
else par = con.offsetParent();
w = con.offset().left - par.offset().left;
$("#wrapper").css('width', w);
});
I can not guarantee that it works in all usecases, but I havn't found problems until now.
Working JSFiddle here. Now even Firefox and safari gets a width for the sidebar.
Second JSFiddle. Here you can toggle between using
marginLeft and offsetLeft for a quick browser test.
Since there is no offset right, the workaround for marginRight needs some more calculations.If you need it, post a comment and I will extend my answer.