Flexbox: shrink image to fit

前端 未结 4 663
不知归路
不知归路 2021-01-05 10:39

I am trying to design a page with the following properties that will be used as digital signage:

  • Page height is viewport height (100vh) so that sc
4条回答
  •  渐次进展
    2021-01-05 10:59

    So here's what we know:

    • The page height is 100vh
    • The first row is static (height: 100px)
    • The second row is static (height: 150px)
    • The third row, which contains images, should fill the remaining height

    I think the solution lies in basic math:

    100vh - 100px - 150px = height of third row
    

    Instead of this in your code:

    div.green {
        background-color: green;
        flex: 0 1 auto;
    }
    
    img {
        max-height: 100%;
    }
    

    Try this:

    div.green {
        background-color: green;
        flex: 1 1 auto;
    }
    
    img {
        height: calc(100vh - 250px);
    }
    

    body {
      margin: 0;
    }
    div#container {
      display: flex;
      flex-direction: column;
      height: 100vh;
    }
    div.red {
      height: 100px;
      background-color: red;
      flex: 0 0 auto;
    }
    div.blue {
      height: 150px;
      background-color: blue;
      flex: 0 0 auto;
    }
    /* 
    div.green {
      background-color: green;
      flex: 0 1 auto;
    }
    img
    {
      max-height: 100%;
    }
    */
    
    div.green {
      background-color: green;
      flex: 1 1 auto;
    }
    img {
      height: calc(100vh - 250px);
    }

    revised fiddle

提交回复
热议问题