As with the .prop()
method, the .css()
method makes setting properties of elements quick and easy. This method can take either a property name and value as separate parameters, or a single object of key-value pairs.
Also, jQuery can equally interpret the CSS and DOM formatting of multiple-word properties. For example, jQuery understands and returns the correct value for both .css({ "background-color": "#ffe", "border-left": "5px solid #ccc" })
and .css({backgroundColor: "#ffe", borderLeft: "5px solid #ccc" })
. Notice that with the DOM notation, quotation marks around the property names are optional, but with CSS notation they're required due to the hyphen in the name.
When a number is passed as the value, jQuery will convert it to a string and add px
to the end of that string. If the property requires units other than px
, convert the value to a string and add the appropriate units before calling the method.
In jQuery 3.x or older, when a number is passed as the value, jQuery will convert it to a string and add px
to the end of that string. There's one exception: px
is not added to keys of jQuery.cssNumber
If the property requires units other than px
, convert the value to a string and add the appropriate units before calling the method.
In jQuery 4.0 or newer, when a number is passed as the value, jQuery will only convert it to a string and add px
to the end of that string for a limited set of properties - mostly related to width, height, border, margin & padding.
When using .css()
as a setter, jQuery modifies the element's style
property. For example, $( "#mydiv" ).css( "color", "green" )
is equivalent to document.getElementById( "mydiv" ).style.color = "green"
. Setting the value of a style property to an empty string — e.g. $( "#mydiv" ).css( "color", "" )
— removes that property from an element if it has already been directly applied, whether in the HTML style attribute, through jQuery's .css()
method, or through direct DOM manipulation of the style
property. As a consequence, the element's style for that property will be restored to whatever value was applied. So, this method can be used to cancel any style modification you have previously performed. It does not, however, remove a style that has been applied with a CSS rule in a stylesheet or <style>
element. Warning: one notable exception is that, for IE 8 and below, removing a shorthand property such as border
or background
will remove that style entirely from the element, regardless of what is set in a stylesheet or <style>
element.
Note: .css()
doesn't support !important
declarations. So, the statement $( "p" ).css( "color", "red !important" )
does not turn the color of all paragraphs in the page to red as of jQuery 3.6.0. Do not depend on that not working, though, as a future version of jQuery may add support for such declarations. It's strongly advised to use classes instead; otherwise use a jQuery plugin.
As of jQuery 1.8, the .css()
setter will automatically take care of prefixing the property name. For example, take .css( "user-select", "none" )
in Chrome/Safari will set it as -webkit-user-select
, Firefox will use -moz-user-select
, and IE10 will use -ms-user-select
.
.css()
method uses this object to see if it may append px
to unitless values..css()
method uses this object to see if it may append px
to unitless values.You can think about jQuery.cssNumber
as a list of all CSS properties you might use without a unit. It's used by .css()
to determine if it needs to add px
to unitless values.
The keys of the jQuery.cssNumber
object are camel-cased and the values are all set to true
. If you want to prevent the .css()
method from automatically adding the px
unit for a specific CSS property, you can add an extra property to the jQuery.cssNumber
object.
You can think about jQuery.cssNumber
as a list of all CSS properties you might use without a unit. Prior to jQuery 4.0, it was used by .css()
to determine if it needs to add px
to unitless values.
The keys of the jQuery.cssNumber
object are camel-cased and the values are all set to true
. If you want to prevent the .css()
method from automatically adding the px
unit for a specific CSS property and that property is not yet a key of the jQuery.cssNumber
object, you can add such an extra property:
jQuery.cssNumber.someCSSProp = true;
- By default the object contains the following properties:
-zIndex
fontWeight
opacity
zoom
lineHeight
widows
(added in jQuery 1.6)orphans
(added in jQuery 1.6)fillOpacity
(added in jQuery 1.6.2)columnCount
(added in jQuery 1.9)order
(added in jQuery 1.10.2)flexGrow
(added in jQuery 1.11.1)flexShrink
(added in jQuery 1.11.1)