【问题标题】:CSS - Aspect Ratio and 100% viewport heightCSS - 纵横比和 100% 视口高度
【发布时间】:2018-12-18 07:09:44
【问题描述】:

我正在尝试像这样将 div 的纵横比设置为纵向 (9:16)..

.ratio {
  position: relative;
  height: 0;
  width: 100%;
  background: teal;
  padding-bottom: 177.77%;
}
<div class="ratio">
  16:9
</div>

这是按预期工作的,但我现在想要使它的最大高度为视口的 100%。

我试图确保它完全适合屏幕,同时保持纵横比并且不引入滚动条。

我最好的方法是什么?

【问题讨论】:

  • 这和我的例子一样,我正在尝试设置最大高度
  • 你的意思是:max-height: 100vh;?
  • 已经尝试过max-height,但没有任何区别
  • 如果我理解你的话,我认为 min 不是 max

标签: css


【解决方案1】:

我建议然后使用vh 单位作为高度。

height: 100vh;

【讨论】:

    【解决方案2】:

    您需要使用16/9 = 1.778 的比率来使用calc() css 函数计算宽度

    body {
      margin: 0;
    }
    
    .ratio {
      position: relative;
      height: 100vh;
      background: teal;
      width: calc(1.778 * 100vh);
      margin: auto;
    }
    <div class="ratio">
      16:9
    </div>

    【讨论】:

      猜你喜欢
      • 2013-09-19
      • 2019-12-03
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 2018-02-13
      • 2016-12-20
      • 2018-06-21
      相关资源
      最近更新 更多