[ACCEPTED]-Get element width in px-css
How many items have the class
myElement? Consider 11 using an
id, not a
class, as getting the width 10 of two elements is not really possible (or 9 logically understandable IMO).
I made a little 8 demo, and for me, it outputs the width in 7 pixels for a single
span element with a width 6 of
100% (for me, it alerts something around 5
By the way,
<span> element's can't have a 4 set width or height, so setting their width 3 and height does you no good. Instead, display 2 them as a
block element (so just replace
<span> with 1
<div>, or add
display: block; to the CSS of
.css('width') should return 100%, however 3 .width() should (as described here http://api.jquery.com/width/) return 2 a unit-less pixel amount. I created a jsfiddle 1 to demonstrate: http://jsfiddle.net/yxCav/
From the jQuery documentation on width()
The difference 9 between .css(width) and .width() is that 8 the latter returns a unit-less pixel value 7 (for example, 400) while the former returns 6 a value with units intact (for example, 400px). The 5 .width() method is recommended when an element's 4 width needs to be used in a mathematical 3 calculation.
So it returns pixels if the 2 pixel width is defined, but you are defining 1 the width in %
This worked for me:
I have experienced like in this post: jQuery width() not returning correct value on a div with generated content. If 5 the div is not displayed earlier or are 4 loading content dynamically, the .width() function 3 returns the percentage value, but till the 2 element is fully loaded the same .width() function 1 will return width in pixels.
More Related questions