jQuery jQuery.cssNumber

Intermediate
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Unitless CSS

What You’ll Learn

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

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.

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.

📝 Syntax

jQuery.cssNumber is an object — there is no function call syntax:

Read a built-in entry

jQuery
jQuery.cssNumber.opacity   // true
jQuery.cssNumber.zIndex    // true
jQuery.cssNumber.width     // undefined (not unitless)

Add a custom property (official jQuery pattern)

jQuery
if ( jQuery.cssNumber ) {
  jQuery.cssNumber.someCSSProp = true;
}
  • Guard with if ( jQuery.cssNumber ) — the object does not exist in jQuery 4.0+.
  • Use camelCase keys matching DOM style property names.
  • Pair with a matching $.cssHooks.someCSSProp entry when the property is custom.

Effect on .css() numeric setters (jQuery < 4.0)

jQuery
$( "#a" ).css( "width", 200 );     // → width: 200px
$( "#b" ).css( "opacity", 0.5 );   // → opacity: 0.5
$( "#c" ).css( "zIndex", 10 );     // → z-index: 10

Built-in properties (selected)

  • Coreopacity, zIndex, lineHeight, fontWeight, zoom (since 1.4.3)
  • LayoutflexGrow, flexShrink, order, columnCount, orphans, widows
  • ModernaspectRatio, scale, animationIterationCount, grid properties (3.4+ / 3.7+)
  • SVGfillOpacity, strokeOpacity, stopOpacity, and related keys

Return value

  • jQuery.cssNumber itself is the object — assigning a property returns the assigned value (true).
  • Reading a missing key returns undefined — jQuery then applies default px logic for numeric setters.

⚡ Quick Reference

GoalCode
Check if property is unitless$.cssNumber.opacity === true
Set opacity (no px)$("#el").css("opacity", 0.5)
Set z-index (no px)$("#el").css("zIndex", 100)
Set line-height multiplier$("#el").css("lineHeight", 1.5)
Width gets px automatically$("#el").css("width", 200)200px
Add custom unitless propertyif ($.cssNumber) { $.cssNumber.myProp = true; }
jQuery 4.0+ alternative$("#el").css("width", "200px") — pass strings with units

📋 cssNumber vs .css() vs cssHooks

Three related pieces of jQuery’s styling pipeline — units, API, and custom handlers.

cssNumber
units

Allowlist of properties that skip auto px on numeric setters

.css()
get/set

Public API — consults cssNumber before writing numeric values

cssHooks
extend

Custom get/set logic — often paired with a cssNumber entry

String setter
"200px"

Always safe — explicit units bypass cssNumber logic entirely

Examples Gallery

Examples 1–5 demonstrate built-in unitless properties, px auto-append behavior, and adding custom keys. Use the Try-it links to run each snippet.

📚 Built-in Properties

Common unitless properties already listed in jQuery.cssNumber.

Example 1 — Set Opacity Without Units

Fade an element to 50% — opacity has been in cssNumber since jQuery 1.4.3.

jQuery
$( "#box" ).css( "opacity", 0.5 );
// inline style: opacity: 0.5  (NOT 0.5px)
Try It Yourself

How It Works

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.

jQuery
$( "#overlay" ).css( "zIndex", 100 );
// inline style: z-index: 100  (NOT 100px)
Try It Yourself

How It Works

zIndex controls stacking order among positioned elements. It is a dimensionless integer — cssNumber prevents jQuery from producing the invalid 100px.

Example 3 — Unitless lineHeight Multiplier

Set readable line spacing with a bare number that multiplies font size.

jQuery
$( "#text" ).css( "lineHeight", 1.5 );
// equivalent to 1.5 × current font-size
Try It Yourself

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
Try It Yourself

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.

jQuery
if ( jQuery.cssNumber ) {
  jQuery.cssNumber.rotateDeg = true;
}
jQuery.cssHooks.rotateDeg = {
  set: function( elem, value ) {
    elem.style.transform = "rotate(" + parseFloat( value ) + "deg)";
  }
};
$( "#box" ).css( "rotateDeg", 90 );
Try It Yourself

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.

🚀 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.

📝 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).

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
cssNumber 3.x only

Bottom line: Safe in jQuery 3.x and earlier. Check if (jQuery.cssNumber) before extending. Plan string-based setters for jQuery 4+.

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 ).

💡 Best Practices

✅ Do

  • Pass explicit strings ("200px", "50%") when units matter
  • Guard custom entries with if ( jQuery.cssNumber )
  • Add cssNumber keys when pairing custom properties with cssHooks
  • Use built-in unitless properties (opacity, zIndex) as intended
  • Plan jQuery 4 migration — string setters replace cssNumber logic

❌ Don’t

  • Assume all numeric .css() values stay unitless — width gets px
  • 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

Key Takeaways

Knowledge Unlocked

Six things to remember about cssNumber

Unitless allowlist for .css().

6
Core concepts
px 02

Skip px

Unitless

Logic
0.5 03

opacity

Built-in

Example
+ 04

Custom

Add key

Extend
& 05

cssHooks

Pair

Plugin
4 06

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.

Next: .height()

Learn how to get and set element height as a unitless pixel number.

.height() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful