[英]How can I make a responsive image grid using only flexbox?
I'm starting my first full website project, I've been learning HTML and CSS for about a month now and I just got done learning the basics of flexbox. 我正在开始我的第一个完整的网站项目,已经学习HTML和CSS已有大约一个月了,而我刚学完flexbox的基础知识。 I was wondering if it's possible to create an image grid where the images resize themselves automatically as the browser window is resized using only flexbox, as that's all I know for now.
我想知道是否有可能创建一个图像网格,当仅使用flexbox调整浏览器窗口的大小时,图像会自动调整自身的大小,这就是我现在所知道的。
What I'm trying to recreate is this . 我想重新创建的是这个 。 One large image on the left, with two smaller images stacked on top of each other to the right.
左侧是一幅大图像,而右侧则是彼此堆叠的两个较小图像。 When I shrink the browser, the images stay in place automatically resize themselves.
当我缩小浏览器时,图像会保留在原位,并自动调整其大小。 Here's the website if you want to check it out live.
如果您想实时查看该网站 ,请访问这里。 Just scroll down a bit till you see it.
向下滚动直到看到它。
Here is my poor attempt on jsfiddle . 这是我对jsfiddle的糟糕尝试。 For some reason, jsfiddle is not displaying it they way it displays on my computer.
由于某种原因,jsfiddle无法按照它们在我的计算机上显示的方式进行显示。 The images on my computer are side by side, like how I sort of it want it, but it breaks onto the next line if I resize the window a tiny bit.
我计算机上的图像是并排的,就像我想要的那样,但是如果我调整窗口的大小,它会跳到下一行。
For those not wanting to deal with jsfiddle, here is the relevant HTML and CSS. 对于那些不想使用jsfiddle的人,这里是相关的HTML和CSS。 I know it's a mess.
我知道那是一团糟。
HTML: 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>
In your example, you forgot to add the class column
into the first div. 在您的示例中,您忘记将class
column
添加到第一个div中。 Like this: 像这样:
<!-- 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>
To achieve this behavior you can change the width
property for max-width
. 要实现此行为,您可以更改
max-width
的width
属性。 The width
property forces the width to be fixed, while the max-width
(and min-width
) says "hey, even if my image it's bigger than that resizes it for the max width x
" width
属性将宽度固定为固定值,而max-width
(和min-width
)表示“嘿,即使我的图片大于它的尺寸,也将其调整为最大宽度x
”
Example: 例:
https://codepen.io/icaromh/pen/jRopmp https://codepen.io/icaromh/pen/jRopmp
Docs: 文件:
https://developer.mozilla.org/en-US/docs/Web/CSS/max-width https://developer.mozilla.org/zh-CN/docs/Web/CSS/max-width
https://developer.mozilla.org/en-US/docs/Web/CSS/min-width https://developer.mozilla.org/zh-CN/docs/Web/CSS/min-width
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.