简体   繁体   English

使用 css 使画廊响应

[英]Making gallery responsive with css

I am trying to build simple gallery.我正在尝试建立简单的画廊。 What I would like to achieve is this:我想要实现的是:

在此处输入图像描述

However, what I am actually achieving is this:但是,我实际实现的是: 在此处输入图像描述

There is basically too much space between images and I can't find a way how to solve it.图像之间基本上有太多空间,我找不到解决方法。 I understand that is because of justify-content: space-between;我知道这是因为justify-content: space-between; but perhaps there's another option that will put less space between the images?但也许还有另一种选择可以减少图像之间的空间?

Html Html

<div class="photoContainer>
   <div class="ant-image">
    ...
   </div>
</div>

Css Css

.photosContainer {
    width: 80%;
    height: 100%;
    overflow: auto;
    overflow-x: hidden;
    display: flex;
    align-content: space-between;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    padding: 10px;
}

.ant-image {
    height: fit-content;
    flex-shrink: 0;
    position: relative;
    display: inline-block;
    width: 200px;

}

With the space-between rule you cannot have the control of the space between the images.使用 space-between 规则,您无法控制图像之间的空间。 My suggestion is to:我的建议是:

  • make the image gallery container smaller because you have a small number of photos使图片库容器更小,因为您的照片数量很少
  • to have more control over the images you can use also for the single image a % width as you have done for the container.为了更好地控制图像,您也可以对单个图像使用 % 宽度,就像对容器所做的那样。

hint: use property object-fit for the single images提示:对单个图像使用属性 object-fit

you can use grid display instead of flex and solve your problem:您可以使用网格显示而不是 flex 并解决您的问题:

.photosContainer{
       display:grid;
       grid-template-columns:repeat(4 , 1fr);
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM