Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Element — DOM structure, geometry, and scrolling

Element represents a DOM element, not an arbitrary node. It provides navigation, attributes, and content replacement; browsers also expose layout measurements and scrolling. Distinguish tree structure, client-area dimensions, and viewport-relative rectangles.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

element.clientWidth; element.clientHeight; element.clientLeft; element.clientTop; element.scrollHeight; element.scrollTop; element.scrollTop = y; element.getBoundingClientRect(); element.getClientRects(); element.scrollIntoView({ block: "nearest", behavior: "instant" }); element.replaceChildren(...nodesOrStrings); element.children; element.tagName; element.parentElement; element.previousElementSibling; element.nextElementSibling; element.hasAttribute(name); element.toggleAttribute(name, force);

Element represents a DOM element. Element.children is a live collection of direct element children, excluding text and comments. Element.parentElement, Element.previousElementSibling, and Element.nextElementSibling return an element or null. Element.tagName gives the qualified name; HTML elements in HTML documents use uppercase names.

Element.hasAttribute(name) tests attribute presence. Element.toggleAttribute(name, force) forces presence for true, absence for false, or toggles without force; it returns final presence. Element.replaceChildren(...nodesOrStrings) replaces all children, treating strings as text, not HTML. With no arguments, it removes all children.

For a typical block element, Element.clientWidth and Element.clientHeight report the inner width and height, including padding but excluding borders and space occupied by scrollbars. For an ordinary inline span, both properties return 0 even if it has padding and rendered content. In standards mode, the root element reports the viewport dimensions excluding scrollbars, rather than the dimensions of its own padding box. Element.clientLeft and Element.clientTop give the client area's offset from the border edge; they can include a scrollbar. Element.scrollHeight includes vertically overflowing content. These measurements are integers. Writable Element.scrollTop gives the vertical scroll offset and can be fractional, or negative in reversed layouts.

Element.getBoundingClientRect() returns an enclosing rectangle; Element.getClientRects() returns fragment rectangles, such as separate lines of an inline element. Coordinates are viewport-relative and account for CSS transforms. Element.scrollIntoView({ block: "nearest", behavior: "instant" }) scrolls containers to reveal the element. Geometry reads can force layout calculation; group them before style writes.

This is a browser-environment API, not ECMAScript alone; Node 24 does not establish availability. The DOM source defines structure and attributes, not the CSSOM View geometry and scrolling algorithms. No minimum browser versions are asserted; check required methods in the target environment. The example deliberately avoids layout measurements.

Examples

(() => {
  const box = document.createElement("div");
  const first = document.createElement("span");
  const last = document.createElement("span");
  box.replaceChildren(first, "text", last);
  box.toggleAttribute("data-ready", true);

  console.log(box.children.length);
  console.log(first.parentElement === box);
  console.log(first.nextElementSibling === last);
  console.log(last.previousElementSibling === first);
  console.log(box.hasAttribute("data-ready"));
  console.log(box.toggleAttribute("data-ready", false));
  box.replaceChildren("done");
  console.log(box.children.length, box.textContent);
})();

Result

2
true
true
true
true
false
0 done

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources