【问题标题】:Fluid image that is not width:100%非宽度的流体图像:100%
【发布时间】:2016-05-26 04:02:15
【问题描述】:

我有一个场景,不同尺寸的图像出现在页面宽度为 50% 的列中。

在列宽超过图像的原生宽度的大屏幕上,图像应呈现为其原生宽度,同时仍保持流畅。

图像尺寸各不相同(即横向与纵向),因此不能将单一宽度或高度应用于 img 元素。我可以将父元素限制为与将要显示的最大图像宽度相匹配的最大宽度,但是宽度较小的图像一旦变得流畅就会扩展得太宽。

基本结构是:

<div class="column">
  <figure>
    <img>
  </figure>
</div>

使用这个 CSS:

.column {width:50%;}
.column figure {
    display:block;
    margin:0 auto;
    width:100%;
    max-width:800px; /* the largest image width */
    text-align: center; /* to center images of lesser width */
}
.column img {?}

我可以向 img 元素添加一个指示方向的数据属性,使用它来应用最大宽度,但这需要站点编辑器进行更多工作,我需要避免这样做。因此,我寻求一个纯 CSS 的解决方案……但我被困住了。

有什么想法吗?

【问题讨论】:

标签: css fluid-images


【解决方案1】:

如何再次使用max-width,让图像完全扩展到其容器的宽度,但不能更大?比如:

.column img {
    max-width: 100%;
}

这将允许图像根据需要响应 - 随其父级的大小缩放,但永远不会超过其原生宽度。这是一个包含一些随机图像的演示 - 请注意,当给定足够的空间时,原生较小和较大的图像如何在不同的宽度处停止:

.column {
  width: 50%;
}
.column figure {
  display: block;
  margin: 0 auto;
  width: 100%;
  max-width: 800px;
  /* the largest image width */
  text-align: center;
  /* to center images of lesser width */
}
.column img {
  max-width: 100%;
}
<div class="column">
  <figure>
    <img src="http://www.ltsgrill.com/wp-content/uploads/2014/09/red_lobster.jpg" />
  </figure>
</div>

<div class="column">
  <figure>
    <img src="http://www.mjseafood.com/_assets/400x300/Crayfish.jpeg" />
  </figure>
</div>

【讨论】:

  • 啊,用龙虾打我。我发誓我以为我试过了,但我想没有。谢谢!
  • @WesleyPicotte 很高兴我能帮上忙!有时只需要第二双眼睛就能发现这样的事情。作为使用 Stack Overflow 的提示,当您得到一个满意地解决您的问题的答案时,您可以通过单击它旁边的复选标记来接受它 - 这向其他人表明该问题已得到回答。 (如果这些是你的东西,你会得到一些互联网积分,哈哈。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-25
  • 2015-06-21
  • 1970-01-01
  • 2011-06-05
  • 2012-08-06
  • 1970-01-01
  • 2014-09-18
相关资源
最近更新 更多