Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how we get element width in decimal using jquery

Tags:

jquery

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.

like image 758
Ranjeet Avatar asked Jun 13 '26 05:06

Ranjeet


2 Answers

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

like image 92
Sadikhasan Avatar answered Jun 15 '26 11:06

Sadikhasan


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.

like image 35
Grokify Avatar answered Jun 15 '26 11:06

Grokify