是否有可能在纯CSS中实现类似Flickr的合理画廊?

问题描述 投票:4回答:3

我一直在使用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;
}

在布局上:

<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>

我有尺寸和方向作为班级。我试过用了

align-content: stretch;
align-items: stretch;

但是然后将图像大小与标签对齐变得棘手。

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

css css3 flexbox
3个回答
5
投票

虽然这个问题已经有一年多的时间了,但是我最近试图解决同样的问题,并且我提出了以下完全响应的CSS-Flickr-like protied gallery解决方案:

.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%;  
}
<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>

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;
}

open in codepen

这是改编自this blog post,它从Wordpress媒体库获得了技术。


0
投票

这是我通过flexbox实现的。

.jgal{
  width: 100%;
  display: flex;
}

.jgal a{
  border-right: 3px solid #fff;
}

.jgal img{
  width:100%;
  height: 100%;
  object-fit: cover;
}

https://jsfiddle.net/1wrn8k4v/2/

© www.soinside.com 2019 - 2024. All rights reserved.