Reference: jQuery css method

Gets or sets one or more styles for the selected elements.

Get a style

Parameters

  1. name of the style property

Returns

Value of the style property

Syntax

$(selector).css("propertyName")

Example

Get the background color of the first div.

var color = $("div").css("background-color");

Set a style

Parameters

  1. name of the style property
  2. value of the style property

Syntax

$(selector).css("propertyName", "value");

Example

Set the background color of the div elements to blue.

$("div").css("background-color", "blue");

Set multiple styles

Parameters

  1. JSON Object containing the property style and value pairs to set.

Syntax

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

Example

Set the height and width to 100px, and background color of the div elements to blue.

$("div").css({ 
    height: "100px", 
    width: "100px", 
    "background-color": "blue" 
});
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License