代码之家  ›  专栏  ›  技术社区  ›  Code Spirit

最大高度填充

  •  0
  • Code Spirit  · 技术社区  · 7 年前

    max-height 容器定义其高度,但最大高度不超过更高分辨率的限制。 最大高度

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
      <style>
        .wrapper {
          width: 100%;
          padding-bottom: 39.65%;
          max-height: 200px;
          height: 0;
          box-sizing: border-box;
        }
      </style>
    </head>
    <body>
      <div class="wrapper">
        <img src="https://image.shutterstock.com/image-vector/sample-red-square-grunge-stamp-260nw-338250266.jpg">
      </div>
    </body>
    </html>
    

    编辑: height 具有 vw .

    .wrapper {
      height: 39.65vw;
      max-height: 200px;
      /* ... */
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Fabrizio Calderan    7 年前

    max-height padding 是两个不同的属性。最后一个不会受到第一个的限制或影响。

    所以,为了限制盒子的总高度,你需要计算 200px 39.65% 你父母的宽度。假设这是宽度为 ~504px . 对于mediaquery,您只需将padding bottom设置为 200像素

    .wrapper {
       width: 100%;
       padding-bottom: 39.65%;
       height: 0;
       box-sizing: border-box;
    }
    
    @media all and (min-width: 504px) {
       padding-bottom: 200px;
    }