Home > Article > Web Front-end > jQuery’s css() method usage guide_jquery
Definition and usage
css() method returns or sets one or more style attributes of the matched element.
Return CSS property value
Returns the CSS property value of the first matching element.
Note: Shorthand CSS properties (such as "background" and "border") are not supported when used to return a value.
name required. Specifies the name of the CSS property. This parameter can contain any CSS property. For example "color".
Example
Get the value of the color style attribute of the first paragraph:
Set CSS properties
Sets the specified CSS property on all matching elements.
name required. Specifies the name of the CSS property. This parameter can contain any CSS property, such as "color".
value optional. Specifies the value of a CSS property. This parameter can contain any CSS property value, such as "red".
If the empty string value is set, removes the specified attribute from the element.
Example
Set the color of all paragraphs to red:
Use functions to set CSS properties
Set the value of the style attribute in all matching elements.
This function returns the property value to be set. Accepts two parameters, index is the index position of the element in the object collection, and value is the original attribute value.
name required. Specifies the name of the CSS property. This parameter can contain any CSS property, such as "color".
Specifies a function that returns the new value of a CSS property.
index - optional. Accepts the index position of the selector
oldvalue - optional. Accepts the current value of a CSS property.
Example 1
Set the color of all paragraphs to red:
Example 2
Gradually increase the width of the div:
Set multiple CSS property/value pairs
Sets the name/value pair object as the style attribute of all matching elements.
This is the best way to set a large number of style properties on all matching elements.
{property:value}
Required. Specifies the name/value pair object to be set as a style attribute.
This parameter can contain several CSS property name/value pairs. For example {"color":"red","font-weight":"bold"}
Example
The above is the entire content of this article, I hope you all like it.