Introduction to jQuery
JQuery Syntax
JQuery Effects
JQuery HTML
JQuery Traversing
The .css()
method is used to get or set one or more CSS properties of selected elements.
jQuery .css()
method is one of the most powerful and convenient tools for manipulating styles directly with JavaScript
To return (get) a CSS property value using jQuery’s .css()
method, use the following syntax:
$(selector).css("property-name");
he following example will return the color value of the div element:
Try it yourself
To set a CSS property in jQuery, you use the .css()
method and provide the property name and value as arguments.
$(selector).css("property-name", "value");
The following example will set the background-color value for selected elements:
Try it yourself
jQuery makes it super easy to set multiple CSS properties at once using the .css()
method with an object.
css({"propertyname":"value","propertyname":"value",...});
The following example will set a background-color and a font-size for selected elements:
Try it yourself
Note
Use camelCase for properties if you prefer (e.g., fontSize
instead of font-size
) — both work: