响应地改变div尺寸保持纵横比

时间:2012-08-25 10:13:23

标签: html css responsive-design

当我给图像一个百分比宽度或高度时,它只会保持其纵横比增长/收缩,但如果我想要与另一个元素相同的效果,是否可以使用百分比将宽度和高度联系在一起?

5 个答案:

答案 0 :(得分:524)

你可以使用纯CSS来做到这一点;不需要JavaScript。这利用padding-top percentages are relative to the containing block's width的(有点违反直觉的)事实。这是一个例子:

.wrapper {
  width: 50%;
  /* whatever width you want */
  display: inline-block;
  position: relative;
}
.wrapper:after {
  padding-top: 56.25%;
  /* 16:9 ratio */
  display: block;
  content: '';
}
.main {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  /* fill parent */
  background-color: deepskyblue;
  /* let's see it! */
  color: white;
}
<div class="wrapper">
  <div class="main">
    This is your div with the specified aspect ratio.
  </div>
</div>

答案 1 :(得分:44)

克服克里斯的想法,另一个选择是使用伪元素,这样你就不需要使用绝对定位的内部元素。

<style>
.square {
    /* width within the parent.
       can be any percentage. */
    width: 100%;
}
.square:before {
    content: "";
    float: left;

    /* essentially the aspect ratio. 100% means the
       div will remain 100% as tall as it is wide, or
       square in other words.  */
    padding-bottom: 100%;
}
/* this is a clearfix. you can use whatever
   clearfix you usually use, add
   overflow:hidden to the parent element,
   or simply float the parent container. */
.square:after {
    content: "";
    display: table;
    clear: both;
}
</style>
<div class="square">
  <h1>Square</h1>
  <p>This div will maintain its aspect ratio.</p>
</div>

我在这里整理了一个演示:http://codepen.io/tcmulder/pen/iqnDr


编辑:

现在,从艾萨克的想法中汲取灵感,现代浏览器更容易使用vw单位来强制纵横比(尽管我也不会像他那样使用vh或者方面比率将根据窗口高度而变化。

所以,这简化了事情:

<style>
.square {
    /* width within the parent (could use vw instead of course) */
    width: 50%;
    /* set aspect ratio */
    height: 50vw;
}
</style>
<div class="square">
  <h1>Square</h1>
  <p>This div will maintain its aspect ratio.</p>
</div>

我在这里整理了一个经过修改的演示:https://codepen.io/tcmulder/pen/MdojRG?editors=1100

你也可以设置max-height,max-width和/或min-height,min-width如果你不希望它变得大得可笑,因为它基于浏览器的宽度现在而不是容器,并且会无限增长/缩小。

请注意,如果您将字体大小设置为vw测量值以及所有内部测量值,您也可以缩放元素内的内容,这里有一个演示:https://codepen.io/tcmulder/pen/VBJqLV?editors=1100

答案 2 :(得分:29)

<style>
#aspectRatio
{
  position:fixed;
  left:0px;
  top:0px;
  width:60vw;
  height:40vw;
  border:1px solid;
  font-size:10vw;
}
</style>
<body>
<div id="aspectRatio">Aspect Ratio?</div>
</body>

此处需要注意的关键是vw =视口宽度,vh =视口高度

答案 3 :(得分:3)

那是我的解决方案

<div class="main" style="width: 100%;">
    <div class="container">
        <div class="sizing"></div>
        <div class="content"></div>
    </div>
</div>

.main {
    width: 100%;
}
.container {
    width: 30%;
    float: right;
    position: relative;
}
.sizing {
    width: 100%;
    padding-bottom: 50%;
    visibility: hidden;
}
.content {
    width: 100%;
    height: 100%;
    background-color: red;
    position: absolute;
    margin-top: -50%;
}

http://jsfiddle.net/aG4Fs/3/

答案 4 :(得分:2)

(function( $ ) {
  $.fn.keepRatio = function(which) {
      var $this = $(this);
      var w = $this.width();
      var h = $this.height();
      var ratio = w/h;
      $(window).resize(function() {
          switch(which) {
              case 'width':
                  var nh = $this.width() / ratio;
                  $this.css('height', nh + 'px');
                  break;
              case 'height':
                  var nw = $this.height() * ratio;
                  $this.css('width', nw + 'px');
                  break;
          }
      });

  }
})( jQuery );      

$(document).ready(function(){
    $('#foo').keepRatio('width');
});

工作示例:http://jsfiddle.net/QtftX/1/

相关问题