2

I am getting the width of element in integer rather than in fraction because my element width is 49.358 px. But i am getting 50px using

$('lichildren').width()

where liChildren is my variable please suggest me any alternate for this issue.

Mox Shah
  • 2,967
  • 2
  • 26
  • 42
Ranjeet
  • 21
  • 1
  • 4
  • 2
    When an element is shown on screen, the browser rounds the size to the nearest pixel - because the smallest unit of measurement on a monitor is a pixel. 50px is probably what the width of the element is when displayed, so the variable is correct – Ben Mar 20 '15 at 09:34

2 Answers2

15

Reference Question Use the native Element.getBoundingClientRect rather than the style of the element. It was introduced in IE4 and is supported by all browsers:

$("#lichildren")[0].getBoundingClientRect().width

Demo

Community
  • 1
  • 1
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
2

See the following answer to a similar question:

Getting the actual, floating-point width of an element

The accepted answer starts by saying:

If you already have a reference to the DOM element, element.getBoundingClientRect will get you the values you want.

Community
  • 1
  • 1
Grokify
  • 15,092
  • 6
  • 60
  • 81