AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79563597
Accepted
vespino
vespino
Asked: 2025-04-09 14:17:16 +0800 CST2025-04-09 14:17:16 +0800 CST 2025-04-09 14:17:16 +0800 CST

图像网格上的最后一行

  • 772

我正在使用以下代码来创建相册(在 WordPress 中,但对于本示例来说,这无关紧要):

结果是这样的:

在此处输入图片描述

我希望最后一行的图片始终占据该行。所以,当只有一张图片时,将其宽度设置为 100%,高度设置为更大;当有两张图片时,将其宽度设置为 50%,高度设置为更大。

每个页面的图片数量都不一样。有什么建议吗?

.gallery {
  display: grid;
  grid-template-columns: auto auto auto;
  gap: 8px;
}

.gallery>.img {
  width: 100% !important;
  position: relative;
  padding-bottom: 75%;
}

.gallery img {
  object-fit: cover;
  object-position: 50%;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  height: 100%;
  width: 100%;
}
<div class="gallery">
  <div class="img">
    <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
  </div>
  <div class="img">
    <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
  </div>
  <div class="img">
    <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
  </div>
  <div class="img">
    <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
  </div>
  <div class="img">
    <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
  </div>
  <div class="img">
    <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
  </div>
  <div class="img">
    <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
  </div>
</div>

html
  • 3 3 个回答
  • 58 Views

