jQuery.cssNumber lists CSS properties that do not need units. Before jQuery 4.0, .css() used this object to decide whether to append px to numeric setter values. This tutorial covers built-in keys, adding custom entries, pairing with cssHooks, and migration notes.
01
Object
$.cssNumber
02
No px
Unitless nums
03
opacity
0 – 1
04
zIndex
Stack order
05
Custom
Add keys
06
1.4.3
Removed 4.0
Fundamentals
Introduction
When you write $("#box").css("width", 200), jQuery turns 200 into 200px because width is a pixel dimension. When you write $("#box").css("opacity", 0.5), jQuery leaves 0.5 alone — opacity is unitless. That distinction comes from jQuery.cssNumber.
Available since jQuery 1.4.3, cssNumber is a lookup object: camelCase property names mapped to true. Prior to jQuery 4.0, the .css() setter consulted this list before auto-appending px. Plugin authors also add custom keys when pairing cssHooks with synthetic unitless properties. jQuery 4.0 removed the API — pass explicit unit strings instead.
Concept
Understanding jQuery.cssNumber
jQuery.cssNumber is a plain object on the jQuery namespace. Each key is a CSS property name in camelCase (opacity, zIndex, lineHeight); each value is true, meaning “this property accepts unitless numeric values.” It is not a function — you read and extend the object directly.
Think of it as an allowlist for bare numbers. Properties not on the list — like width, height, marginTop — get px appended when you pass a number to .css(). Properties on the list keep the number as-is. That prevents invalid values like opacity: 0.5px or z-index: 10px.
💡
Beginner Tip
Most day-to-day styling never touches cssNumber — jQuery already lists common unitless properties. You only need to add entries when building a custom cssHook that accepts bare numbers, or when maintaining plugins that target jQuery 3.x and earlier.
Foundation
📝 Syntax
jQuery.cssNumber is an object — there is no function call syntax:
Because opacity is in cssNumber, jQuery passes the bare number to the browser. Opacity ranges from 0 (transparent) to 1 (opaque) — adding px would be invalid CSS.
Example 2 — Stack Layers with zIndex
Raise an overlay above siblings using a unitless integer.
lineHeight: 1.2 → tighter text
lineHeight: 2 → looser text
Unitless multiplier — not 1.5px
How It Works
CSS line-height accepts unitless numbers as multipliers of the element’s font size. jQuery lists lineHeight in cssNumber so 1.5 stays a multiplier rather than becoming 1.5px.
📈 px Logic & Custom Keys
Compare auto-append behavior and extend cssNumber for plugins.
Example 4 — width Gets px; opacity Does Not
Side-by-side comparison of numeric setter behavior in jQuery 3.x.
jQuery
$( "#box" ).css( "width", 200 ); // → width: 200px
$( "#box" ).css( "opacity", 0.4 ); // → opacity: 0.4
// width is NOT in $.cssNumber
// opacity IS in $.cssNumber
width 200 → style="width: 200px;"
opacity 0.4 → style="opacity: 0.4;"
cssNumber is the allowlist jQuery checks
How It Works
This is the core behavior cssNumber controls. Dimension properties default to pixels; unitless properties do not. When in doubt, pass an explicit string: .css("width", "200px").
Example 5 — Official Demo: Add a Custom Property
Register rotateDeg for a cssHook that accepts degree values without px.
Box rotates 90°
.css("rotateDeg", 90) passes 90 — not "90px"
Official guard: if (jQuery.cssNumber) { ... }
How It Works
The official jQuery docs show this pattern alongside cssHooks. Without the cssNumber entry, jQuery would append px to 90, producing an invalid value for a rotation hook. Guard the assignment for jQuery 4.0 compatibility.
Applications
🚀 Common Use Cases
Fade effects — .css("opacity", 0) and .animate({ opacity: 1 }) rely on cssNumber.
Modal overlays — .css("zIndex", 9999) to stack above page content.
Typography — .css("lineHeight", 1.6) for readable unitless spacing.
Flexbox order — .css("order", 2) and .css("flexGrow", 1) without units.
Plugin cssHooks — add custom keys so numeric setters reach your hook correctly.
Debugging px bugs — if a numeric setter produces px unexpectedly, check whether the property is in cssNumber.
🧠 How cssNumber Affects .css()
1
Numeric setter
.css("propName", 200) — value is a JavaScript number.
Input
2
Check cssNumber
jQuery looks up camelCase key on $.cssNumber (jQuery < 4.0).
Lookup
3
Append px or not
Listed → keep number. Not listed → append px for dimensions.
Transform
4
🎨
Write inline style
Final value applied to element.style — or routed through a cssHook.
Important
📝 Notes
Available since jQuery 1.4.3 — removed in jQuery 4.0.
Keys are camelCase DOM property names; all values are true.
Only affects numeric setter values — strings like "200px" or "50%" pass through unchanged.
Guard custom additions: if ( jQuery.cssNumber ) { ... }.
jQuery 4.0+: pass explicit unit strings to .css() — no auto px append at all.
Pairs naturally with cssHooks for custom animatable properties.
Inspect the full list in DevTools: Object.keys($.cssNumber) (jQuery 3.x).
Compatibility
Browser Support
jQuery.cssNumber is a jQuery-internal API (since 1.4.3, removed in 4.0). It runs wherever jQuery runs — behavior is identical across browsers because px-append logic lives in jQuery, not the DOM.
✓ jQuery 1.4.3–3.x
jQuery.cssNumber
Present in jQuery 1.x, 2.x, and 3.x. Removed in jQuery 4.0 — use explicit unit strings instead. Load jQuery 3.7.1 in tutorials below to explore cssNumber; migrate to string values when upgrading to 4.x.
100%jQuery 3.x projects
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
cssNumber3.x only
Bottom line: Safe in jQuery 3.x and earlier. Check if (jQuery.cssNumber) before extending. Plan string-based setters for jQuery 4+.
Wrap Up
Conclusion
jQuery.cssNumber is the allowlist that tells jQuery which CSS properties accept bare numbers without a px suffix. Built-in entries cover opacity, zIndex, lineHeight, flex properties, and more. Plugin authors add custom keys alongside cssHooks.
Remember: .css("width", 200) becomes 200px; .css("opacity", 0.5) stays 0.5. jQuery 4.0 removed cssNumber — pass strings with explicit units instead. For jQuery 3.x projects, guard extensions with if ( jQuery.cssNumber ).
Extend cssNumber without a matching cssHook for custom properties
Rely on cssNumber in new jQuery 4.0+ projects — it no longer exists
Forget that string setters bypass cssNumber entirely
Add every property to cssNumber — only truly unitless ones belong
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about cssNumber
Unitless allowlist for .css().
6
Core concepts
{ }01
Object
Allowlist
Registry
px02
Skip px
Unitless
Logic
0.503
opacity
Built-in
Example
+04
Custom
Add key
Extend
&05
cssHooks
Pair
Plugin
406
Removed
jQ 4.0
Migrate
❓ Frequently Asked Questions
jQuery.cssNumber is an object on the jQuery namespace listing CSS property names that are unitless. Before jQuery 4.0, when you pass a numeric value to .css('propertyName', number), jQuery checks cssNumber — if the property is listed (value true), it leaves the number as-is; otherwise it appends 'px' for dimension properties like width and height.
width is not in cssNumber — jQuery treats numeric setters for dimension properties as pixel values and appends px automatically. opacity is in cssNumber — it is inherently unitless (0 to 1), so jQuery does not add px. The same rule applies to zIndex, lineHeight, flexGrow, order, and other listed properties.
If you define a cssHook for a custom property that accepts unitless numbers, add: if (jQuery.cssNumber) { jQuery.cssNumber.myProp = true; }. Use camelCase keys matching DOM style property names. Guard with if (jQuery.cssNumber) because the object was removed in jQuery 4.0.
Yes — jQuery 4.0 removed the cssNumber API. In jQuery 4+, pass string values with explicit units to .css() instead of relying on automatic px append. If you support jQuery 3.x and earlier, cssNumber still matters for numeric setters and cssHook plugins.
They solve different problems. cssHooks define how to get and set a property. cssNumber tells jQuery not to append px when the setter receives a bare number. Custom cssHooks often register a matching cssNumber entry so .css('rotateDeg', 45) passes 45 to the hook instead of the invalid string '45px'.
The object ships with unitless properties including opacity, zIndex, lineHeight, fontWeight, zoom, flexGrow, flexShrink, order, columnCount, animationIterationCount, aspectRatio, scale, grid-area properties, and several SVG opacity properties. Keys are camelCase; all values are true.
Did you know?
jQuery’s opacity cssHook and cssNumber entry work together — that is why $("#el").fadeTo(400, 0.5) and .animate({ opacity: 0.5 }) produce valid CSS without you ever writing px. The same object lists flexbox properties like flexGrow and order added across jQuery 1.10–1.12, and modern keys like aspectRatio and scale in jQuery 3.7 — all reflecting CSS properties that accept bare numbers in the spec.