views:

17479

answers:

2

As in the subject, how can one get the total width of an element, including its border and padding, using jQuery? I've got the jQuery dimensions plugin, and running .width() on my 760px-wide, 10px padding DIV returns 760.

Perhaps I'm doing something wrong, but if my element manifests itself as 780 pixels wide and Firebug tells me that there's 10px padding on it, but calling .width() only gives 760, I'd be hard pressed to see how.

Thanks for any suggestions.

+34  A: 

The width method no longer requires the dimensions plugin, because it has been added to the jQuery Core

What you need to do is get the padding, margin and border width-values of that particular div and add them to the result of the width method

Something like this:

var theDiv = $("#theDiv");
var totalWidth = theDiv.width();
totalWidth += parseInt(theDiv.css("padding-left"), 10) + parseInt(theDiv.css("padding-right"), 10); //Total Padding Width
totalWidth += parseInt(theDiv.css("margin-left"), 10) + parseInt(theDiv.css("margin-right"), 10); //Total Margin Width
totalWidth += parseInt(theDiv.css("borderLeftWidth"), 10) + parseInt(theDiv.css("borderRightWidth"), 10); //Total Border Width

Split into multiple lines to make it more readable

That way you will always get the correct computated value, even if you change the padding or margin values from the css


[Update]

This answer was written prior to jQuery 1.3, and the functions that existed at the time where not adequate by themselves to calculate the whole width.

Now, as J-P correctly states, jQuery has the funtions outerWidth and outerHeight which include the border and padding by default, and also the margin if the first argument of the function is true

Andreas Grech
In general, I would rather believe jQuery than doing the calculation on my own. The problem is that the width() function does not actually specify what it does in case of "box-sizing: border-box". I just tested it, and it returns the inner width excluding padding etc. This may or may not be correct; different people may expect different things. So the code sample above actually works, but it may not be the most reliable way. As indicated in some other answers, I would recommend using the outerWidth() function instead. It at least promises what it is supposed to in documentation.
Jan Zich
The outerWidth/outerHeight function did not exist when I wrote this answer.
Andreas Grech
+1 outerWidth was what I was looking for.
bendewey
It'd probably be best to move your update to the top of the answer.
Conley Owens
+30  A: 

Anyone else stumbling upon this answer should note that jQuery now (>=1.3) has outerHeight/outerWidth functions to retreive the width including padding/borders, e.g.

$(elem).outerWidth(); // Returns the width + padding + borders

To include the margin as well, simply pass true:

$(elem).outerWidth( true ); // Returns the width + padding + borders + margins
J-P
I don't have edit capability, but I would change the first comment to:// Returns the width + padding + bordersAnd change the second comment to // Returns the width + padding + borders + margins
Nick Hebb
Just edited, thanks Nick!
J-P