是否可以在纯CSS中实现类似Flickr的对齐画廊?

pet*_*nar 4 css flexbox

我一直在使用miromannino.github.io作为几页的画廊布局,最近我一直在尝试弄清楚是否有可能在原始CSS中进行此操作,可能使用flexbox。

问题在于水平和垂直图片各不相同,它们应始终填充容器宽度的100%。我最接近的是:

.jgal {
  max-width: 90vw;
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  margin: 0 auto;
  align-items: flex-start;
}

.jgalimg {
  display: block;
  align-self: flex-start;
  max-height: 40vh;
  max-width: 100vw;
  width: auto;
  height: auto;
}
Run Code Online (Sandbox Code Playgroud)

在像这样的布局上:

<div class="jgal">
    <a class="jgallink" href="url-to-img.jpg">
        <img class="jgalimg hor" src="url-to-thumb.jpg" width="640" height="480" />
    </a>
    <a class="jgallink" href="url-to-img-2.jpg">
        <img class="jgalimg ver" src="url-to-thumb-2.jpg" width="480" height="640" />
    </a>
    [ ... ]
</div>
Run Code Online (Sandbox Code Playgroud)

我有上课的大小和方向。我试过使用

align-content: stretch;
align-items: stretch;
Run Code Online (Sandbox Code Playgroud)

但是将图片尺寸与标签对齐会变得很棘手。

那么,有什么想法吗?:)

Kar*_*lis 5

尽管这个问题已有一年多的历史了,但是我最近还是在尝试解决同样的问题,并且提出了以下完全响应式的仅Flickr风格的,类似CSS的合理画廊解决方案:

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

.jg > a, .jg::after {
  --ratio: calc(var(--w) / var(--h));
  --row-height: 9rem;
  flex-basis: calc(var(--ratio) * var(--row-height));
}

.jg > a {
  margin: 0.25rem;
  flex-grow: calc(var(--ratio) * 100);
}

.jg::after {
  --w: 2;
  --h: 1;
  content: '';
  flex-grow: 1000000;
}

.jg > a > img {
  display: block;
  width: 100%;  
}
Run Code Online (Sandbox Code Playgroud)
<div class="jg">

  <a href="#" style="--w: 200; --h: 300">
    <img src="http://via.placeholder.com/200x300">
  </a>
  <a href="#" style="--w: 300; --h: 200">
    <img src="http://via.placeholder.com/300x200">
  </a>
  <a href="#" style="--w: 500; --h: 400">
    <img src="http://via.placeholder.com/500x400">
  </a>
  <a href="#" style="--w: 300; --h: 300">
    <img src="http://via.placeholder.com/300x300">
  </a>
  <a href="#" style="--w: 300; --h: 400">
    <img src="http://via.placeholder.com/300x400">
  </a>
  <a href="#" style="--w: 400; --h: 300">
    <img src="http://via.placeholder.com/400x300">
  </a>
  <a href="#" style="--w: 200; --h: 300">
    <img src="http://via.placeholder.com/200x300">
  </a>
  <a href="#" style="--w: 400; --h: 300">
    <img src="http://via.placeholder.com/400x300">
  </a>
  <a href="#" style="--w: 300; --h: 500">
    <img src="http://via.placeholder.com/300x500">
  </a>

</div>
Run Code Online (Sandbox Code Playgroud)


小智 0

我不确定您的目标是什么max-width: 90vw,但您可以使用固定大小的缩略图来实现这一目标。下面的 CSS 将缩放图像以填充容器,并以垂直和水平居中方式裁剪图像。

.jgal {
  /* set font-size to 0 to avoid the whitespace between HTML elements causing spaces between the photos */
  font-size: 0;
}
a.jgallink {
  /* set desired image size */
  width: 320px;
  height: 240px;

  /* set space between photos */
  margin-right: 1px;

  position: relative;
  overflow: hidden;
  display: inline-block;
}

img.jgalimg {
  position: absolute;
  left: 50%;
  top: 50%;
  height: 100%;
  width: auto;
  -webkit-transform: translate(-50%,-50%);
      -ms-transform: translate(-50%,-50%);
          transform: translate(-50%,-50%);
}

img.jgalimg.ver {
  width: 100%;
  height: auto;
}
Run Code Online (Sandbox Code Playgroud)

在codepen中打开

这是改编自这篇博客文章,该文章从 Wordpress 媒体库获取了该技术。