0

I m trying to get a css property on page load but as you can see below, it never shows the display but it shows correctly if I change it. My question is how can I display the "block" on Display 1?

Thanks for your help

let hello = document.getElementById('hello');

document.getElementById('you').innerHTML += hello.style.display;

hello.style.display = "inline-block";

document.getElementById('too').innerHTML += hello.style.display
#hello {
  background-color:yellow;
  height:20px;
  display:block;
}
<div id="hello">Hello</div>
<div id="you">Display 1: </div>
<div id="too">Display 2: </div>

1 Answer 1

2

.style reads from the inline styles of an element (as in <div style="...">), not the stylesheet. You can use hello.getBoundingClientRect().height to read the height of a DOM element.

Alternatively, you could use getComputedStyle:

const styles = window.getComputedStyle(hello)
const height = styles.getPropertyValue('height')
Sign up to request clarification or add additional context in comments.

1 Comment

I needed to get the display value and your getBoundingClientRect() doesn't work for it so I edited my post The getComputedStyle is what I needed, thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.