jQuery cssNumber

Intermediate
6 min read
Updated: Oct 2026
4 examples
Live preview

What Is cssNumber?

jQuery.cssNumber (since 1.4.3, removed in 4.0) is an object of camelCase CSS property names that are unitless. Before 4.0, .css() used it to decide whether a bare number should get a px suffix.

Remember
// In cssNumber → no px
$("p").css("opacity", 0.5)   // "0.5"
$("p").css("zIndex", 10)     // "10"

// Not in cssNumber → px added (jQuery < 4)
$("p").css("width", 200)     // "200px"

// Add a custom key (guard for 4.0+):
if (jQuery.cssNumber) {
  jQuery.cssNumber.myProp = true;
}
NeedUse
Unitless number (opacity, z-index)Listed in $.cssNumber
Pixel size (width, height)Number → px (pre-4.0) or pass "200px"
Custom unitless prop$.cssNumber.prop = true + cssHooks
jQuery 4+Always pass strings with units

How to Use cssNumber

Treat it as a lookup: if the property is listed, numbers stay unitless.

  1. Check that jQuery.cssNumber exists (skip on jQuery 4+).
  2. For built-ins like opacity, pass a number to .css() — no px.
  3. For a custom property, set $.cssNumber.propName = true.
  4. Pair custom keys with a cssHook when the property is synthetic.
PieceRequired?Role
$.cssNumberYes (pre-4.0)Map of unitless property names
CamelCase keyYesDOM-style name (zIndex, not z-index)
Value—Always true for listed keys

Pseudocode

Pseudocode
when .css(prop, number) is called (jQuery < 4):
    if prop is in $.cssNumber:
        write number as-is          // opacity → 0.5
    else:
        write number + "px"         // width → "200px"

to teach a custom prop:
    if $.cssNumber exists:
        $.cssNumber.myProp = true

Cheat Sheet

Keep these facts handy for numeric .css() setters.

TopicDetail
APIjQuery.cssNumber / $.cssNumber
Since1.4.3
RemovedjQuery 4.0
Common keysopacity, zIndex, lineHeight, fontWeight
AlsoflexGrow, order, columnCount, grid keys
Extendif ($.cssNumber) $.cssNumber.prop = true
Safe habitPass "200px" / "0.5" as strings when unsure

Live Preview

Compare a unitless property with a dimension property. Results show the inline style attribute after each call.

Browser demo of how $.cssNumber affects numeric .css() setters.

Result
$("p").css("opacity", 0.5)
style → "opacity: 0.5;"

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Opacity Stays Unitless

opacity is in cssNumber — a number is not turned into px.

jQuery
// <p id="msg">Hello</p>
$("#msg").css("opacity", 0.5);
console.log($("#msg").attr("style")); // "opacity: 0.5;"
console.log($.cssNumber.opacity);     // true
Try It Yourself

How It Works

  1. Set. Pass the number 0.5 to .css().
  2. Lookup. jQuery finds opacity in $.cssNumber.
  3. Write. The value stays 0.5 — not 0.5px.
  4. Confirm. $.cssNumber.opacity === true.

Example 2 — Width Gets px, Opacity Does Not

Side-by-side comparison of a dimension property and a unitless one.

jQuery
// <div id="box">Demo</div>
$("#box").css("width", 200);
$("#box").css("opacity", 0.4);
console.log($("#box").attr("style"));
// e.g. "width: 200px; opacity: 0.4;"
Try It Yourself

How It Works

  1. Width. Not in cssNumber → jQuery appends px.
  2. Opacity. Listed → stays a unitless number.
  3. Inline style. Both end up on the same style attribute.
  4. Rule. Dimensions want units; opacity does not.

Example 3 — zIndex Without px

Stacking order is an integer — also listed in cssNumber.

jQuery
// <div id="layer">Top</div>
$("#layer").css("zIndex", 100);
console.log($("#layer").css("z-index")); // "100"
console.log($.cssNumber.zIndex);         // true
Try It Yourself

How It Works

  1. Key. Use camelCase zIndex (or hyphenated z-index in .css()).
  2. Number. 100 is written without px.
  3. Read. Getter returns the stacking value as a string.
  4. Listed. $.cssNumber.zIndex is true.

