doorman
doorman

Reputation: 16969

Binding child div's height to parent height

Binding child div's height to parent height doesn't seem to work

<div #parentdiv style="background-color:blue;height:1000px">
    <div style="background-color:red" [style.height.px]="parentdiv.height">
      hallo
    </div>
</div>

Here is a plunkr https://plnkr.co/edit/ifWbZSQ9FeiV6n1g9bes?p=preview

Upvotes: 6

Views: 9771

Answers (2)

dfsq
dfsq

Reputation: 193301

There are plenty of ways to do it with CSS (for example, check Pankaj's answer). But if you really-really need to go with javascript (believe me, you don't), then use offsetHeight property:

<div #parentdiv style="background-color:blue;height:1000px">
  <div style="background-color:red" [style.height.px]="parentdiv.offsetHeight">
    hallo
  </div>
</div>

Demo: https://plnkr.co/edit/QEH4fNoZlmTCn34hnh12?p=preview

That being said, what I posted here is for educational purposes, prefer CSS approach, over this one.

Upvotes: 15

Pankaj Parkar
Pankaj Parkar

Reputation: 136194

It can be easily achieve by CSS height: inherit

@Component({
  selector: 'my-app',
  styles: [`
    .myHeight{
      height: inherit;
      background-color:red;
    }
  `]
  template: `
    <div #parentdiv style="background-color:blue;height:1000px">
        <div class="myHeight">
          hallo
        </div>
    </div>
  `,
})

Demo

Upvotes: 4

Related Questions