.css( propertyName )Returns: String
Description: Get the value of style properties for the first element in the set of matched elements.
version added: 1.0.css( propertyName )
- propertyNameType: StringA CSS property.
version added: 1.9.css( propertyNames )
- propertyNamesType: ArrayAn array of one or more CSS properties.
The .css()
method is a convenient way to get a style property from the first matched element, especially in light of the different ways browsers access most of those properties (the getComputedStyle()
method in standards-based browsers versus the currentStyle
and runtimeStyle
properties in Internet Explorer) and the different terms browsers use for certain properties. For example, Internet Explorer's DOM implementation refers to the float
property as styleFloat
, while W3C standards-compliant browsers refer to it as cssFloat
. For consistency, you can simply use "float"
, and jQuery will translate it to the correct value for each browser.
Also, jQuery can equally interpret the CSS and DOM formatting of multiple-word properties. For example, jQuery understands and returns the correct value for both .css('background-color')
and .css('backgroundColor')
. Different browsers may return CSS color values that are logically but not textually equal, e.g., #FFF, #ffffff, and rgb(255,255,255).
Shorthand CSS properties (e.g. margin, background, border) are not supported. For example, if you want to retrieve the rendered margin, use: $(elem).css('marginTop')
and $(elem).css('marginRight')
, and so on.
As of jQuery 1.9, passing an array of style properties to .css()
will result in an object of property-value pairs.
Examples:
Example: Get the background color of a clicked div.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Example: Get the width, height, text color, and background color of a clicked div.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|