Skip to curriculum
Jump to the editor

17.1. Get and set inline styles with one helper

Easy
Topics
10–20 min

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.

Signature

JAVASCRIPT
css(element, name)          // returns a string: the inline value, "" when not set
css(element, name, value)   // sets one property, returns element
css(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).
  • value: a string, a number, null or "".

Requirements

  1. Reading returns the element's inline value of the property, or "" when it is not set. Values from stylesheets do not count.
  2. Setting returns the element, so calls can chain.
  3. camelCase and kebab-case names both work. A capital letter becomes - plus its lower case, so WebkitTransform is -webkit-transform.
  4. Custom properties (names that start with --) are used exactly as written: they are case-sensitive.
  5. 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.
  6. null or "" removes the property.
  7. A third argument of undefined counts as missing: the call reads.
  8. A value ending in !important is set with the important priority; the stored value is the part before it.
  9. A value the browser rejects leaves the old value in place.
  10. The object form sets each own key with the same rules, skips keys whose value is undefined, and returns the element.
  11. Only the properties named in the call change; every other inline property stays.

Examples

JAVASCRIPT
const box = document.querySelector("#box");

css(box, "backgroundColor", "teal").style.backgroundColor; // "teal"
css(box, "background-color");                              // "teal"
css(box, "width", 120);       // style="… width: 120px"
css(box, "opacity", 0.5);     // "0.5", no px
css(box, "--columns", 3);     // "3", no px
JAVASCRIPT
css(box, { color: "red", marginTop: 8, padding: null });
// color: red; margin-top: 8px; padding removed

css(box, "color", "blue !important");
box.style.getPropertyPriority("color"); // "important"
css(box, "color");                       // "blue"
JAVASCRIPT
css(box, "color", "green");
css(box, "color", "not-a-colour"); // rejected by the browser
css(box, "color");                 // still "green"
css(document.createElement("p"), "color"); // "": never set

Environment

  • Plain JavaScript in a browser page; the tests call the global css from css.js on elements they create.
  • Use the element's style object (CSSStyleDeclaration). Computed styles (getComputedStyle) are not what reading means here.
  • Vendor names other than the camelCase rule above (such as cssFloat) are out of scope.

Hints

  1. Which style methods take a kebab-case name, and which one takes a priority?
  2. How can one regular expression turn every capital letter into a dash and a lower-case letter?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…