3 个回答

  • Voted
  1. Best Answer
    G-Cyrillus
    2025-04-09T17:42:05+08:002025-04-09T17:42:05+08:00

    从您的代码和 Temani Afif 的解释来看,您必须从六列网格布局开始,因此元素可以跨越三分之一行、一半行或整整一行。

    下面的代码片段使用了您之前使用代码提出的相同方法。CSS 适用于.gallery带有 X 的容器的未知数量的子元素div.img。

    我也切换了一些 CSS 规则:grid而不是absolute,aspect-ratio而是padding-bottom。您可以保留自己的尺寸和位置,这仅供参考。

    下面是一个从 6 个元素减至 1 个元素的示例代码片段。网格中最后一个元素的位置通过https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes#tree-structural_pseudo-classes和 https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors/Selectors_and_combinators#subsequent-sibling_combinator进行过滤。

    .gallery {
      display: grid;
      grid-template-columns: repeat(6, 1fr); /* mind this one */
      gap: 8px;
    }
    .gallery > .img {
      display: grid;
      aspect-ratio: 3/2;
      grid-column: span 2;/* a third of 6 columns */
    }
    .gallery img {
      object-fit: cover;
      height: 100%;
      width: 100%;
    }
    
    
    /********************************************/
    /* selectors to filter position in the flow */
    /********************************************/
    .gallery > .img:last-child:nth-child(3n + 1) /* standing alone on a row */
    {
      grid-column: span 6; /* 100 % of 6 columns */
    }
    .gallery > .img:nth-last-child(2):first-child,/* first of a serie of only 2 first row */
    .gallery > .img:nth-last-child(2):first-child ~ .img,/* last one of a serie of only 2 first row */
    .gallery > .img:nth-last-child(3):nth-child(3n - 3) ~ .img /* only 2 on the last -row */
    {
      grid-column: span 3; /* 50% of 6 columns */
    }
    /********************************************/
    /******** end filtering position ************/
    /********************************************/
    
    
    /* below CSS is only for the demo purpose, there's no need to mind it */
    /* demo */
    body {
      max-width: 400px;
      margin: auto;
      display: grid;
      gap: 1.6em;
      padding:1.2em;
    }
    .gallery {
      border: solid red 1px;
      counter-reset : gal;
    }
    .img {
      counter-increment:gal
    }
    .gallery::after {
      content:counter(gal) ' element(s)';
    position:absolute;
      translate:-1ch -1.2em;
      color:green;
      font-weight:bolder;
    }
    /* EO demo */
    <div class="gallery">
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
    </div>
    <div class="gallery">
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
    </div>
    <div class="gallery">
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
    </div>
    <div class="gallery">
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
    </div>
    <div class="gallery">
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
    </div>
    <div class="gallery">
      <div class="img">
        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
      </div>
    </div>

    • 2
  2. Temani Afif
    2025-04-09T15:40:50+08:002025-04-09T15:40:50+08:00

    您可以依靠:nth-child特定图像来定位

    .container {
      display: grid;
      grid-template-columns: repeat(6,1fr); /* 6 columns and not 3 */
      gap: 5px;
      border: 2px solid red;
      margin: 10px;
    }
    .container img {
      width: 100%;
      grid-column: span 2; /* span 2 cells to get a 3-column layout */
    }
    
    /* if one image is alone in the last row */
    .container img:nth-child(3n + 1):nth-last-child(1) {
      grid-column: span 6;
    }
    /* if two images are in the last row */
    .container img:nth-child(3n + 2):nth-last-child(1),
    .container img:nth-child(3n + 1):nth-last-child(2){
      grid-column: span 3;
    }
    <div class="container">
     <img src="https://picsum.photos/id/1/400/200">
     <img src="https://picsum.photos/id/1/400/200">
     <img src="https://picsum.photos/id/1/400/200">
     <img src="https://picsum.photos/id/1/400/200">
    </div>
    <div class="container">
     <img src="https://picsum.photos/id/1/400/200">
     <img src="https://picsum.photos/id/1/400/200">
     <img src="https://picsum.photos/id/1/400/200">
     <img src="https://picsum.photos/id/1/400/200">
     <img src="https://picsum.photos/id/1/400/200">
    </div>
    <div class="container">
     <img src="https://picsum.photos/id/1/400/200">
     <img src="https://picsum.photos/id/1/400/200">
     <img src="https://picsum.photos/id/1/400/200">
     <img src="https://picsum.photos/id/1/400/200">
     <img src="https://picsum.photos/id/1/400/200">
     <img src="https://picsum.photos/id/1/400/200">
    </div>

    • 0
  3. kartik kukadiya
    2025-04-09T14:30:13+08:002025-04-09T14:30:13+08:00
    <?php
    $totalImages = 7; // change this as needed
    $columns = 3; // number of columns in the grid
    $fullRows = floor($totalImages / $columns);
    $remaining = $totalImages % $columns;
    ?>
    <html>
        <head>
            <style>
                .gallery {
                    display: grid;
                    grid-template-columns: repeat(3, 1fr);
                    gap: 8px;
                }
                .gallery > .img {
                    width: 100%;
                    position: relative;
                    padding-bottom: 75%;
                }
                .gallery img {
                    object-fit: cover;
                    object-position: 50%;
                    position: absolute;
                    top: 0;
                    right: 0;
                    bottom: 0;
                    left: 0;
                    height: 100%;
                    width: 100%;
                }
    
                /* Special handling for last row */
                .last-row-1 {
                    grid-column: span 3;
                    padding-bottom: 50%; /* taller image */
                }
                .last-row-2 {
                    grid-column: span 2;
                    padding-bottom: 60%;
                }
                .last-row-2 + .last-row-2 {
                    grid-column: span 1;
                    padding-bottom: 60%;
                }
            </style>
        </head>
        <body>
            <div class="gallery">
                <?php for($i=0; $i<$totalImages; $i++): ?>
                    <?php
                        $class = '';
                        if ($i >= $fullRows * $columns) {
                            // This image is in the last (incomplete) row
                            if ($remaining == 1) {
                                $class = 'last-row-1';
                            } elseif ($remaining == 2) {
                                $class = 'last-row-2';
                            }
                        }
                    ?>
                    <div class="img <?= $class ?>">
                        <img src="https://fastly.picsum.photos/id/266/200/300.jpg?hmac=nAZYC6vsnq4fuOzfge00Ylvi9ALRMbMA8wxBxIPTjs0">
                    </div>
                <?php endfor; ?>
            </div>
        </body>
    </html>
    

    工作原理

    网格有 3 列。

    PHP 逻辑计算出最后一行有多少个图像($remaining)。

    基于此,我们为最后一行图像分配一个自定义类。

    在 CSS 中,我们使用 grid-column: span X 使最后一行图像跨越整行(对于 1 个图像)或一半(对于 2 个图像)。

    我们还调整了 padding-bottom,使它们更高,以达到视觉平衡。

    • -1

相关问题

  • 使用代码点渲染轮廓图标,而不是从 Material Design Google Fonts 中填充

  • 如何使用CSS将选项卡制作为箭头形状

  • 在 html 和 css 中绘制表格内的倾斜线

  • HTML 表格,一行中有 2 行单元格

  • 如何创建 mat-grid-list 角度材质

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve