Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery dimensions

jQuery dimensions


With jQuery, it is easy to work with the dimensions of elements and the browser window.


jQuery dimension methods

jQuery provides several important methods for working with dimensions:


jQuery dimensions

jQuery Dimensions


jQuery width() and height() methods

The width() method sets or returns the width of an element (not including padding, border or margin).

The height() method sets or returns the height of an element (not including padding, border or margin).

The following example returns the width and height of the specified

element:

Example

$

(

"

button

"

)

.

click

(

function

(

)

{

var

txt

=

"

"

;

txt

+=

"

The width of the div is:

"

  • $

(

"

#div1

"

)

.

width

(

)

"


"

;

txt

+=

"

The height of the div is:

"

  • $

(

"

#div1

"

)

.

height

(

)

; $

(

"

#div1

"

)

.

html

(

txt

)

;

}

)

;

Try it »


jQuery innerWidth() and innerHeight() methods

The innerWidth() method returns the width of an element (including padding).

The innerHeight() method returns the height of an element (including padding).

The following example returns the width and height of the specified

The element's inner-width/height:

Example

$

(

"

button

"

)

.

click

(

function

(

)

{

var

txt

=

"

"

;

txt

+=

"

div width, including padding:

"

  • $

(

"

#div1

"

)

.

innerWidth

(

)

"


"

;

txt

+=

"

div height, including padding:

"

  • $

(

"

#div1

"

)

.

innerHeight

(

)

; $

(

"

#div1

"

)

.

html

(

txt

)

;

}

)

;

Try it »


jQuery outerWidth() and outerHeight() methods

The outerWidth() method returns the width of an element (including padding and border).

The outerHeight() method returns the height of an element (including padding and border).

The following example returns the width and height of the specified

The element's outer-width/height:

Example

$

(

"

button

"

)

.

click

(

function

(

)

{

var

txt

=

"

"

;

txt

+=

"

div width, including padding and border:

"

  • $

(

"

#div1

"

)

.

outerWidth

(

)

"


"

;

txt

+=

"

div height, including padding and border:

"

  • $

(

"

#div1

"

)

.

outerHeight

(

)

; $

(

"

#div1

"

)

.

html

(

txt

)

;

}

)

;

Try it