当宽度恒定时,根据窗口高度使图像响应

时间:2016-05-12 05:50:24

标签: html css

CodePen上的示例:http://codepen.io/seadrag0n/pen/oxVJgX

我正在学习响应式设计而不使用像bootstrap这样的库,我在弹出窗口中显示大图像时遇到问题,应该同时响应高度和宽度。我有以下代码段:



* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.outer {
  margin-left: auto;
  margin-right: auto;
  max-width: 1200px;
  border: 1px solid #333;
  position: relative;
  top: 50%;
}

.inner {
  width: 100%;
  height: auto;
  position: absolute;
}

.inner img {
  max-width: 100%;
  height: auto!important;
  position: absolute;
}

<div class="outer">
  <div class="inner">
    <img src="http://www.cameraegg.org/wp-content/uploads/2015/06/Sony-RX100-IV-Sample-Images-2.jpg" />
  </div>
</div>
&#13;
&#13;
&#13;

使用上面的标记,图片响应width但不响应height,在1920 x 1080分辨率的屏幕中,图片看起来不错,但当我切换到{{1}时分辨率,图像溢出而不是调整到屏幕大小。我该如何解决这个问题?

我希望实现与this插件中相同的样式,如果保持宽度相同并减小浏览器窗口的高度,即使宽度相同,图像也会调整到屏幕大小

2 个答案:

答案 0 :(得分:0)

检查此CodePen:http://codepen.io/uzumakinaruto1/pen/QNozyr

&#13;
&#13;
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.outer {
  max-width: 1200px;
  border: 1px solid #333;
  position: relative;
  top: 50%;
}

.inner {
  width: 100%;
  height: 100%;
  position: fixed;
  overflow: hidden;
}

.inner img {
  width: 100%;
  height: 100%;
  position: absolute;
}
&#13;
<div class="outer">
  <div class="inner">
    <img src="http://www.cameraegg.org/wp-content/uploads/2015/06/Sony-RX100-IV-Sample-Images-2.jpg" />
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

demo

只需使用height 100vh

即可

&#13;
&#13;
.outer {
  margin-left: auto;
  margin-right: auto;
  max-width: 1200px;
  border: 1px solid #333;
  position: relative;
  top: 50%;
}

.inner {
  width: 100%;
  height: auto;
  position: absolute;
}

.inner img {
  height: 100vh;
  max-width: 100%;
  position: absolute;
}
&#13;
<div class="outer">
  <div class="inner">
    <img src="https://c7.staticflickr.com/3/2538/3742771246_2648fa4e6e_b.jpg" />
  </div>
</div>
&#13;
&#13;
&#13;