固定比例盒子

要创建具有固定比例的一个盒子,所有你需要做的就是给 div 的顶部或底部设置一个 padding:

  1. .container {
  2. height: 0;
  3. padding-bottom: 20%;
  4. position: relative;
  5. }
  6. .container div {
  7. border: 2px dashed #ddd;
  8. height: 100%;
  9. left: 0;
  10. position: absolute;
  11. top: 0;
  12. width: 100%;
  13. }

使用 20% 的 padding-bottom 使得框等于其宽度的 20% 的高度。与视口宽度无关,子元素的 div 将保持其宽高比(100%/ 20%= 5:1)。

演示