Example 4 — Add a Custom Unitless Key

Official pattern: guard, then mark your property before using a cssHook.

jQuery
$(function () {
  if ($.cssNumber) {
    $.cssNumber.rotateDeg = true;
  }
  $.cssHooks.rotateDeg = {
    get: function (elem) {
      var m = (elem.style.transform || "").match(/rotate\((-?[\d.]+)deg\)/);
      return m ? parseFloat(m[1]) : 0;
    },
    set: function (elem, value) {
      elem.style.transform = "rotate(" + parseFloat(value) + "deg)";
    }
  };

  $("#box").css("rotateDeg", 90);
  console.log($("#box").css("rotateDeg")); // 90
});
Try It Yourself

How It Works

  1. Guard. if ($.cssNumber) keeps the code safe on jQuery 4+.
  2. Mark. rotateDeg = true stops jQuery from appending px.
  3. Hook. The setter builds a rotate(Ndeg) transform.
  4. Result. .css("rotateDeg", 90) receives 90, not "90px".

Edge Cases & Pitfalls

Check these before relying on numeric .css() setters.

removed

Missing on jQuery 4.0+

$.cssNumber is undefined. Always pass unit strings, and guard custom registrations.

case

Keys are camelCase

Register zIndex, not z-index. Hyphenated names work in .css() calls, but the object uses DOM names.

hook

Custom props still need a hook

cssNumber only skips px. Without a cssHook, a made-up name does nothing useful.

strings

Strings skip the px logic

If you pass "50%" or "2em", jQuery does not append px — the auto-append path is for bare numbers.

Browser Support

jQuery.cssNumber exists in jQuery 1.4.3–3.x in the browser. It was removed in jQuery 4.0. Not for Node.js without a DOM.

✓ jQuery 1.4.3–3.x · Removed in 4.0

jQuery.cssNumber

Available wherever jQuery 1.4.3–3.x runs in a browser. On jQuery 4+, pass explicit unit strings to .css() instead.

Legacy Pre-4.0 jQuery
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 1.4.3–3.x

Bottom line: Use cssNumber on jQuery 3.x and earlier. For new code, prefer .css("width", "200px") so behavior stays clear across versions.

Performance Notes

ApproachCostBest for
Bare number + cssNumberNegligiblejQuery 3.x unitless props
String with unitsSameAll versions, clearest intent
Custom cssNumber + hookSmallPlugin / synthetic properties

The lookup is a cheap object check. Clarity matters more than micro-cost — explicit unit strings travel better across jQuery major versions.

Key Takeaways

  • Role: list of unitless CSS properties for .css() numbers.
  • Listed: no px; not listed: often get px (pre-4.0).
  • Removed: gone in jQuery 4.0 — pass unit strings instead.
  • Extend: if ($.cssNumber) $.cssNumber.prop = true.

One line: cssNumber tells pre-4.0 jQuery which numeric .css() values must stay unitless.

Frequently Asked Questions

jQuery.cssNumber is an object listing CSS property names that are unitless. Before jQuery 4.0, .css() used it to decide whether to append px to a bare number — listed properties stay unitless; others often get px.
width is not in cssNumber, so jQuery treats the number as pixels. opacity is in cssNumber, so the value stays a unitless 0–1 number.
Yes. jQuery 4.0 removed cssNumber. Pass string values with explicit units to .css() instead. On jQuery 3.x and earlier, cssNumber still controls numeric setters.
Guard and set a camelCase key: if (jQuery.cssNumber) { jQuery.cssNumber.myProp = true; }. Often paired with a cssHook that expects a bare number.
cssHooks define get/set behavior. cssNumber only controls the px auto-append step. Custom hooks that take numbers usually register a matching cssNumber entry.
Common ones include opacity, zIndex, lineHeight, fontWeight, zoom, flexGrow, flexShrink, order, columnCount, and several grid/SVG opacity properties. Keys are camelCase; values are true.

Next: height()

Learn how to get and set an element’s height with jQuery.

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.

7 people found this page helpful