Sunday 26 June 2022

How to get the desired style of an element?

I have the following code and I want to get the desired CSS property of the element and not the computed/applied ones. For example the computedStyle.width should have an undefined value. How can I accomplish this?

let ele = document.querySelector(".hello");
let computedStyle = window.getComputedStyle(ele);

console.log(computedStyle.backgroundColor);
console.log(computedStyle.height);
console.log(computedStyle.width); // <- This should be undefined or ""
.hello {
  height: 10px;
  background-color: red;
}
<div class="hello">
  helloInnerText
</div>


from How to get the desired style of an element?

No comments:

Post a Comment