From 48dbb94c3c9118d5f37f68a553d3247890e3a8a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Go=C5=82e=CC=A8biowski-Owczarek?= Date: Wed, 9 Oct 2024 23:33:44 +0200 Subject: [PATCH] css: Document no auto-appending `px` in jQuery 4.0 Also, document the removal of `jQuery.cssNumber` in jQuery 4.0. --- entries/css.xml | 5 ++++- entries/jQuery.cssNumber.xml | 23 ++++------------------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/entries/css.xml b/entries/css.xml index 7d6bd56e3..a5b17f2db 100644 --- a/entries/css.xml +++ b/entries/css.xml @@ -108,6 +108,7 @@ $( "div" ).on( "click", function() { + @@ -146,7 +147,8 @@ $( "div" ).on( "click", function() {

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.

@@ -286,5 +288,6 @@ $( "div" ).on( "click", function() { +
diff --git a/entries/jQuery.cssNumber.xml b/entries/jQuery.cssNumber.xml index ce5af8867..60d9cc84c 100644 --- a/entries/jQuery.cssNumber.xml +++ b/entries/jQuery.cssNumber.xml @@ -1,31 +1,16 @@ - + jQuery.cssNumber 1.4.3 - An object containing all CSS properties that may be used without a unit. The .css() method uses this object to see if it may append px to unitless values. + An object containing all CSS properties that may be used without a unit. Prior to jQuery 4.0, the .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)
  • -