Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding child div's height to parent height

Tags:

angular

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

like image 206
doorman Avatar asked Sep 28 '16 09:09

doorman


2 Answers

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.

like image 137
dfsq Avatar answered Nov 07 '22 18:11

dfsq


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

like image 32
Pankaj Parkar Avatar answered Nov 07 '22 19:11

Pankaj Parkar