Angular Material: layout-fill not filling parent <div>

白昼怎懂夜的黑 提交于 2019-12-03 14:41:28

I guess the fundamental issue comes from using both layout-align and layout-fill in the same parent container.

Solutions

In short, there are 2 ways to overcome this:

  1. Add a custom css rule on parent container with align-items: stretch;

  2. Use only layout-fill (no layout-align on the parent container)


Reasons

Whenever using layout-* directive, angular-material would add a class to the element, and apply flex layout related styles there. For example:

<div class="parent" layout="row" layout-align="space-between center" layout-fill> 
  <div class="child child-1" flex>
  <div class="child child-2" flex="none">
</div>

Will be compiled to :

<div class="parent layout-fill layout-align-space-between-center layout-row" layout="row" layout-align="space-between center" layout-fill> 
  <div class="child child-1 flex" flex>
  <div class="child child-2 flex-none" flex="none">
</div>

And the resulting css would be:

.layout {
    box-sizing: border-box;
    display: -webkit-box;
    display: -webkit-flex;
    display: -moz-box;
    display: -ms-flexbox;
    display: flex;
}

.layout-row {
    flex-direction: row;
}

.layout-align-space-between-center {
    align-items: center;
    align-content: center;
    max-width: 100%;
    justify-content: space-between;
}

.layout-fill {
    margin: 0;
    width: 100%;
    min-height: 100%;
    height: 100%;
}

As you can see here, layout-fill does not change anything in flex related-rules at all.

What we expect for a stretched flex item should leverage on align-items: stretch as mentioned in this A Complete Guide to Flexbox by CSS-tricks. But this simply is not the case for angular material implementation. And it make sense because align-item rule is already being used in layout-align-* as a way to position the item.

Examples

Check this codepen example to see how it works.

<div layout-fill>
<img src="" class="bg" alt="image you want in background"></img>
<div layout="column" layout-align="center center">
    <md-content class="md-padding">
        Sample Text Here
    </md-content>
    <md-button class="md-raised md-primary">Sign Up</md-button>
</div>
</div>

and add this to yr CSS

  img.bg {
  /* Set rules to fill background */
  min-height: 100%;
  min-width: 1024px;

  /* Set up proportionate scaling */
  width: 100%;
  height: auto;

  /* Set up positioning */
  position: fixed;
  top: 0;
  left: 0;
}

A bit late 😀 You need to add "layout"-attribute to the div where your layout-fill is.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!