固定缩略图比例大小并保持纵横比?

Fixed thumbnail ratio size & maintaining aspect ratio?

有一些问题很相似,但我无法将其与我的具体情况联系起来。

我创建了一个具有 4 对 3 对 2 列结构的响应式缩略图网格(百分比为 25%-33,33%-50%)。

我正在使用 500x330 像素的假占位符图像进行测试,但我想要一个固定的缩略图占位符(css 代码?),响应比例为 500x330,并保持图像的纵横比放置在居中(垂直和水平)占位符缩略图。

在 fiddle 示例中,我在 photoshop 中制作了一些缩略图,这些缩略图具有 500x330 像素的大小以及不同的纵向和横向尺寸,因此它并不是真正的横向图像的肖像,但它提供了一个想法我想要达到的目标。我希望有人理解我的问题并能帮助我或指导我。

--> Fiddle

<div class="col-4">
    <a class="thumb" href="#">
        <img src="img/test2.jpg">
        <div class="caption">Project untitled<br> Category</div>
    </a>
</div>

我认为 This 是您想要达到的结果。

我在每个拇指周围添加了一个 div.img-wrapper 和一些 css 魔法

HTML

<div id="container">
    <div class="col-4">
        <div class="img-wrapper">   <a class="thumb" href="#">
            <img src="http://mintywhite.com/wp-content/uploads/2012/10/fond-ecran-wallpaper-image-arriere-plan-hd-29-HD.jpg">
            <div class="caption">Project untitled<br> Category</div>
        </a>

        </div>
    </div>
    <div class="col-4">
        <div class="img-wrapper">   <a class="thumb" href="#">
            <img src="http://fakeimg.pl/500x330/ccc/">
            <div class="caption">Project untitled<br> Category</div>
        </a>

        </div>
    </div>
    <div class="col-4">
        <div class="img-wrapper">   <a class="thumb" href="#">
            <img src="http://www.gettyimages.com/CMS/Pages/ImageCollection/StaticContent/image1_%20164248809.jpg">
            <div class="caption">Project untitled<br> Category</div>
        </a>

        </div>
    </div>
    <div class="col-4">
        <div class="img-wrapper">   <a class="thumb" href="#">
            <img src="http://fakeimg.pl/500x330/ccc/">
            <div class="caption">Project untitled<br> Category</div>
        </a>

        </div>
    </div>
    <div class="col-4">
        <div class="img-wrapper">   <a class="thumb" href="#">
            <img src="http://fakeimg.pl/500x330/ccc/">
            <div class="caption">Project untitled<br> Category</div>
        </a>

    </div>
</div>
<div class="col-4">
    <div class="img-wrapper">   <a class="thumb" href="#">
            <img src="http://upload.wikimedia.org/wikipedia/commons/f/f5/Poster-sized_portrait_of_Barack_Obama.jpg">
            <div class="caption">Project untitled<br> Category</div>
        </a>

    </div>
</div>
<div class="col-4">
    <div class="img-wrapper">   <a class="thumb" href="#">
            <img src="http://fakeimg.pl/500x330/ccc/">
            <div class="caption">Project untitled<br> Category</div>
        </a>

    </div>
</div>
<div class="col-4">
    <div class="img-wrapper">   <a class="thumb" href="#">
            <img src="http://fakeimg.pl/500x330/ccc/">
            <div class="caption">Project untitled<br> Category</div>
        </a>

    </div>
</div>
</div>

已更改 CSS#

.col-4 {
    position: relative;
    float: left;
    display: inline-block;
}

