如何使图像缩小以适应 flex 列?

huangapple go评论61阅读模式
英文:

How do I make image shrink to flex column?

问题

我正在尝试创建一个包含图像的8列弹性网格,在8个项目后换行。我希望图像保持列的宽度。然而,图像保持其本机大小,而不会缩小到列的大小,列的大小通过flex-basis设置为12.5%。

我尝试为图像添加max-width,但它们仍然保持其本机大小。

我的问题是:无论图像有多大,我如何将它们限制在其12.5%的列内?

这是一个CodePen示例

.container {
  max-width: 80%;
}

.flex-grid {
  display: flex;
  flex-wrap: wrap;
  flex-basis: 12.5%;
}

.flex-img {
  max-width: 100%;
  padding: 10px;
  max-width: 100%;
}
<div class="container">
  <div class="flex-grid">
    <div class="flex-img">
      <a href="https://placeholder.com"><img src="https://via.placeholder.com/150"></a>
    </div>
    <!-- 其他图像项目 -->
  </div>
</div>
英文:

I'm trying to create an 8-column flex grid that contains images that line breaks after 8 items. I want the images to stay the width of the column. However, the image is holding onto its native size and not reducing to the size of the column, which is set a 12.5% with flex-basis

I've tried adding a max-width to the image but they still hold onto their native size.

My question is; how can I contain images within their 12.5% column regardless of how big the image is?

<a href="https://codepen.io/mrthomason/pen/KKxMXrK">Here's a codepen</a>

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
  max-width: 80%
}

.flex-grid {
  display: flex;
  flex-wrap: wrap;
  flex-basis: 12.5%;
}

.flex-img {
  max-width 100%;
  padding: 10px;
  max-width 100%;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;flex-grid&quot;&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/350&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 0

在大多数情况下,元素的大小来自外部(父元素),并且不依赖于固有大小时,通常更好的做法是使用grid。以下是我编辑您的代码以使其正常工作的方式。

.container {
  max-width: 80%;
}

.grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
}

.img-wrapper {
  padding: 10px;
}

.img-wrapper img {
  object-fit: cover;
  width: 100%;
  max-height: 100%;
}
<div class="container">
  <div class="grid">
    <div class="img-wrapper">
      <a href="https://placeholder.com">
        <img src="https://via.placeholder.com/150">
      </a>
    </div>
    <!-- 其余图像部分请参考上面的示例,省略了以保持简洁 -->
  </div>
</div>

请注意,上面提供的是代码的翻译,其中CSS和HTML部分已经被翻译为中文。

英文:

In most cases where the size of an element is coming from the outside (parent) and doesn't rely on the intrinsic size, it is often the better idea to use a grid.

Here is how I edit your code to make it work.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
  max-width: 80%;
}

.grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
}

.img-wrapper {
  padding: 10px;
}

.img-wrapper img {
  object-fit: cover;
  width: 100%;
  max-height: 100%;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;grid&quot;&gt;
    &lt;div class=&quot;img-wrapper&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;
        &lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;
      &lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;img-wrapper&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;
        &lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;
      &lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;img-wrapper&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;
        &lt;img src=&quot;https://via.placeholder.com/350&quot;&gt;
      &lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;img-wrapper&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;
        &lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;
      &lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;img-wrapper&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;
        &lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;
      &lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;img-wrapper&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;
        &lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;
      &lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;img-wrapper&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;
        &lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;
      &lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;img-wrapper&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;
        &lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;
      &lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;img-wrapper&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;
        &lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;
      &lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;img-wrapper&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;
        &lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;
      &lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

在图像本身上使用 max-width: 100% 来限制其宽度,同时对这些容器应用 width: 12.5%box-sizing: border-box,即 .flex-img divs:

.container {
  max-width: 80%;
}

.flex-grid {
  display: flex;
  flex-wrap: wrap;
}

.flex-img {
  box-sizing: border-box;
  padding: 10px;
  width: 12.5%;
}

.flex-img img {
  max-width: 100%;
}
<div class="container">
  <div class="flex-grid">
    <div class="flex-img">
      <a href="https://placeholder.com"><img src="https://via.placeholder.com/150"></a>
    </div>
    <!-- 其他 .flex-img 元素 -->
  </div>
</div>
英文:

Use max-width: 100% on the image itself to restrict it to its containers width, and apply width: 12.5% and box-sizing: border-box to those containers, i.e. the .flex-img divs:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
  max-width: 80%
}

.flex-grid {
  display: flex;
  flex-wrap: wrap;
}
.flex-img {
  box-sizing: border-box;
  padding: 10px;
  width: 12.5%;
}
.flex-img img {
  max-width: 100%;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;flex-grid&quot;&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/350&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;flex-img&quot;&gt;
      &lt;a href=&quot;https://placeholder.com&quot;&gt;&lt;img src=&quot;https://via.placeholder.com/150&quot;&gt;&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月23日 23:48:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定