95

I've searched around and couldn't find this. I'm trying to get the width of a div, but if it has a decimal point it rounds the number.

Example:

#container{
    background: blue;
    width: 543.5px;
    height: 20px;
    margin: 0;
    padding: 0;
}

If I do $('#container').width(); it will return 543 instead of 543.5. How do I get it to not round the number and return the full 543.5 (or whatever number it is).

Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
MoDFoX
  • 2,134
  • 2
  • 21
  • 22
  • 2
    Why are you having decimal pixel widths? They get rounded anyway to whole numbers. You can't have half a pixel. – Moin Zaman Aug 30 '10 at 18:26
  • Why do you need this? Without that info, this question is pointless. – Ruan Mendes Apr 18 '13 at 00:24
  • 2
    @JuanMendes In my case, on a HiDPI display, Chrome 38 computes a non-integer window width. So, if $.width rounds up, say, 1250.6 to 1251, and I make calculations based on 1251, I have problems. Rounding down isn't as much of an issue. – JKS Oct 27 '14 at 23:41

5 Answers5

200

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

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

Note: For IE8 and below, see the "Browser Compatibility" notes in the MDN docs.

$("#log").html(
  $("#container")[0].getBoundingClientRect().width
);
#container {
    background: blue;
    width: 543.5px;
    height: 20px;
    margin: 0;
    padding: 0;
}
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="container"></div>
<p id="log"></p>
Ross Allen
  • 43,772
  • 14
  • 97
  • 95
  • 9
    +1 If you are setting the width of another element, be sure to change its width this way: $("#other").css("width", $('#container').get(0).getBoundingClientRect().width + "px"); If you use jQuery width() the value will be rounded. – lepe Mar 11 '14 at 03:24
  • 1
    According to this similar question the `width` and `height` properties are not supported in all versions of IE, so in some cases they need to be calculated before they can be used: http://stackoverflow.com/questions/11907514/getting-the-actual-floating-point-width-of-an-element – flyingL123 Jun 05 '15 at 21:51
  • Nice! same question for outerWidth(true)? :) small remark: ` In IE8 and below, the DOMRect object returned by getBoundingClientRect() lacks height and width properties. Also, additional properties (including height and width) cannot be added onto these DOMRect objects.` – TecHunter Aug 26 '15 at 07:27
  • 5
    Note that `getBoundingClientRect()` calculates the dimensions **after applying CSS transformations**. – user1620220 Jul 19 '16 at 14:15
  • After testing I found that this method does not work for hidden elements (`display: none`). – Jory Hogeveen Nov 07 '17 at 17:55
10

Ross Allen's answer is a good starting point but using getBoundingClientRect().width will also include the padding and the border width which ain't the case the the jquery's width function:

The returned TextRectangle object includes the padding, scrollbar, and the border, but excludes the margin. In Internet Explorer, the coordinates of the bounding rectangle include the top and left borders of the client area.

If your intent is to get the width value with the precision, you'll have to remove the padding and the border like this:

var a = $("#container");
var width = a[0].getBoundingClientRect().width;

//Remove the padding width (assumming padding are px values)
width -= (parseInt(a.css("padding-left")) + parseInt(a.css("padding-right")));

//Remove the border width
width -= (a.outerWidth(false) - a.innerWidth());
Community
  • 1
  • 1
The_Black_Smurf
  • 5,178
  • 14
  • 52
  • 78
  • You shouldn't the `.replace("px", "")` as `parseInt` should remove that for you. – Steve Schrab Aug 17 '16 at 19:29
  • This question being all about preventing rounding, I'm bemused by the explicit assumption here that rounding is fine for the padding values. Why would you treat that any differently to width values?! Use `parseFloat()` perhaps? – phils Oct 09 '18 at 05:14
  • Similarly, with `innerWidth()` being part of the original problem, I fear that the border width calculation here is also working on rounded values. – phils Oct 09 '18 at 05:16
9

Just wanted to add my experience here, though the question's old: The consensus above seems to be that jQuery's rounding is effectively just as good as an unrounded calculation -- but that doesn't seem to be the case in something I've been doing.

My element has a fluid width, generally, but content that changes dynamically via AJAX. Before switching the content, I temporarily lock the dimensions of the element so my layout doesn't bounce around during the transition. I've found that using jQuery like this:

$element.width($element.width());

is causing some funniness, like there are sub-pixel differences between the actual width and the calculated width. (Specifically, I will see a word jump from one line of text to another, indicating the the width has been changed, not just locked.) From another question -- Getting the actual, floating-point width of an element -- I found out that window.getComputedStyle(element).width will return an unrounded calculation. So I changed the above code to something like

var e = document.getElementById('element');
$('#element').width(window.getComputedStyle(e).width);

And with THAT code -- no funny bouncing! That experience seems to suggest that the unrounded value actually does matter to the browser, right? (In my case, Chrome Version 26.0.1410.65.)

Community
  • 1
  • 1
davidtheclark
  • 4,666
  • 6
  • 32
  • 42
  • Current doc for jQuery's .css() points out that getComputedStyle() is called runtimeStyle() i IE, and claims one of the advantages of .css() is this unified interface. – norwebian Aug 19 '13 at 14:33
2

You can use getComputedStyle for it:

parseFloat(window.getComputedStyle($('#container').get(0)).width)
Anton Chukanov
  • 645
  • 5
  • 20
-1

Use the following to get an accurate width:

var htmlElement=$('class or id');
var temp=htmlElement[0].style.width;
JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
Ali Hasan
  • 174
  • 1
  • 17
  • why would you evne want jquery to get your computed width if you actually set it and have it available in `style.width` – user151496 Jul 06 '20 at 09:15