.col-4:after {
    padding-top: 63%;
    display: block;
    content:'';
}
.img-wrapper {
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    left: 0;
    /*fill parent*/
}
.img-wrapper .thumb {
    height:100%;

.img-project img, .thumb img {
    position:relative;
    top:50%;
    left:50%;
    max-width:100%;
    max-height:100%;
    -webkit-transform:translate(-50%,-50%);
    -moz-transform:translate(-50%,-50%);
    -ms-transform:translate(-50%,-50%);
    -o-transform:translate(-50%,-50%);
    transform: translate(-50%,-50%);
}

已满CSS

/************************************************
Site Name: 
Author: 
************************************************/
 html {
    margin: 0;
    padding: 0;
}
body {
    font-family: regular, helvetica, arial, sans-serif;
    font-weight: normal;
    font-size: 18px;
    line-height: 1.4;
    text-transform: none;
    letter-spacing: 0;
    color: #111;
    overflow-y: scroll;
}
body, input, textarea, select, button {
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: subpixel-antialiased;
    -moz-osx-font-smoothing: grayscale;
}
:hover {
    -webkit-transition: all 0.3s;
    -moz-transition: all 0.3s;
    -ms-transition: all 0.3s;
    -o-transition: all 0.3s;
    transition: all 0.3s;
}
b, strong {
    font-weight: normal;
}
a, a:visited {
    color: #111;
    text-decoration: underline;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    -webkit-tap-highlight-color: transparent;
}
h1, nav {
    font-size: 27px;
    font-weight: normal;
    text-transform: uppercase;
    text-decoration: none;
    letter-spacing: 2px;
    background-color: transparent;
}
h1 {
    margin: 0 0 26px 0;
}
p {
    margin: 0 0 16px 0;
    background-color: transparent;
}
p a:hover, a:focus, a:active {
    color: #111;
    text-decoration: none;
}
h1 a, a:visited {
    color: #111;
    text-decoration: none;
}
h1 a:hover, a:focus, a:active {
    color: #111;
    text-decoration: underline;
}
/************************************************
Thumbnails - Columns - Content
************************************************/
 #container {
    display: block;
    margin: 98px 10px 100px 10px;
    overflow: hidden;
    background-color: transparent;
}
.col-4 {
    position: relative;
    float: left;
    display: inline-block;
}
.col-4 {
    width: 25%;
}
.col-4:after {
    padding-top: 63%;
    display: block;
    content:'';
}
.img-wrapper {
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    left: 0;
    /*fill parent*/
}
.img-wrapper .thumb {
    height:100%;
}
.img-project, .thumb {
    display: block;
    padding: 10px;
}
.img-project img, .thumb img {
    position:relative;
    top:50%;
    left:50%;
    max-width:100%;
    max-height:100%;
    -webkit-transform:translate(-50%,-50%);
    -moz-transform:translate(-50%,-50%);
    -ms-transform:translate(-50%,-50%);
    -o-transform:translate(-50%,-50%);
    transform: translate(-50%,-50%);
}
.thumb:hover img {
    opacity: 0.2;
}
.caption {
    position: absolute;
    opacity: 0;
    width: 80%;
    padding: 0;
    top: 50%;
    left: 50%;
    text-align: center;
    background-color: transparent;
    -webkit-transform: translateX(-50%) translateY(-50%);
    -moz-transform: translateX(-50%) translateY(-50%);
    transform: translateX(-50%) translateY(-50%);
    -webkit-transition: all 0.3s;
    -moz-transition: all 0.3s;
    -ms-transition: all 0.3s;
    -o-transition: all 0.3s;
    transition: all 0.3s;
}
.col-4 a:hover .caption {
    opacity: 1;
}
/************************************************
Media queries
************************************************/
 @media all and (max-width: 1024px) {
    h1, nav {
        font-size: 22px;
    }
    #container {
        margin: 89px 7px 100px 7px;
    }
    .img-project, .thumb {
        padding: 7px;
    }
}
@media all and (min-width: 769px) {
    nav ul {
        display: block !important;
    }
}
@media all and (max-width: 768px) {
    body {
        font-size: 16px;
    }
    .col-4 {
        width: 33.33333%;
    }
}
@media all and (max-width: 479px) {
    .col-4 {
        width: 50%;
    }
}

更新 1

我计算的纵横比可能是 1:0.63 有点不对。您应该使用 :after 元素的 padding-top 来达到所需的比率

更新 2

如您所问,添加了填充 - Fiddle 我刚刚将其从 .thumb class 中删除并添加到 .img-wrapper class.