jQuery.cssLoading saved progress…

jqueryCss

Build css, a small helper modeled on jQuery's .css() method, for reading and writing an element's inline styles. The same function name does three different jobs depending on how you call it: pass just a property name to read its current value, pass a name and a value to set one property, or pass an object to set several at once. Setting returns the element so calls can be chained; reading returns the value.

Signature

function css(el: HTMLElement, prop: string): string;                 // GET one
function css(el: HTMLElement, prop: string, value: string): HTMLElement; // SET one
function css(el: HTMLElement, props: Record<string, string>): HTMLElement; // SET many

Work only with inline styles (el.style) — never computed styles or stylesheet rules.

Examples

const el = document.createElement('div');

css(el, 'color', 'red'); // sets el.style.color, returns el
css(el, 'color');        // → 'red'   (reads the inline value)
css(el, 'margin');       // → ''       (unset → empty string)
// Object form sets several at once and returns the element, so calls chain:
css(el, { color: 'blue', margin: '0px' });
css(css(el, 'color', 'green'), 'padding', '4px');

Notes

  • Three call shapes. Branch on the arguments: an object means set-many; a string plus a value means set-one; a string alone means get.
  • Get returns a string. Reading an unset property returns '' (empty string), not undefined or null.
  • Set returns the element. Both set forms return el so you can chain further calls.
  • Get must not mutate. Reading a property leaves the element's styles untouched.
  • Inline only. Use el.style for both reading and writing. Do not reach for getComputedStyle or stylesheet rules.
Loading editor…