dragmosh dragmosh - 1 year ago 60
CSS Question

Setting width of an element is adding 5%

I've been trying to set the relative width of an element using jQuery's width function, but it keeps adding 5% to whatever value I enter.

For example doing:

sets the width to 15%.

$('.class').css('width', '10%')
works properly, so I'm using that for now.

Answer Source

The jQuery .width() method sets the width of the content of an element, not including border or padding, you probably need .outerWidth() method which includes border and padding as well.

Another solution is to set box-sizing: border-box; on your element, which forces .width() method to include border and padding.

See the official API docs of .width() and .outerWidth() for more info.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download