Guys ive been asking around and no one can really tell me the benefits
of using .css(‘width’) and .css(‘height’)rather then .width() and
.height().
Querying CSS will give you the applied value as in 5px
or 1em
or what ever size is assigned to it.
Using jQuery you can use the following methods:
.height
(element height, without padding , margins or borders)
.innerHeight
(element height + padding)
.outerHeight
(element height + padding and borders)
.outerHeight(true)
(element height + padding + borders + margin)
.width
(element width, without padding , margins or borders)
.innerWidth
(element width + padding)
.outerWidth
(element width + padding and borders)
.outerWidth(true)
(element width + padding + borders + margin)
All of those methods will return just a number, representing the height/width units in pixels.
The benefit using jQuery is across browser you are able to be more in
control of the exact width/height by being able to ignore/include
margins, paddings or borders.
Furthermore, as those methods always return the units as a plain
number it is easier to calculate with them as previously mentioned as
you don't need to worry about the measurement itself, i.e: px
, em
,
etc..
DEMO - Demonstrating height methods (same applies to width)
The demo above is using a div:
<div id="myDiv">My Div</div>
With the following CSS:
div{
border: 1px solid blue;
padding: 5px;
margin: 10px;
}
Using this script:
var $div = $("#myDiv");
var height = $div.height();
var heightWithPadding = $div.innerHeight();
var heightWithPaddingAndBorder = $div.outerHeight();
var heightWithPaddingAndBorderAndMargin = $div.outerHeight(true);
var $result = $("#result");
$result.append("height: " + height);
$result.append("<br />height with padding: " + heightWithPadding);
$result.append("<br />height with padding and borders: " + heightWithPaddingAndBorder);
$result.append("<br />height with padding and borders and margin: " + heightWithPaddingAndBorderAndMargin);
Resulting in the following:
height: 20
height with padding: 30
height with padding and borders: 32
height with padding and borders and margin: 52