7

I'm rewritting my Textarea Line Count plugin (<-- shameless plug), and have this question:

When I call $("#someElement").css("letter-spacing"), will I ever get a value in ems, or anything other than px? Based on this example: http://jsfiddle.net/xhhr2/, in Google Chrome at least, it appears that either jQuery or the browser is converting the measurement to px for me. Can I always expect this behavior?

Community
  • 1
  • 1
Chris Laplante
  • 29,338
  • 17
  • 103
  • 134

2 Answers2

6

According to Crescent Fresh's answer that links to a hack by Dean Edwards, jQuery goes through great lengths to return the actual, computed pixel value across all browsers and not what was defined originally in the style sheet, so it seems that yes, you can rely on it.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 1
    Indeed, look what I found: http://struckthrough.krystophv.org/tag/curcss/. This method was deprecated long ago, but it did use to return `1em` instead of converting it, for example. – Chris Laplante Apr 21 '11 at 16:45
5

As far as I know, the browser has to convert every size you give it (whether it's in em, %. etc) into pixels. That's how the DOM stores it, and thus jQuery will return that value to you.

EDIT According to this answer here, (referenced by Pekka's answer) only IE supports the currentStyle, which gives the size (including the unit) that was set by CSS. Apparently, no other browser supports this, instead they all use computed style which converts everything into pixels. So I was about half-right. :P

Community
  • 1
  • 1
Dominic Barnes
  • 28,083
  • 8
  • 65
  • 90
  • Can you give some proof of some kind? – Levi Morrison Apr 21 '11 at 16:37
  • 2
    Makes sense. I'd be pissed if jQuery (or the browser for that matter) returned any measurement in `%` :) – Chris Laplante Apr 21 '11 at 16:37
  • I can't find it right now but I seem to remember coming across an SO question that mentioned that the actual string from the CSS including the original units (`95%` or `1.2em`) would be returned with older versions of jQuery (I'm not sure but I'm going to say <=1.2) but not anymore. – no.good.at.coding Apr 21 '11 at 16:39
  • I just tried my little jsFiddle example with `pt`, and the measurement is indeed converted into `px` as well. `%` didn't do anything; I guess `letter-spacing` can't be expressed as a percentage. – Chris Laplante Apr 21 '11 at 16:40
  • Thats true for computed values. I don't use jQuery but I think there is also the possibility to fetch both the computed and the non computed value. The API for jQuery.css() and search results on Google say that it's only returns the value of the CSS property from the stylesheet (e.g.: css('line-height') => 'normal'). – Johnny Apr 21 '11 at 16:45
  • 1
    [Here it is](http://stackoverflow.com/questions/5475589/jquery-css-function-not-returning-expected-values) – no.good.at.coding Apr 21 '11 at 16:45
  • I don't know if this is true. I just did a bunch of stuff with `font-size`, and using `currentStyle` in IE returned values in `pt` by default, and `em` if ems were used to set the size. Even worse, if no size is specifically used, Opera returns a font-size of `0em` – sdleihssirhc Apr 21 '11 at 16:48