jQuery CSS Method
Notes:
jQuery CSS() Method:
The jQuery CSS() method is used to set one or more CSS style(s) to the selected HTML element(s). It is also used to get the value of a CSS style applied to the selected HTML element(s).
Setting the single CSS style or property:
Syntax:
$(selector).css(“css-property”,”value”);
It helps us to select any html element(s) targeted by the given selector and
apply the given CSS property.
Ex:
$(“#firstp”).css(“background-color”,”red”);
It selects any html element(s) whose id attribute value is set to firstp and
applies the CSS property background color to red .
Setting multiple CSS styles or properties:
Syntax:
$(selector).css({“css-property1”:”value1”,” css-property2”:”value2”,….});
It helps us to select any html element(s) targeted by the given selector and
apply the given set of CSS properties.
Ex:
$(“#firstp”).css({“background-color”:”green”,”color”:”white”});
It selects any html element(s) whose id attribute value is set to firstp and
applies the CSS properties background color green and text color white .
Getting the value of a single CSS style or property:
Syntax:
$(selector).css(“css property”);
It helps us to select any html element(s) targeted by the given selector and
return the value of a given CSS property.
Ex:
alert($(“#firstp”).css(“background-color”));
It selects any html element(s) whose id attribute value is set to firstp and
returns the value of background-color CSS property.
The current implementation of the CSS() method in jQuery allows us to set multiple CSS properties to the selected HTML element(s), but only allows us to get one CSS property at a time.
Interview Questions: