7

How can I get css properties from a ElementRef element? I've tried to use element.style but all the properties are empty. The Renderer class has this.renderer.setElementStyle method but there is no this.renderer.getElementStyle method.

1 Answer 1

8

elementRef instance has a nativeElement property which is an element node itself and contains most of the properties one may need. Another solution which is well supported by modern browsers is

window.getComputedStyle(elementRef.nativeElement)
Sign up to request clarification or add additional context in comments.

6 Comments

Well most properties of Angular components are empty by default unless you set some. What property do you need?
I need the css properties that are set by css code. In my case I'm trying to find the display property but my question is more like a global question.
not the best solution I guess, but it works window.getComputedStyle(elementRef.nativeElement)
Thank you it works perfect! But I'm using this code in ngAfterViewChecked which is called a lot of times. Does getComputedStyle use a lot of memory? And can you please edit you post so I can accept it as answer?
Ok, edited. I can't give an answer on it's performance. This needs some research.
|

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.