title: Incorrect jQuery element width calculation
body: <p>Having a simple <strong>html</strong> code</p>
  
  <pre><code>&lt;div id="header"&gt;
    &lt;div id="headerBox"&gt;
      &lt;div id="headerText"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>with <strong>css</strong> styles</p>
  
  <pre><code>div#header {
      display: block;
      overflow: hidden;    
      margin: 0;
      padding: 0;
      width: 100%;
  }
  div#headerBox {
      padding: 1em;
      text-align: center;
      white-space: nowrap;
      border-bottom: 10px double gray;
  }
  div#headerText {
      background: red;
      display: inline-block;
  }
  </code></pre>
  
  <p>and <strong>jQuery</strong> (2.x edge)</p>
  
  <pre><code>function resize(win) {
    var size;
    var w = $('#headerBox').width();
    $('#headerText').html('');
    $('#headerBox').css('font-size', 1 + 'px');
    $('#headerText').html(
      "width of &lt;span style=\"font-size:2em;\"&gt;headerBox&lt;/span&gt; element is " + w + "px");
    while ($('#headerText').width() &lt;= w) {
      size = parseInt($('#headerBox').css('font-size').replace(/\D+$/, ''), 10);
      $('#headerBox').css('font-size', (size + 1) + 'px');
    }
    $('#headerBox').css('font-size', size + 'px');
  }
  
  $(window).resize(function(e){
    resize(this)
  });
  
  resize(window);
  </code></pre>
  
  <p>all together accessible via <strong><a href="http://jsfiddle.net/J5rkq/" rel="nofollow noreferrer">this fiddle</a></strong>,</p>
  
  <p>I experience <strong>incorrect jQuery element width calculation</strong>. When you access the above fiddle, you see that <code>headerText</code> element is too wide. There should be same right padding as is on left side, text should be centered. Once you <strong>resize</strong> the <strong>Result</strong> window (in the fiddle), text is adjusted as supposed to.</p>
  
  <p>Question is <strong>WHY</strong> there is incorrect calculation on the very first time?</p>
  
  <p>It seems that <code>var w = $('#headerBox').width();</code> is incorrect calculated. <strong>WHY?</strong></p>
  
  <hr>
  
  <p><img src="https://i.stack.imgur.com/huWil.png" alt="enter image description here"></p>