From the docs, innerWidth does almost what I need:
"Gets the inner width (excludes the border and includes the padding) for the first matched element."
I need to know the width excluding the padding. I.e. the usable space inside the element.
Does jquery provide anything like this - done a bit of googling and can't find any solutions.
I thought about getting the padding-left
and padding-right
values to subtract from inner width- But given these could be percentages, pixels or em I'm not sure if this would be reliable.
Any suggestions?
jQuery innerHeight() Method width() - Sets or returns the width of an element. height() - Sets or returns the height of an element. innerWidth() - Returns the width of an element (includes padding) outerWidth() - Returns the width of an element (includes padding and border).
The innerWidth() method returns the inner width of the FIRST matched element.
The clientHeight property returns the viewable height of an element in pixels, including padding, but not the border, scrollbar or margin.
jQuery outerWidth() Method The outerWidth() method returns the outer width of the FIRST matched element. As the image below illustrates, this method includes padding and border.
You're probably just looking for the width() function..
See the docs, it excludes the padding: http://api.jquery.com/width/
(Just as intended in modern browser's representation of the css width property)
Edit: It's now 2012 and jQuery 1.8 is just coming out. While this is still relevant, you may also want to read the following article from the jQuery blog regarding box-sizing
considerations in version 1.8
$('#element').width();
It's as simple as that!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With