0

I justed noticed jQuery's width() gets different results in current chrome and firefox.
Im targeting a li element, in the inspector it shows me a width of 108.531px
Since jQuery's width() outputs an integer it should be 109.. But in Chrome this resorts to 108, Firefox logs 109..

I tried it with these attributes:

border: none;
outline: none;
box-sizing: border-box;

I found this post, but am uncertain if this still is state of the art:

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

How to make jQuery to not round value returned by .width()?

Why does jQuery have this inconsistency?

Community
  • 1
  • 1
Jan Benda
  • 152
  • 10
  • What are you trying to achieve? – guest271314 May 22 '16 at 19:51
  • im computing the total width of a horizontal navigation, so in short: for each li > totalWidth += li.width()` – Jan Benda May 22 '16 at 19:54
  • Does `.getBoundingClientRect().width` not return expected result? – guest271314 May 22 '16 at 19:56
  • yes it does. i just dont feel comfortable using it since i dont know exactly what it does.. and even then: why would i ever use jquerys width() if it is unreliable? – Jan Benda May 22 '16 at 19:57
  • If you do not need to use `.width()`, then do not use it? See https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect – guest271314 May 22 '16 at 19:58
  • thanks for the reference! Wouldnt it make sense for jquery to use the getBoundingClientRect architecture, since it outputs the right value? btw: how do you mark code in a comment here? didnt find it in the help – Jan Benda May 22 '16 at 20:02
  • _"Wouldnt it make sense for jquery to use the getBoundingClientRect architecture, since it outputs the right value?"_ See https://github.com/jquery/jquery/issues/1724 . _"btw: how do you mark code in a comment here? "_ Surround code with backtick `` characters . You can review source for `.width()` at http://james.padolsey.com/jquery/#v=1.11.2&fn=jQuery.fn.width – guest271314 May 22 '16 at 20:06

0 Answers0