I am having a problem using offsetWidth for different browsers. This difference in results causes some weird layouts. I created a very small example that displays what I see.
jsbin
HTML
<table id="tbl"> <tr> <td>Cell 1</td> <td>Cell 2</td> </tr> </table> <button onclick="calc()">Calculate Offset Width</button> <div>Cell 1 offsetWidth: <span id="c1offWidth"></span></div>
Js
function calc(){ document.getElementById('c1offWidth').innerHTML = document.getElementById('tbl').children[0].children[0].offsetWidth; }
CSS
Eric Meyer Reset CSS
When I run it on chrome, safari and opera, the value returned for cell offset width 1 is 72. When I run this on firefox and IE9-10, the return value is 77.
I got the impression that this was the best way to calculate the width of an element, including padding and border.
Is there a cross-browser solution that will always return the same result? I tried using jQuery, but the results were even more confusing.
EDIT Because everyone recommends outerWidth, I did jsbin too. Results still vary across browsers. Chrome returns 36; IE9-10 and Firefox return 39.
updated jsbin
Mark meyer
source share