html - 如何仅使用 flexbox 制作响应式图像网格?

标签 html css image flexbox responsive-images

我正在开始我的第一个完整网站元素,我已经学习 HTML 和 CSS 大约一个月了,我刚刚完成了 flexbox 的基础知识学习。我想知道是否有可能创建一个图像网格,其中图像会随着浏览器窗口的大小自动调整,仅使用 flexbox,我现在只知道这些。

我要重新创建的是 this .左边一张大图,右边两张小图叠在一起。当我缩小浏览器时,图像会自动调整大小。这是 website如果你想现场查看。只需向下滚动一点,直到您看到它。

这是我对 jsfiddle 的拙劣尝试.出于某种原因,jsfiddle 没有按照它在我的电脑上显示的方式显示它。我电脑上的图像是并排的,就像我想要的那样,但如果我稍微调整窗口大小,它就会中断到下一行。

对于那些不想处理 jsfiddle 的人,这里是相关的 HTML 和 CSS。我知道这是一团糟。

HTML:

/* Section: Gallery */

#gallery {
  display: flex;
  flex-wrap: wrap;
  background: rgb(252, 246, 237);
  text-align: center;
}

.coffee-img-1 {
  width: 500px;
  padding: 1.5rem;
  margin-right: 5rem;
  margin-top: 2rem;
  max-width: 100%;
  height: auto;
}

.coffee-img-2,
.coffee-img-3 {
  width: 400px;
  padding: 30px;
  max-width: 100%;
  height: auto;
}

.column {
  flex: 50%;
  padding: 1.5rem;
}

#gallery img:hover {
  animation-name: opacity-hover;
  animation-duration: 300ms;
  animation-fill-mode: forwards;
}

@keyframes opacity-hover {
  100% {
    opacity: 0.9;
  }
}
<!-- Gallery -->
<section id="gallery">
  <div>
    <img class="coffee-img-1" src="https://i.imgur.com/58DZwQ2.jpg" alt="Table with coffee and pastries">
  </div>
  <div class="column">
    <img class="coffee-img-2" src="https://i.imgur.com/hsSn85u.jpg" alt="Barista pulling two espresso shots">
    <img class="coffee-img-3" src="https://i.imgur.com/nmAId6V.jpg" alt="Barista brewing coffee with aeropress">
  </div>
</section>

最佳答案

在您的示例中,您忘记将类 column 添加到第一个 div 中。像这样:

<!-- Gallery -->
<section id="gallery">
  <div class="column">
    <img class="coffee-img-1" src="https://i.imgur.com/58DZwQ2.jpg" alt="Table with coffee and pastries">
  </div>
  <div class="column">
    <img class="coffee-img-2" src="https://i.imgur.com/hsSn85u.jpg" alt="Barista pulling two espresso shots">
    <img class="coffee-img-3" src="https://i.imgur.com/nmAId6V.jpg" alt="Barista brewing coffee with aeropress">
  </div>
</section>

要实现此行为,您可以更改 max-widthwidth 属性。 width 属性强制固定宽度,而 max-width(和 min-width)表示“嘿,即使我的图像它大于最大宽度调整它的大小 x"

例子:

https://codepen.io/icaromh/pen/jRopmp

文档:

https://developer.mozilla.org/en-US/docs/Web/CSS/max-width

https://developer.mozilla.org/en-US/docs/Web/CSS/min-width

关于html - 如何仅使用 flexbox 制作响应式图像网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55927543/

相关文章:

html - 如何强制位置为 :absolute to resize the parent div? 的元素

jquery - 灯箱,在 iFrame 外 float 一个关闭按钮

java - 使用图像作为seekBar,带百分比

javascript - retinajs 插件问题

css - 如何在 Internet Explorer 8 中设置 <div> 框的最小高度?

jquery - 使用 jquery 显示/隐藏元素

html - Bootstrap CSS 回退不起作用

ios - 将 uiview 捕获为图像

html - 如何将不同大小的图像放置在 2 列中并对齐 figcaption

javascript - 如何使用 jquery 和 scale (css) 放大/缩小每个 div 中的每个图像?