ckersch
ckersch

Reputation: 7687

Dealing with scroll bars and jquery .width() method

jQuery's .width() method doesn't seem to account for scroll bars. This is problematic for me, since I'd like to set the width of some children to equal the width of their parent. I used jQuery similar to the following:

$('#contentDiv').width($('#containerDiv').width())

In this example, #contentDiv is the element I'd like to size, and I want to set it to have the width of #containerDiv, which is its parent element. My problem is that this cuts off the side of #contentDiv, as seen in this fiddle.

In my actual code, I have several elements that I'm sizing with jQuery, which all need to fit in the scrollable div, so just setting the css of #contentDiv to 100% is not an option. What's the best way of dealing with scroll bar widths of divs in jQuery?

Upvotes: 6

Views: 8953

Answers (4)

Michael Scheper
Michael Scheper

Reputation: 7048

Another approach I'd try is setting both elements' box-sizing property to 'border-box', and see whether setting your contentDiv's width to 100% then works the way you want.

Now that fewer projects worry about crufty old browsers anymore, 'border-box' can make things easier to work with. Be sure to test multiple browsers on multiple platforms, though, because I'm not sure they all handle scrollbars the same way.

Upvotes: 0

Michael Scheper
Michael Scheper

Reputation: 7048

Try this:

$('#contentDiv').width($('#containerDiv')[0].clientWidth)

For more information about that solution, see this StackOverflow answer.

Upvotes: 0

ckersch
ckersch

Reputation: 7687

UPDATED:

None of the jQuery width-finding methods account for the scroll bar. In my original example, using .innerWidth(true) LOOKS like it works, but only because it returns and object, which causes width to fail and the inner contents size themselves to fit in the available space, because the example wasn't very good. However, it's possible to write a function to compute the available space in a div with a scroll bar in it, which can then be used to position the contents as you wish.

To write that function, I took advantage of the fact that, when a div is appended to a div with a scroll bar in it, it takes up the full available width (i.e. the inner width of the parent minus the width of the scroll bar).

The function looks like this:

function noScrollWidth(div){
    var measureDiv = $('<div id="measureDiv">');

    div.append(measureDiv);

    var width = measureDiv.outerWidth();

    measureDiv.remove();

    return width
};

I then use this to size my content div:

$('#contentDiv').width(noScrollWidth($('#containerDiv')));

Working fiddle.

Upvotes: 0

banjoSas
banjoSas

Reputation: 194

The best solution I found while working around this solution is this:

http://chris-spittles.co.uk/?p=531

jQuery is all powerful and everything but sometimes a small dash of native JS is all you need to render pixel perfect pages... I hope you will find this solution helpful!

Upvotes: 1

Related Questions