Is there a way to detect if the browser has subpixel precision for elements ?
IE9, unlike any of the other major browsers, has subpixel precision for its elements (an elements width can be 50.25px) and because of that, I need to treat a thing differently.
One way is to use jQuery to detect the browser name and version, but this is deprecated in jQuery and not recommended, instead being suggested that the existence of features should be tested for not the browsers names and versions.
I’m not sure where you got the idea that IE9 is the only browser that supports fractional pixel units, but that assumption is totally incorrect.
From section 4.3 of the spec (emphasis added):
And defining <number>:
Therefore, per spec, the
pxlength unit must support fractional numbers.To prove this, take a look at this fiddle in fullscreen and use your browser’s zoom function to zoom all the way in:
In this Chrome screenshot, notice that the 5.5px blue box is indeed taller than the 5px red box.
I think the confusion might stem from the fact that the non-standard
element.clientHeightreturns a calculated (rounded) integer value, and that rounding happens differently in different browsers.In my fiddle, for the
clientHeightof the blue<div>, IE9 and Firefox 15 at 100% zoom give6. Chrome 22 and Opera 12 give5. In all browsers, the value of that property changes as the user changes the browser’s zoom level.In other words, it’s unreliable.
If you want to do calculations with the actual, fractional units of an element, use
getComputedStyle.