Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery how to get innerWidth but without the padding?

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?

like image 477
Chris Avatar asked Sep 27 '11 10:09

Chris


People also ask

How to get element innerHeight in jQuery?

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).

What does innerWidth () method do in jQuery?

The innerWidth() method returns the inner width of the FIRST matched element.

What is clientHeight in jQuery?

The clientHeight property returns the viewable height of an element in pixels, including padding, but not the border, scrollbar or margin.

Does jQuery width include padding?

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.


2 Answers

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

like image 180
Ben Avatar answered Sep 18 '22 15:09

Ben


$('#element').width(); 

It's as simple as that!

like image 44
daryl Avatar answered Sep 21 '22 15:09

daryl