Bootstrap - 如何缩小图像以适应窗口高度?

时间:2016-02-10 14:44:24

标签: javascript jquery html css twitter-bootstrap-3

如何使图像溢出高度响应?

我的图像有时会有很长的高度,使窗口滚动条可见。

enter image description here

所以我想让这些高度响应 - 使图像按比例缩小以适应窗口高度。

enter image description here

有可能吗?

CSS,

 html, body {
    height: 100%;
    margin: 0;
    padding: 0 0;
    border: 4px solid black;
}

.container-fluid {
    height: 100%;
    display: table;
    width: 100%;
    padding-right: 0;
    padding-left: 0;
    border: 4px solid blue;
}

.row-fluid {
    height: 100%;
    display: table-cell;
    vertical-align: middle;
    width: 100%;

    border: 4px solid red;
}

.centering {
    float: none;
    margin: 0 auto;
}

HTML,

<div class="container-fluid">
    <div class="row-fluid text-center">
        <div>
            <img src="images/xxx.jpg" class="img-responsive centering">
        </div>
    </div>
</div>

2 个答案:

答案 0 :(得分:2)

您可以在height: 100%上使用vertical-align: topimg,在元素上使用height: 100vh

body, html {
  margin: 0;
  padding: 0;
}

div {
  display: inline-block;
  height: 100vh;
  width: 100%;
  text-align: center;
}

img {
  height: 100%;
  vertical-align: top;
}
<div>
  <img src="http://placehold.it/400x850">
</div>

答案 1 :(得分:1)

用内置的.centering bootstrap

替换.center-block

然后添加一个新类.myimg

.myimg {
  margin: 0 auto;
  position: absolute;
  height:100%;
  top:0;
  bottom:0;
}

检查这个小提琴https://jsfiddle.net/wo2eysh8/3/