gpt4 book ai didi

html - 用不同大小的元素包装 flexbox

转载 作者:行者123 更新时间:2023-11-28 14:23:58 26 4
gpt4 key购买 nike

我可以使用具有以下文档结构的 flexbox 实现这种布局吗?

Layout

我要大的<img>在左侧,右侧有两个较小的图像并环绕。

这就是我用 display: flex 所做的在 gallery-containerflex-wrap .

.container {
height: 100%;
}

.container .gallery-container {
background-color: #f6f6f6;
display: flex;
flex-wrap: wrap;
width: 300px;
align-items: flex-start;
}

.container .gallery-container .gallery-big-image {
display: block;
width: 200px;
height: 200px;
background: lavender;
}

.container .gallery-container .gallery-small-img {
display: block;
width: 100px;
height: 100px;
background-color: purple;
}
<div class="container">
<div class="gallery-container">
<div class="gallery-big-image">big</div>
<div class="gallery-small-img">small</div>
<div class="gallery-small-img">small</div>
<div class="gallery-small-img">small</div>
<div class="gallery-small-img">small</div>
<div class="gallery-small-img">small</div>
</div>
</div>

( codepen )

最佳答案

使用 flexbox 的布局笨重且效率低下,原因在这里解释:CSS-only masonry layout

但是,使用 CSS Grid 布局相对简单易行。

HTML 没有变化。

.gallery-container {
display: grid;
grid-template-columns: repeat(auto-fill, 100px);
grid-auto-rows: 100px;
width: 300px;
background-color: #f6f6f6;
}

.gallery-big-image {
grid-column: span 2;
grid-row: span 2;
background: lavender;
}

.gallery-small-img {
background-color: purple;
color: white;
}
<div class="container">
<div class="gallery-container">
<div class="gallery-big-image">big</div>
<div class="gallery-small-img">small 1</div>
<div class="gallery-small-img">small 2</div>
<div class="gallery-small-img">small 3</div>
<div class="gallery-small-img">small 4</div>
<div class="gallery-small-img">small 5</div>
<div class="gallery-small-img">small 6 (continues wrapping)</div>
<div class="gallery-small-img">small 7 (continues wrapping)</div>
</div>
</div>

关于html - 用不同大小的元素包装 flexbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54892908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com