【问题标题】:How to use flexbox to make my Image gallery如何使用 flexbox 制作我的图片库
【发布时间】:2020-07-19 13:58:08
【问题描述】:

我想知道如何让我的风景图库用风力涡轮机肖像照片旁边的其他图像填充空白空间。

我应用我的css代码的网站是photography.comsma.com

这是我的css


    .gallery{
        padding: 50px;
    }
    .ImageTile{

        padding: 10px;
        width: 50%;
        max-width: 100%;
        max-height: 100%;
        display: inline-block;
    }
    .ImageTile img {

        height: auto;
        width: 100%;
    }
    @media only screen and (max-width:1100px) {
        .gallery{
            padding:5%;
        }
        .ImageTile{
            width: 100%;
            max-width: 100%;
            max-height: 100%;
            display: inline-block;
        }

    }

这是我的html

<div class="gallery">
    <div class="ImageTile">
        <img src="/assets/Photography/Landscapes/1.jpg">
    </div>
    <div class="ImageTile">
        <img src="/assets/Photography/Landscapes/2.jpg">
    </div>
    <div class="ImageTile">
        <img src="/assets/Photography/Landscapes/3.jpg">
    </div>
    <div class="ImageTile">
        <img src="/assets/Photography/Landscapes/4.jpg">
    </div>
    <div class="ImageTile">
        <img src="/assets/Photography/Landscapes/5.jpg">
    </div>
    <div class="ImageTile">
        <img src="/assets/Photography/Landscapes/6.jpg">
    </div>
    
</div>

【问题讨论】:

  • 添加一些演示图片。
  • 图片在我的网站上,位于photography.comsma.com

标签: html css flexbox css-grid


【解决方案1】:

在这种特殊情况下,我会进行 3 处修改:

首先,我会将此规则添加到 ImageTile:

.ImageTile{
          width: 100%;
 }

然后,我会在 html 标签中为高大的图片定义一个子类:

    <div class="ImageTile tall">
      <img src="https://photography.comsma.com/assets/Photography/Landscapes/2.jpg">
    </div>

最后,我会针对更大的屏幕进行@media 查询。一条规则使画廊成为网格,一条规则让高大的图片将自己定位在第 2 列,并使用 3 个普通图像空间作为高度:

@media only screen and (min-width:1100px) {
    .gallery{
        display:grid;
        grid-template: "ImageTile ImageTile";
    }
   .ImageTile.tall {
      grid-row: 1/span 3;
      grid-column: 2;
  }
    

}

我会丢失小屏幕的@media 查询,或者可能只是保留填充规则。

这是我的代码笔,您可以看到它的实际效果:

Codepen for COMSMA PHOTOGRAPHY

【讨论】:

  • 太棒了,我喜欢它,我从没想过要为最小屏幕设置默认值。
【解决方案2】:
.gallery{
   display: flex;
   justify-content: space-between;
   align-items: center;
   flex-flow: row wrap;
   }

【讨论】:

  • 我可以获取您的代码的链接,以便查看您的工作并添加更多建议
  • 这是我的 github 仓库 github.com/comsma/COMSMA-Photography/tree/master/src/app 但我想我想制作一个像这里所示的方形空间画廊一样的画廊 -> amandanilsson.se
  • 好吧,也许我错了,因为它是一个方形空间网站
  • 您好!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的答案添加解释并说明适用的限制和假设。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-13
  • 1970-01-01
相关资源
最近更新 更多