Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

HTMLElement.style: transform and height

Set an element’s transform and height from JavaScript, and distinguish inline declarations from the values resolved by the CSS cascade.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

const style = element.style;
style.transform = "translateY(12px)";
style.height = "80px";
style.removeProperty("transform");
style.height = "";

HTMLElement.style CSSStyleDeclaration.transform/height refers to two CSS properties exposed on an HTML element’s inline style object. HTMLElement.style returns a live object reflecting the element’s style attribute, so a saved reference continues to reflect changes. The current specification returns CSSStyleProperties, which inherits from CSSStyleDeclaration; some browsers may still return CSSStyleDeclaration. These are web platform APIs, not part of ECMAScript 2024.

You cannot replace the style object: assigning directly to element.style sets cssText and replaces the declaration block. Read or change individual declarations with element.style.transform and element.style.height. Assign valid CSS strings such as "80px". In standards mode, assigning a nonzero unitless value such as "81" to height is ignored without throwing, leaving the previous value intact. In quirks mode, browsers can instead interpret that value as a pixel length.

transform changes how a transformable box is drawn: translateY(12px) moves it down by 12 CSS pixels without changing the space it occupies in normal flow. height specifies a layout height: by default it sizes the content box, but with box-sizing: border-box it sizes the border box.

Reading an absent inline declaration returns "". Assigning "" or calling removeProperty() removes that declaration; this is not the same as setting transform: none or height: auto. To read resolved values after the cascade, use the read-only object returned by getComputedStyle(element).

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>HTMLElement.style: transform and height</title>
</head>
<body>
<script>
(() => {
  const element = document.createElement("div");
  const style = element.style;
  const log = () => console.log(JSON.stringify([style.transform, style.height]));

  style.transform = "translateY(12px)";
  style.height = "80px";
  log();

  style.height = "81";
  log();

  style.removeProperty("height");
  log();
})();
</script>
</body>
</html>

Result

["translateY(12px)","80px"]
["translateY(12px)","80px"]
["translateY(12px)",""]

Save the complete example as an HTML file and open it in a browser. Keep `<!doctype html>` so the page runs in standards mode. Check the output in the browser console. The element is not attached to the document, so the output shows inline declarations rather than rendered dimensions.

Related lessons

  • JavaScript · Lesson 11 · Advanced · Version: 4

Sources