Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In angular , How to use component variable in style

Tags:

css

angular

I want to use a variable width in my angular 5 component template, just like this:

<style>
    input:checked + .slider:before {
        transform: translateX({{width}}px);
    }
</style>

While 'width' is a variable defined in component!

however the "{{width}}" is not parsed ,how to implement this function.thanks!

like image 419
Yang Rui Avatar asked Feb 14 '26 12:02

Yang Rui


1 Answers

You can use Angular ngStyle in this case:

[ngStyle]="{ width: width }"

Take a look at this demo

like image 122
Mohammad Kermani Avatar answered Feb 17 '26 01:02

Mohammad Kermani