Appendix H. CSS

Method

Description

Return Value

CSS

css(property)

Returns the specified CSS property value from the first selected element, for example:

$('div').css('background-color')

String

css(properties)

Sets the specified CSS properties. The properties argument is defined as an object literal of key, value pairs, for example:

$('div').css({
  backgroundColor: 'red',
  marginLeft: '10px'
});

jQuery

css(property, value)

Sets the specified CSS property value, for example:

$('div').css('background', 'red');

jQuery

Positioning

offset()

Returns the offset position of the first selected element relative to the viewport.

var $offset = $('div').offset();
alert('Left: ' + $offset.left);
alert('Top: ' + $offset.top);

Object

Height and Width

height()

Returns the pixel height (CSS height, excluding borders and padding) of the first selected element.

Integer

height(value)

Sets the pixel height (CSS height) of the first selected element. If no unit of measurement is provided, px (pixels) is used.

jQuery

width()

Returns the pixel width (CSS width, excluding borders and padding) of the first selected element.

Integer

width(value)

Sets the pixel width (CSS width) of the first selected element. If no unit of measurement is provided, px (pixels) is used.

jQuery

outerHeight(options)

Returns the offsetHeight (includes the pixel height, borders, and padding) of the first selected element. The options argument is a JavaScript object literal of options. See the "Options" section for more information.

Integer

outerWidth(options)

Get Beginning JavaScript® and CSS Development with jQuery now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.