【问题标题】:Prefer shrinking over growing in a flex container with flex-flow: row wrap在使用 flex-flow: row wrap 的 flex 容器中收缩而不是增长
【发布时间】:2020-02-01 13:08:05
【问题描述】:

显示具有以下规格的不同尺寸和比例的图片库:

  1. 图像之间没有空白(边距)。
  2. 尽可能尊重原始比例。
  3. 被链接包围的图片。
  4. 非 JS 解决方案。
  5. 图像可能会被剪裁一点。
  6. 便携式解决方案。
  7. 显示的图像集是随机的。
  8. 图像必须从左到右显示(防止使用列)。

我通过以下 flexbox 解决方案实现了这一点:

section {
  display: flex;
  flex-flow: row wrap;
  justify-content: center;
}
section a {
  flex: auto;
}
section img {
  height: 100%;
  width: 100%;
  object-fit: cover;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Controlling flex growability</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <style>

    </style>
</head>
<body>
    <section>
        <a href="#"><img src="https://placekitten.com/400/195" width="400" height="195" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/256/400" width="256" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/237" width="400" height="237" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/111" width="400" height="111" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/245" width="400" height="245" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/250/400" width="250" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/269" width="400" height="269" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/288/400" width="288" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/234/400" width="234" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/194/400" width="194" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/222/400" width="222" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/192/400" width="192" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/141" width="400" height="141" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/289" width="400" height="289" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/210/400" width="210" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/187" width="400" height="187" alt="Kitty"></a>
    </section>
</body>
</html>

解决方案有效,但是根据窗口的大小,有些图片被放大了太多,我希望每行有更多的元素,即使项目需要缩小更多。

这意味着,而不是:

我更喜欢更高密度的项目,这样图像就不会被放大:

我寻找解决方案来在全球范围内增加每行的元素数量,以便图像不会被放大(或至少不会太大:例如:最大 10%)。

目前我发现的两个 hackish 解决方案是:

解决方案 1

使用 zoom 属性:

section {
  display: flex;
  flex-flow: row wrap;
  justify-content: center;
  zoom: 50%;
}
section a {
  flex: auto;
}
section img {
  height: 100%;
  width: 100%;
  object-fit: cover;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Controlling flex growability</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <style>

    </style>
</head>
<body>
    <section>
        <a href="#"><img src="https://placekitten.com/400/195" width="400" height="195" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/256/400" width="256" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/237" width="400" height="237" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/111" width="400" height="111" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/245" width="400" height="245" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/250/400" width="250" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/269" width="400" height="269" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/288/400" width="288" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/234/400" width="234" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/194/400" width="194" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/222/400" width="222" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/192/400" width="192" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/141" width="400" height="141" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/289" width="400" height="289" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/210/400" width="210" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/187" width="400" height="187" alt="Kitty"></a>
    </section>
</body>
</html>

但该属性在 Chrome 中效果很好,而不是在 Firefox 中。

解决方案 2

width/heighttransform: scale:

模拟 zoom 属性

section {
  display: flex;
  flex-flow: row wrap;
  justify-content: center;
  width: 200% !important;
  height: 200% !important;
  transform-origin: 0 0;
  transform: scale(0.5);
  float: left;
  margin-right: -100000px;
  margin-bottom: -100000px;
}
section a {
  flex: auto;
}
section img {
  height: 100%;
  width: 100%;
  object-fit: cover;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Controlling flex growability</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <style>

    </style>
</head>
<body>
    <section>
        <a href="#"><img src="https://placekitten.com/400/195" width="400" height="195" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/256/400" width="256" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/237" width="400" height="237" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/111" width="400" height="111" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/245" width="400" height="245" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/250/400" width="250" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/269" width="400" height="269" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/288/400" width="288" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/234/400" width="234" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/194/400" width="194" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/222/400" width="222" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/192/400" width="192" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/141" width="400" height="141" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/289" width="400" height="289" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/210/400" width="210" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/187" width="400" height="187" alt="Kitty"></a>
    </section>
</body>
</html>

到目前为止,该解决方案有效,但需要一些技巧,而且远非优雅,现在将对页面的其他元素产生影响。

还有其他解决方案,更面向 flexgrid 允许这种控制吗? 我尝试过使用 flex-grow: 0:确实,它会禁用不断增长的项目,但是图像周围到处都是空白。

【问题讨论】:

    标签: css flexbox image-gallery


    【解决方案1】:

    我修改了你的开始尝试。

    主要思路是将imgwidth: 100%;改为width: auto;,并指定链接的height。这将为我们提供有间隙的图像。

    要消除我们可以添加到链接display: flex;flex-direction: column; 的空白。快完成了。

    最后一步是添加到链接max-width: 100%;,如果图像width 比小屏幕中的列宽,它将防止ovelflow。 如果我们放置更高的height 链接,我们可以在 Temani Afif 的第 4 个图像的第一个解决方案中看到这样的问题。已编辑

    查看 sn-p。

    section {
      display: flex;
      flex-flow: row wrap;
      justify-content: center;
    }
    
    section a {
      flex: auto;
      display: flex;
      flex-direction: column;
      height: 166px;
      max-width: 100%;
    }
    
    section img {
      height: 100%;
      width: auto;
      object-fit: cover;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <title>Controlling flex growability</title>
      <meta name="viewport" content="width=device-width,initial-scale=1">
      <style>
    
      </style>
    </head>
    
    <body>
      <section>
        <a href="#"><img src="https://placekitten.com/400/195" width="400" height="195" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/256/400" width="256" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/237" width="400" height="237" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/111" width="400" height="111" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/245" width="400" height="245" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/250/400" width="250" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/269" width="400" height="269" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/288/400" width="288" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/234/400" width="234" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/194/400" width="194" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/222/400" width="222" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/192/400" width="192" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/141" width="400" height="141" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/289" width="400" height="289" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/210/400" width="210" height="400" alt="Kitty"></a>
        <a href="#"><img src="https://placekitten.com/400/187" width="400" height="187" alt="Kitty"></a>
      </section>
    </body>
    
    </html>

    【讨论】:

    • 这是迄今为止最好的答案。它不会给出与使用 zoom 相同的结果,因为它通过这样做将所有行固定为具有完全相同的高度:它会阻止自然 flex 算法“适应”“尽可能优化”的东西。据我了解,目前还不可能以兼容且非 hacky 的方式实现我想要的。
    【解决方案2】:

    这是一个想法,您可以考虑高度来控制行的大小,主要技巧是依靠min-width:100% 为您的图像填充空间。

    基本上,a 将定义高度,图像将遵循该高度并计算auto 宽度以保持比率。图像的宽度将定义链接的宽度,然后链接将增长以填充空间(在其中创建空间)。最后,min-width:100% 你将使图像填充链接内部创建的空间。

    section {
      display: flex;
      flex-flow: row wrap;
      justify-content: center;
    }
    
    section a {
      flex: auto;
      height: 100px;
    }
    
    section img {
      height: 100%;
      width: auto; /* we need auto to keep the ratio based on the height */
      min-width: 100%; /* we expand the image to fill the gaps */
      max-width:100%;
      object-fit: cover;
    }
    <section>
      <a href="#"><img src="https://placekitten.com/400/195" width="400" height="195" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/256/400" width="256" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/237" width="400" height="237" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/111" width="400" height="111" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/245" width="400" height="245" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/250/400" width="250" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/269" width="400" height="269" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/288/400" width="288" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/234/400" width="234" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/194/400" width="194" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/222/400" width="222" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/192/400" width="192" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/141" width="400" height="141" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/289" width="400" height="289" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/210/400" width="210" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/187" width="400" height="187" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/288/400" width="288" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/234/400" width="234" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/194/400" width="194" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/222/400" width="222" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/192/400" width="192" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/141" width="400" height="141" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/289" width="400" height="289" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/210/400" width="210" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/187" width="400" height="187" alt="Kitty"></a>
    </section>

    如果您将vw 视为高度单位,您将拥有一个静态网格,该网格将在保持相同整体结构的同时进行缩放:

    section {
      display: flex;
      flex-flow: row wrap;
      justify-content: center;
    }
    
    section a {
      flex: auto;
      height: 8vw;
    }
    
    section img {
      height: 100%;
      width: auto;
      min-width: 100%;  
      max-width:100%;
      object-fit: cover;
    }
    <section>
      <a href="#"><img src="https://placekitten.com/400/195" width="400" height="195" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/256/400" width="256" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/237" width="400" height="237" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/111" width="400" height="111" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/245" width="400" height="245" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/250/400" width="250" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/269" width="400" height="269" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/288/400" width="288" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/234/400" width="234" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/194/400" width="194" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/222/400" width="222" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/192/400" width="192" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/141" width="400" height="141" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/289" width="400" height="289" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/210/400" width="210" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/187" width="400" height="187" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/288/400" width="288" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/234/400" width="234" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/194/400" width="194" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/222/400" width="222" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/192/400" width="192" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/141" width="400" height="141" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/289" width="400" height="289" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/210/400" width="210" height="400" alt="Kitty"></a>
      <a href="#"><img src="https://placekitten.com/400/187" width="400" height="187" alt="Kitty"></a>
    </section>

    【讨论】:

      【解决方案3】:

      解决此问题的一种方法是将line-height: 0 添加到a 并将height 值设置为px 值。

      section {
        display: flex;
        flex-flow: row wrap;
        justify-content: center;
      }
      section a {
        flex: auto;
        line-height: 0;
      }
      section img {
        height: 300px;
        width: 100%;
        object-fit: cover;
      }
      <!DOCTYPE html>
      <html lang="en">
      <head>
          <title>Controlling flex growability</title>
          <meta name="viewport" content="width=device-width,initial-scale=1">
          <style>
      
          </style>
      </head>
      <body>
          <section>
              <a href="#"><img src="https://placekitten.com/400/195" width="400" height="195" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/256/400" width="256" height="400" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/400/237" width="400" height="237" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/400/111" width="400" height="111" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/400/245" width="400" height="245" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/250/400" width="250" height="400" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/400/269" width="400" height="269" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/288/400" width="288" height="400" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/234/400" width="234" height="400" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/194/400" width="194" height="400" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/222/400" width="222" height="400" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/400/227" width="400" height="227" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/192/400" width="192" height="400" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/400/141" width="400" height="141" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/400/289" width="400" height="289" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/400/255" width="400" height="255" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/210/400" width="210" height="400" alt="Kitty"></a>
              <a href="#"><img src="https://placekitten.com/400/187" width="400" height="187" alt="Kitty"></a>
          </section>
      </body>
      </html>

      【讨论】:

      • 这会改变每一行的高度,它不允许每行有更多的项目,就像缩放效果或等效的黑客一样。
      猜你喜欢
      • 1970-01-01
      • 2017-07-16
      • 2021-08-27
      • 1970-01-01
      • 2017-12-03
      • 2021-01-01
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多