Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

CSSStyleDeclaration: border widths and transform

Set individual border widths and a CSS transform through `element.style`, and distinguish inline declarations from the values the browser resolves after applying the cascade.

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

Syntax

const style = element.style;
style.borderStyle = "solid";
style.borderWidth = "2px 4px 6px 8px";
style.borderTopWidth = "3px";
style.borderRightWidth = "4px";
style.borderBottomWidth = "6px";
style.borderLeftWidth = "8px";
style.transform = "translateX(12px)";
style.setProperty("border-top-width", "3px");
style.getPropertyValue("border-top-width");
getComputedStyle(element).borderTopWidth;

element.style exposes a live object whose properties let you read and edit declarations in an element’s style attribute. In the current CSSOM, the object is a CSSStyleProperties, which inherits from CSSStyleDeclaration; older documentation may name the base interface. This is a browser API, not part of ECMAScript 2024.

borderWidth is the border-width shorthand. One value sets every side; two set top/bottom and right/left; three set top, right/left, and bottom; four set top, right, bottom, and left. To change one side, use borderTopWidth, borderRightWidth, borderBottomWidth, or borderLeftWidth. Use CSS value strings such as "3px", "0", or "thin"; the number 3 does not mean three pixels. Width alone does not make a border visible: a border style of none or hidden makes its width resolve to zero. Set a style such as borderStyle = "solid".

style.transform = "translateX(12px)" replaces the entire inline transform declaration. If the element is transformable and this declaration wins in the cascade, the element is drawn 12 CSS pixels to the right without moving its slot in normal layout. An applicable !important declaration in an author stylesheet can override the inline declaration. Put multiple functions in one space-separated string; their order matters.

setProperty("border-top-width", "3px") and getPropertyValue("border-top-width") use the hyphenated CSS name, unlike style.borderTopWidth. Reading style.borderTopWidth returns "" if that side has no inline declaration. To read the value the browser resolves after the cascade for an element in the document, use getComputedStyle(element); its result is live but read-only.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Border widths and transform</title>
</head>
<body>
  <script>
    {
      const box = document.createElement("div");
      const style = box.style;

      style.borderStyle = "solid";
      style.borderWidth = "2px 4px 6px 8px";
      style.setProperty("border-top-width", "3px");
      style.transform = "translateX(12px)";

      console.log([
        style.borderTopWidth,
        style.borderRightWidth,
        style.borderBottomWidth,
        style.borderLeftWidth,
      ].join(" "));
      console.log(style.getPropertyValue("border-top-width"));
      console.log(style.transform);
    }
  </script>
</body>
</html>

Result

3px 4px 6px 8px
3px
translateX(12px)

Save the example as an HTML file, open it in a browser, and check the developer console. The shorthand sets all four widths, and `setProperty()` changes only the top width. The element is not inserted into the document, so the output shows inline declarations rather than a rendered result.

Related lessons

  • JavaScript · Lesson 35 · Intermediate · Version: 3

Sources