jQuery in Action
Changing element styling
63
Keep in mind that this variant of the
css()
command always returns a string, so if
you need a number or some other type, you'll need to parse the returned value.
For a small set of
CSS
values that are commonly accessed, jQuery thoughtfully
provides convenience commands that easily access these values and convert them
to the most commonly used types. Specifically, we can get (or set) the width and
height of an element as a number by using the convenient
to the most commonly used types. Specifically, we can get (or set) the width and
height of an element as a number by using the convenient
width()
and
height()
commands. To set the width or height:
Keep in mind that these are shortcuts for the more verbose
css()
function, so
$("div.myElements").width(500)
is identical to
$("div.myElements").css("width","500px")
Command syntax: css
css(name)
Retrieves the computed value of the CSS property specified by name for the first element in
the wrapped set
the wrapped set
Parameters
name
(String) Specifies the name of a CSS property whose computed value is to
be returned
Returns
The wrapped set
The wrapped set
Command syntax: width and height
width(value)
height(value)
Sets the width or height of all elements in the matched set
Parameters
value
(Number) The value to be set in pixels
Returns
The wrapped set
The wrapped set