jQuery cssHooks

Beginner
5 min read
Updated: Nov 2026
3 examples
CSS

Definition

jQuery.cssHooks (since 1.4.3) lets you plug custom get and set functions for a CSS property name. That extends both .css() and .animate().

Syntax

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

jQuery
$.cssHooks.someProp = {
  get: function (elem, computed, extra) { /* read */ },
  set: function (elem, value) { /* write */ }
};

// Then normal API works:
$("#el").css("someProp", "10px");
$("#el").css("someProp");
CallWhat it does
cssHooks — Custom CSS Get and SetLearn jQuery.cssHooks: override how .css() gets and sets properties, normalize vendor prefixes, use cssNumber and fx.step, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Confirm $.cssHooks exists (jQuery 1.4.3+).
Step 2Optional: feature-test the real style property name (standard vs vendor).
Step 3Inside $(function () { … }), assign $.cssHooks.propName = { get, set }.

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 — Basic Get/Set Skeleton

Register a hook on ready, then set and read with .css().

jQuery
$(function () {
  $.cssHooks.borderRadius = {
    get: function (elem) {
      return elem.style.borderRadius || "0px";
    },
    set: function (elem, value) {
      elem.style.borderRadius = value;
    }
  };

  $("#box").css("borderRadius", "16px");
  console.log($("#box").css("borderRadius")); // "16px"
});
Try It Yourself

How It Works

  1. Ready. Registration waits until document ready.
  2. Hook. get reads; set writes style.borderRadius.
  3. Call. .css("borderRadius", "16px") hits your set.

Example 2 — Feature-Test Property Name

Find the supported name (standard or vendor-prefixed) before wiring a hook.

jQuery
function styleSupport(prop) {
  var cap = prop.charAt(0).toUpperCase() + prop.slice(1);
  var prefixes = ["Moz", "Webkit", "ms"];
  var div = document.createElement("div");
  var supported = prop in div.style ? prop : null;
  if (!supported) {
    for (var i = 0; i < prefixes.length; i++) {
      var vendor = prefixes[i] + cap;
      if (vendor in div.style) { supported = vendor; break; }
    }
  }
  return supported;
}

console.log(styleSupport("borderRadius")); // e.g. "borderRadius"
Try It Yourself

How It Works

  1. Probe. Create a temporary div and check div.style.
  2. Standard first. If borderRadius exists, use it.
  3. Prefixes. Otherwise try Moz, Webkit, ms.

Example 3 — Custom Property + cssNumber

Expose a unitless rotateDeg that maps to transform.

jQuery
$(function () {
  $.cssNumber.rotateDeg = true; // do not append "px"

  $.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", 45);
  console.log($("#box").css("rotateDeg")); // 45
});
Try It Yourself

How It Works

  1. cssNumber. Marks the property unitless so jQuery won’t add px.
  2. set. Builds a rotate(Ndeg) transform string.
  3. get. Parses the degrees back out of style.transform.

Frequently Asked Questions

jQuery.cssHooks is an object where you register get and set handlers for CSS property names. When you call .css('prop') or .animate({ prop: value }), jQuery can route through your hook instead of the default style logic.
Use cssHooks for special cases: vendor-prefix normalization, synthetic properties (like a rotateDeg helper), or animation of non-standard values. Everyday color and width changes only need .css().
jQuery writes its own cssHooks during document ready and can overwrite hooks registered too early. The official docs recommend wrapping your registration in a ready callback.
cssHooks define how to read and write a property. cssNumber tells jQuery not to auto-append px to numeric setter values — useful for opacity, z-index, or custom degree values.
A get/set hook lets animation use that property. For complex values, also add $.fx.step.propName so each frame pushes the tweened number through your setter.
Rarely for common modern properties. Hooks remain useful for plugins, legacy support, custom properties, and understanding how .css() and .animate() share one extension point.

Next: cssNumber — Unitless CSS Properties

Learn which CSS properties skip the automatic px suffix in jQuery.

cssNumber — Unitless CSS Properties 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.

8 people found this page helpful