【问题标题】:CSS responsive img downscale by width and heightCSS响应式img按宽度和高度缩小
【发布时间】:2015-03-01 17:43:29
【问题描述】:

我一直在尝试将我当前的网站布局迁移到响应式布局,但我遇到了以下问题。在我的图片库中,我根据视口在液柱中输出缩略图。我使用媒体查询来为不同的视口实现这一点,并且始终使用宽度作为屏幕宽度的百分比。我有水平和垂直方向的缩略图。缩小效果很好,但在某些时候,当图像开始缩小时,垂直图像变得比水平图像大并且溢出它们的父元素。话虽这么说,我正在寻找一种方法来限制宽度和高度的缩小。例如,如果我使用 width: 16%;高度:0;底部填充:16%;为了实现方形元素,a 希望限制该元素内的 img 在其高度等于父元素高度时相应地缩放。

这个问题有没有唯一的 CSS 解决方案?

这是我的代码:

.photo-list {
	padding: 45px 0 0 0;
    width: 100%;
}

.photo-thumb {
    display: inline-block;
    margin: 2% 2% 2% 2%;
    padding-bottom: 16%;
    width: 16%;
    height: 0;
    vertical-align: top;
    text-align: center;
    background-color: grey;
    }

.photo-thumb img {
    max-width: 100%;
    height: auto;
}

@media only screen and (max-width : 1440px),
only screen and (max-device-width : 1440px){
    .photo-thumb {
    width: 21%;
    padding-bottom: 21%;
    }
}

@media only screen and (max-width : 1080px),
only screen and (max-device-width : 1080px){
    .photo-thumb {
    width: 29.3333%;
    padding-bottom: 29.3333%;
    }
}

@media only screen and (max-width : 530px),
only screen and (max-device-width : 530px){
    .photo-thumb {
    width: 46%;
    padding-bottom: 46%;
    }
}

@media only screen and (max-width : 320px),
only screen and (max-device-width : 320px){
    .photo-thumb {
    width: 96%;
    padding-bottom: 96%;
    }
}
<ul class="photo-list clear">
    <li class="photo-thumb">
        <img alt="somewhere far beyond" src="http://lorempixel.com/300/200/nature/">
    </li>
    <li class="photo-thumb">
        <img alt="into the wild" src="http://lorempixel.com/300/200/city/">
    </li>
    <li class="photo-thumb">
        <img alt="missing summer" src="http://lorempixel.com/200/300/sports/">
    </li>
    <li class="photo-thumb">
        <img alt="light trails" src="http://lorempixel.com/200/300/business/">
    </li>
    <li class="photo-thumb">
        <img alt="living tree" src="http://lorempixel.com/300/200/fashion/">
    </li>
    <li class="photo-thumb">
        <img alt="end of day" src="http://lorempixel.com/300/200/nature/">
    </li>
    <li class="photo-thumb">
        <img alt="ray of light" src="http://lorempixel.com/200/300/city/">
    </li>
</ul>

感谢任何帮助!

干杯, 杰罗姆

【问题讨论】:

  • 您的 sn-p 不起作用,请使用绝对路径。
  • Horen,谢谢你,我已经修好了。

标签: css image responsive-design height liquid


【解决方案1】:

这可能会解决您的问题。在这个 sn-p 中,我认为您可以根据图像的性质为图像分配“垂直”和“水平”类。如果您不知道哪个图像将是垂直和水平的,那么您必须使用一个小 js 来根据图像的 naturalHeight 和 naturalWidth 分配类。

.photo-list {
	padding: 45px 0 0 0;
    width: 100%;
}

.photo-thumb {
    position: relative;
    display: inline-block;
    margin: 2% 2% 2% 2%;
    padding-bottom: 16%;
    width: 16%;
    height: 0;
    vertical-align: top;
    text-align: center;
    }
.thumb-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
.photo-thumb img {
    position: relative;
}
.photo-thumb img.vertical {
    height: 100%;
    width: auto;
}
.photo-thumb img.horizontal {
    width: 100%;
    height: auto;
}

@media only screen and (max-width : 1440px),
only screen and (max-device-width : 1440px){
    .photo-thumb {
    width: 21%;
    padding-bottom: 21%;
    }
}

@media only screen and (max-width : 1080px),
only screen and (max-device-width : 1080px){
    .photo-thumb {
    width: 29.3333%;
    padding-bottom: 29.3333%;
    }
}

@media only screen and (max-width : 530px),
only screen and (max-device-width : 530px){
    .photo-thumb {
    width: 46%;
    padding-bottom: 46%;
    }
}

@media only screen and (max-width : 320px),
only screen and (max-device-width : 320px){
    .photo-thumb {
    width: 96%;
    padding-bottom: 96%;
    }
}
<ul class="photo-list clear">
    <li class="photo-thumb">
        <div class="thumb-container">
            <img class="horizontal" alt="somewhere far beyond" src="http://lorempixel.com/300/200/sports/">
        </div>
    </li>
    <li class="photo-thumb">
        <div class="thumb-container">
            <img class="horizontal" alt="into the wild" src="http://lorempixel.com/300/200/sports/">
        </div>
    </li>
    <li class="photo-thumb">
        <div class="thumb-container">
            <img class="vertical" alt="missing summer" src="http://lorempixel.com/200/300/sports/">
        </div>
    </li>
    <li class="photo-thumb">
        <div class="thumb-container">
            <img class="vertical" alt="light trails" src="http://lorempixel.com/200/300/sports/">
        </div>
    </li>
    <li class="photo-thumb">
        <div class="thumb-container">
            <img class="horizontal" alt="living tree" src="http://lorempixel.com/300/200/sports/">
        </div>
    </li>
    <li class="photo-thumb">
        <div class="thumb-container">
            <img class="horizontal" alt="end of day" src="http://lorempixel.com/300/200/sports/">
        </div>
    </li>
    <li class="photo-thumb">
        <div class="thumb-container">
            <img class="vertical" alt="ray of light" src="http://lorempixel.com/200/300/sports/">
        </div>
    </li>
</ul>

【讨论】:

  • 嗨,Akash,感谢您的回答!我在后端添加了方向作为一个类,它工作正常。我也想知道,因为我必须添加 和一个 包括每个缩略图的标题,为了保持列表容器之间的边距防止跨度溢出,这样做是一个好方法如果跨度太长,列中的下一个元素?
  • 这个Fiddle 展示了一种为每张图片添加标题的方法。您可以添加任意长的字幕。我从答案 sn-p 复制了代码,并没有对其进行编辑,以防万一您不喜欢这种方式。
  • 再次感谢 Akash,不幸的是,这种方法的问题在于,由于标题位于父容器和拇指容器之外,因此标题显示得太远,无法显示水平缩略图。有没有一种方法可以在垂直和水平缩略图的缩略图下方显示相同距离的标题,并且仍然强制响应边距在调整时考虑标题的存在,防止它与列表的其他部分重叠?
  • 嗨@JeromeWalters,我不会说这是最好的方法。这个Fiddle 展示了如何将标题保持在水平图像附近。即使 (caption + image) height <li> 仍将占用标题空间。此版本未使用 JS。如果我们可以使用一些 JS,那么我们可以得到更好的结果来保持&lt;li&gt; 的高度尽可能小。
  • 这里是另一个VERSION,带有 JS 代码,让一切看起来都如你所愿..
猜你喜欢
  • 2013-07-30
  • 2016-08-30
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 2014-08-03
  • 1970-01-01
  • 2013-09-06
相关资源
最近更新 更多