How to set an element height same as width?

2020-07-24 06:21发布

问题:

Can somebody tell me what is the best way to set height of element same as width? Also it should had auto-scaling according to web browser resolution changes. Is there a wato to that with only html template? Something like:

<div 
    class="tile"
    #square
    [ngStyle]="{'height.px': square.width}"    
>

回答1:

HTML:

  <div #div (window:resize)="0" [ngStyle]="{'height.px': div.offsetWidth }">

DEMO

All the credit goes to @yurzui



回答2:

You need to use square.offsetWidth:

<div 
    class="tile"
    #square
    [ngStyle]="{'height.px': square.offsetWidth}"    
>

Link to working demo.