Search code examples
jquerycsswidth

Is it possible to use jQuery to get the width of an element in percent or pixels, based on what the developer specified with CSS?


I'm writing a jQuery plugin and something I need to be able to do is determine the width of an element that the user specifies. The problem is that .width() or .css('width') will always report exact pixels, even if the developer has assigned it e.g. width:90% with CSS.

Is there any way to have jQuery output the width of an element in px or % depending on what the developer has given it with CSS?


Solution

  • I'd say the best way is to compute it yourself:

    var width = $('#someElt').width();
    var parentWidth = $('#someElt').offsetParent().width();
    var percent = 100*width/parentWidth;