html - flex 盒包装不同尺寸的元素

标签 html css flexbox

我可以使用具有以下文档结构的 Flexbox 实现此布局吗?

Layout

我想要大的<img>左边是两个较小的图像,右边是环绕的图像。

这就是我所做的,display: flexgallery-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 - flex 盒包装不同尺寸的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54892908/

相关文章:

html - 具有可变间距的基于 UL 的液体水平菜单

html - HTML 标签和元素有什么区别?

javascript - 如何在编号列表中引用特定元素编号?

javascript - 从上到下,从左到右更改图像加载

javascript - 如何使 jQuery UI 选项卡在激活选项卡时执行 'display: flex' 而不是 'display: block'?

css - 垂直居中对齐 Bootstrap 中的列

c# - 中继器 - 在 "content"中带有滚动条的固定页眉和固定脚

javascript - jQuery 检查复选框不起作用

javascript - XFBML Facebook Like 按钮 - 自定义添加评论文本输入

html - 在嵌套的 flexbox 容器中收缩包含图像的 flexbox