Reputation: 75
I am trying to set the height of an element same as its width. It should be auto-scaling/ keep size ratio according to web browser resolution changes. Is there a way to do that with only html template? Something like:
<div class="tile" #square [ngStyle]="{'height.px': square.width}">
Upvotes: 2
Views: 2816
Reputation: 28708
The trick is to use the element's offsetWidth, but we also need to silent the window resizing to keep the ratio
HTML:
<div #square (window:resize)="0" [ngStyle]="{'height.px': square.offsetWidth }">
Upvotes: 5
Reputation: 34673
You need to use square.offsetWidth
:
<div
class="tile"
#square
[ngStyle]="{'height.px': square.offsetWidth}"
>
Link to working demo.
Upvotes: 1