Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 565
ckersch Avatar asked Apr 12 '13 16:04

ckersch


1 Answers

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!

like image 125
banjoSas Avatar answered Oct 01 '22 02:10

banjoSas