views:

444

answers:

1

Is it possible to detect the computed font-size of a DOM element, taking into consideration generic settings made elsewhere (In the body tag for example), inherited values, and so on?

A framework-independent approach would be nice, as I'm working on a script that should work standalone, but that is not a requirement of course.

Background: I'm trying to tweak CKEditor's font selector plugin (source here) so that it always shows the font size of the current cursor position (as opposed to only when within a span that has an explicit font-size set, which is the current behaviour).

+9  A: 

You could try to use the non-standard IE element.currentStyle property, otherwise you can look for the DOM Level 2 standard getComputedStyle method if available :

function getStyle(el,styleProp) {
  var camelize = function (str) {
    return str.replace(/\-(\w)/g, function(str, letter){
      return letter.toUpperCase();
    });
  };

  if (el.currentStyle) {
    return el.currentStyle[camelize(styleProp)];
  } else if (document.defaultView && document.defaultView.getComputedStyle) {
    return document.defaultView.getComputedStyle(el,null)
                               .getPropertyValue(styleProp);
  } else {
    return el.style[camelize(styleProp)]; 
  }
}

Usage:

var element = document.getElementById('elementId');
getStyle(element, 'font-size');

More info:

Edit: Thanks to @Crescent Fresh, @kangax and @Pekka for the comments.

Changes:

  • Added camelize function, since properties containing hypens, like font-size, must be accessed as camelCase (eg.: fontSize) on the currentStyle IE object.
  • Checking the existence of document.defaultView before accessing getComputedStyle.
  • Added last case, when el.currentStyle and getComputedStyle are not available, get the inline CSS property via element.style.
CMS
This looks great, will try it out.
Pekka
Be careful, `currentStyle` and `getComputedStyle` are fundamentally different: http://erik.eae.net/archives/2007/07/27/18.54.15/ `getComputedStyle` is incapable of getting the *inherited* style, and always *computes* it as `px`, whether the value was declared as `px` or not.
Crescent Fresh
Thanks for the caveat. Very good to know. For the job at hand it's fine, as stated above.
Pekka
It's actually `document.defaultView.getComputedStyle`, not `window.getComputedStyle` (MDC gets it wrong, or considers its own — Mozilla — implementation only). `getComputedStyle` is specified to be a member of `AbstractView`, which `document.defaultView` implements, and there's really no guarantee that `window == document.defaultView` (neither in specs, nor in practice; in fact, Safari 2.x is a good example of `window != document.defaultView`). It's interesting that PPK does similar mistake in getstyles article that you linked to (testing `defaultView` on `window`).
kangax
Thanks a lot for your help folks. I got it working with Firefox but am stuck with IE. If you'd like to take a look: http://stackoverflow.com/questions/1956573/runtimestyle-in-ie7-8
Pekka
Sorted. @CMS, if I'm not mistaken you need to edit your answer to point out that `currentStyle` will accept camelized properties only (e.g. fontSize) while `getPropertyValue` works with the original form (font-size).
Pekka
Many thanks for the feedback guys, edited to check `document.defaultView` instead of `window` as @kangax points out, also added a `camelize` function to get the right properties when working with `el.currentStyle`, any further feedback is really appreciated!
CMS
Well, it would also be a good idea to check existence of `document.defaultView` before accessing `getComputedStyle` on it.
kangax
Does someone know why this doesn't work with jQuery ? if I replace var element = document.getElementById('elementId');getStyle(element, 'font-size');by getStyle($('#elementId'), 'font-size') there is a javascript error...
Vinze
@Vinze: that's because `$('#elementId')` returns a jQuery object while CMS's `getStyle` function expects a DOM element. Use `getStyle($('#elementId')[0], 'font-size')` instead.
Tim Down