jQuery cssNumber

Beginner
5 min read
Updated: Nov 2026
3 examples
CSS

Definition

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.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
// 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;
}
CallWhat it does
cssNumber — Unitless CSS PropertiesLearn jQuery.cssNumber: which CSS properties skip auto px in .css(), opacity and zIndex examples, custom keys with cssHooks, jQuery 4.0 removal, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Check that jQuery.cssNumber exists (skip on jQuery 4+).
Step 2For built-ins like opacity, pass a number to .css() — no px.
Step 3For a custom property, set $.cssNumber.propName = true.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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() Method — Get and Set Element Height

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

.height() Method — Get and Set Element Height tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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