Interface code changes inline styles all the time: a drawer slides by setting transform, a progress bar sets width, a theme switch sets custom properties like --accent. Writing element.style.x = … by hand works until names arrive in different shapes (backgroundColor from code, background-color from a config file), numbers need units, and some values need !important.
Write css in css.js: one helper that reads one inline property, sets one, or sets many from an object.
css(element, name) // returns a string: the inline value, "" when not setcss(element, name, value) // sets one property, returns elementcss(element, styles) // sets every own property of styles, returns element
element: any element.
name: a camelCase name (backgroundColor), a kebab-case name (background-color) or a custom property (--gap).
Reading returns the element's inline value of the property, or "" when it is not set. Values from stylesheets do not count.
Setting returns the element, so calls can chain.
camelCase and kebab-case names both work. A capital letter becomes - plus its lower case, so WebkitTransform is -webkit-transform.
Custom properties (names that start with --) are used exactly as written: they are case-sensitive.
A number gets px added (120 becomes 120px, -4 becomes -4px), except for the unitless properties opacity, z-index, line-height, font-weight, flex-grow, flex-shrink and order, and except for custom properties.
null or "" removes the property.
A third argument of undefined counts as missing: the call reads.
A value ending in !important is set with the important priority; the stored value is the part before it.
A value the browser rejects leaves the old value in place.
The object form sets each own key with the same rules, skips keys whose value is undefined, and returns the element.
Only the properties named in the call change; every other inline property stays.
css(box, "color", "green");css(box, "color", "not-a-colour"); // rejected by the browsercss(box, "color"); // still "green"css(document.createElement("p"), "color"); // "": never set