English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery CSS() Method

jQuery css()The method retrieves or sets one or more style properties of the selected element.

This method provides a quick way to apply styles (inline styles) directly to HTML elements.

Return CSS property value

To return the value of a specified CSS property, use the following syntax:

$(selector).css(property)

The following example returns the background color of the clicked DIV:

$("div").click(function(){
  $("this").css("background"-color");
});
Test See‹/›

Set CSS property and value

To set a specified CSS property and value, use the following syntax:

$(selector).css(property, value)

The following example sets the color attribute of all paragraphs:

$("button").click(function(){
  $("p").css("color", "blue");
});
Test See‹/›

When usingcss()MethodGetWhen setting property values, it will returnThe First Selected Elementvalue.

Usingcss()MethodSetWhen setting property values, it will beAll Selected ElementsSetting One or More Properties/Value pairs.

Setting Multiple CSS Properties and Values

To set multiple CSS properties and values, use the following syntax:

$("selector").css({property:value, property:value, ...})

The following example sets multiple CSS properties and values for all paragraphs:

$("button").click(function(){
  $("p").css({
    "color": "white",
    "font"-size": ""1.3em",
    "background"-"color": "#"4285f4",
    "padding": ""20px"  
  });
});
Test See‹/›

jQuery CSS Reference

For a complete reference of CSS methods, please visit ourjQuery HTML / CSS Reference.