0

Having a simple html code

<div id="header">
  <div id="headerBox">
    <div id="headerText">
    </div>
  </div>
</div>

with css styles

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;
}

and jQuery (2.x edge)

function resize(win) {
  var size;
  var w = $('#headerBox').width();
  $('#headerText').html('');
  $('#headerBox').css('font-size', 1 + 'px');
  $('#headerText').html(
    "width of <span style=\"font-size:2em;\">headerBox</span> element is " + w + "px");
  while ($('#headerText').width() <= 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);

all together accessible via this fiddle,

I experience incorrect jQuery element width calculation. When you access the above fiddle, you see that headerText element is too wide. There should be same right padding as is on left side, text should be centered. Once you resize the Result window (in the fiddle), text is adjusted as supposed to.

Question is WHY there is incorrect calculation on the very first time?

It seems that var w = $('#headerBox').width(); is incorrect calculated. WHY?


enter image description here

7
  • Perhaps you wanted outerWidth() instead? api.jquery.com/outerWidth Commented Dec 8, 2013 at 13:38
  • 1
    The jsFiddle displays correctly for me in Firefox. Commented Dec 8, 2013 at 13:38
  • 1
    @A.Wolff - I am using Chrome 31.0.1650.63m and it does not display it correctly Commented Dec 8, 2013 at 13:41
  • strange, i'm using same chrome version Commented Dec 8, 2013 at 13:42
  • @A.Wolff - close it, open again and then resize the Result window in the fiddle - you see no difference? Commented Dec 8, 2013 at 13:44

2 Answers 2

1

JQuery .width doesn't include the padding of #headerbox. Use .outerWidth to get correct width.

var w = $('#headerBox').outerWidth();
Sign up to request clarification or add additional context in comments.

1 Comment

-1 :: This is not an answer to my problem. Sure .width() does not include padding. I know that. I don't want padding to be included in the calculation.
1

Found the problem:

Due to padding: 1em; for headerBox, width of this element changes with change of font-size.

So in while loop I need to work with up-to-date information, not the one I stored at the beginning.

Therefore

var w = $('#headerBox').width();
...
while ($('#headerText').width() <= w) {
  ...
}

needs to be changed to

while ($('#headerText').width() <= $('#headerBox').width()) {
  ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.