响应图像的垂直填充

时间:2015-07-07 23:26:36

标签: javascript css css3

这个 CSS:

.outer-img-wrap {
    border: 2px solid gray;
    margin: 1vw auto;
    max-width: 192px;
    text-align: center;
    overflow: hidden;
}
.outer-img-wrap img {
    width: auto !important;
    height: auto !important;
    max-width: 100%;
    vertical-align: middle;
}
.inner-img-wrap {
    background: #000;
    border: thin solid #ff9900;
    margin: 2px;
}

已应用于此 HTML:

<td style="width: 25%">
    <div class="outer-img-wrap">
        <div class="inner-img-wrap">
            <img src="http://placehold.it/64x64" />
        </div>
    </div>
</td>

在适当宽度的表格单元格中生成居中的响应图像。所有图像最终都具有相同的宽度,这就是我想要的。

我还希望图像以响应的方式具有相同的高度。所以我在页面中添加了 Javascript 来更新填充。

function resizeImageElements()
{
    var imageElements = $(".outer-img-wrap .inner-img-wrap img");
    var imageElementsMaxHeight = -1;
    imageElements.map( function(index) {
        // compare the height of the img element
        if( $(this).height() > imageElementsMaxHeight ) {
            imageElementsMaxHeight = $(this).height();
        }
    } );

    imageElements.map( function(index) {
        var computeTopBottomPadding = ( imageElementsMaxHeight - $(this).height() ) / 2;
        $(this).css( {
            "padding-top": computeTopBottomPadding,
            "padding-bottom": computeTopBottomPadding,
        } );
    } );
}

resizeImageElements();

问题是:如果没有Javascript代码,我可以达到相同的效果;只是使用CSS?

小提琴位于http://jsfiddle.net/ybkgLq4d/

2 个答案:

答案 0 :(得分:0)

嗯,目前尚不清楚你从哪里拍摄这些照片。如果您能够在后端服务器中处理这些图像(使用PHP,Python或ruby),您可以始终使用固定比率创建它们的版本,这样在前端调整大小时它们将具有相同的尺寸。这将是我最喜欢的选择。

如果你不能或不想创建不同版本的图像,我相信你最好的选择是使用带有背景图像的元素而不是图像元素本身,并使用背景大小来适应你的需求。

例如:

.outer-img-wrap {
    border: 2px solid gray;
    margin: 1vw auto;
    max-width: 192px;
    text-align: center;
    overflow: hidden;
}
.outer-img-wrap .img {
    display:inline-block;
    position:relative;
    vertical-align: middle;
    width:100%;
    background-position:center center;
    height:50px; /* or whatever height you want, you could use % value too relative to it's parent element */

    /* cross browser */
    -moz-background-size:cover;
    -o-background-size:cover;
    -ms-background-size:cover;
    -webkit-background-size:cover;
    background-size:cover;
}
.inner-img-wrap {
    background: #000;
    border: thin solid #ff9900;
    margin: 2px;
}

应用于此html:

<td style="width: 25%">
    <div class="outer-img-wrap">
        <div class="inner-img-wrap">
            <div class="img" style="background-image:url(http://placehold.it/64x64)"></div>
        </div>
    </div>
</td>

小提琴:https://jsfiddle.net/ybkgLq4d/14/

答案 1 :(得分:0)

我尝试保留简单的标记,并与:beforebox-shadow以及calc进行了一些比较。

&#13;
&#13;
* {
  box-sizing: border-box;
}
.pic {
  background-color: black;
  border: 2px solid gray;
  box-shadow: inset 2px 2px 0 0 white, inset -2px -2px 0 0 white, inset 3px 3px 0 0 orange, inset -3px -3px 0 0 orange;
  max-width: 192px;
  position: relative;
  width: 25%;
}
.pic:before {
  content: '';
  display: block;
  padding-bottom: 100%;
}
img {
  display: block;
  left: 50%;
  max-width: calc(100% - 6px);
  position: absolute;
  -webkit-transform: translate3d(-50%, -50%, 0);
  transform: translate3d(-50%, -50%, 0);
  top: 50%;
}

/* for layout */

figure {
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
}
figcaption {
  -webkit-flex: 1;
  -ms-flex: 1;
  flex: 1;
  padding-left: 1em;
}
&#13;
<figure>
  <div class="pic">
    <img src="http://placehold.it/64x64">
  </div>
  <figcaption>text text text
    <figcaption>
</figure>
<figure>
  <div class="pic">
    <img src="http://placehold.it/400x300">
  </div>
  <figcaption>text text text
    <figcaption>
</figure>
<figure>
  <div class="pic">
    <img src="http://placehold.it/128x128">
  </div>
  <figcaption>text text text
    <figcaption>
</figure>
&#13;
&#13;
&#13;

相关问题