CSS Styles
In the previous chapter we already encountered the .css() method — we used it to change the font color, but its capabilities don't end there. Time to dig a bit deeper so we don't have to storm the forums with basic questions ;)
Let's start digging with a more thorough look at the .css() method:
get the value of a CSS property
set the value of a CSS property
set multiple values
here a callback function is used to set the value, index is the element's position in the selection, value is the current property value
Here are some usage examples:
// set the font color value
$("#my").css("color", "red")
// change the background color
$("#my").css("background-color", "yellow")To change multiple parameters, pass an object in key-value format (this is essentially JSON):
$("#my").css({
"color": "red",
"font-size": "18px",
"background-color": "white"
})For property names you can use either CSS notation (see the example above) or the JavaScript variant:
And here's an exotic way of changing the font size using a callback function:
An interactive example:
Last updated