← Back to Index

.css()

Get the computed style properties for the first element in the set of matched elements or set one or more CSS properties for every matched element.

Examples

1. Get property

const color = $('p').css('color');

Result

"rgb(0, 0, 0)"

2. Set property

$('.box').css('background-color', 'red');

Result

<div class="box" style="background-color: red;"></div>

3. Set multiple properties

$('.box').css({
     'width': 100,
     'height': '100px',
     'opacity': 0.5
});

Result

<div class="box" style="width: 100px; height: 100px; opacity: 0.5;"></div>

Parameters

Parameter Type Description
prop string | object The name of the CSS property (camelCase or kebab-case), or an object of property-value pairs.
value string | number The value to set. Numbers are automatically converted to pixels for appropriate properties.

Returns: The property value (string) for getter, or the original SR object for setter.