使网格列跨越整行

2023-08-02前端开发问题
6

本文介绍了使网格列跨越整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

假设我们有 2 个 CSS 网格容器,其中包含动态的基于宽度的列数.

Imagine we have 2 CSS Grid containers with dynamic columns count based on width.

display: grid;
grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));

网格完美运行,但是如果我们需要另一个网格以使第一列与另一个网格中的相同,使用上面显示的代码,但它是跨越更多单元格的另一列 - 取决于多少个单元格在当前行中.

The grid works perfectly, but what if we need to have another grid to have the 1st column to be same as in another grid with the code shown above, but it's another column to span through more cells - depending on how many cells are in the current row.

为了更好地理解问题,有图片:

To better understand issue, there are images:

在更窄的包装上:

我们需要应用类似 grid-column: span ALL (如果存在类似的东西),这意味着 ALL = 直到当前行的末尾.

We would need to apply something like grid-column: span ALL (if something like that exists), with meaning that ALL = till the end of current row.

真正重要的是第一"列应始终与1"列对齐.

What is really important is that "First" column should always align with "1" column.

运行示例的代码在这里:

Code to run example is here:

.grid div {
  /* Not important fancy styles */
  height: 40px;
  text-align: center;
  padding-top: 20px;
}

.grid {
  width: 350px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  background-color: silver;
}

.grid-second {
  background-color: red;
}

.grid-another {
  background-color: purple;
  border: 1px solid gray;
}

<div class="grid">
  <div class="grid-first">
    First
  </div>
  <div class="grid-second">
    Second (Want till end)
  </div>
</div>
<!-- Another same grid -->
<div class="grid">
  <div class="grid-another">
    1
  </div>
  <div class="grid-another">
    2
  </div>
  <div class="grid-another">
    3
  </div>
  <div class="grid-another">
    4
  </div>
</div>

PS.请不要使用媒体查询发布解决方案.我对任何(甚至是小技巧的解决方案)感兴趣,它可以在不使用媒体查询的情况下工作.

PS. please do not post solutions using media query. I am interested in any (even little hacky solution), which will work without usage of media queries.

推荐答案

下面是 CSS Grid 规范中的两个有趣的部分:

Here are two interesting sections in the CSS Grid specification:

7.1.显式网格

网格放置属性中的数字索引从边缘开始计数的显式网格.正索引从一开始就计数,而负索引则从末尾开始计数.

Numeric indexes in the grid-placement properties count from the edges of the explicit grid. Positive indexes count from the start side, while negative indexes count from the end side.

这里也...

8.3.基于行的放置:grid-row-startgrid-column-startgrid-row-endgrid-column-end 属性

如果给定一个负整数,它会倒数,开始从显式网格的末端边缘.

If a negative integer is given, it instead counts in reverse, starting from the end edge of the explicit grid.

换句话说,在处理显式网格时,这意味着由这些属性定义的网格:

In other words, when dealing with an explicit grid, which means a grid defined by these properties:

  • 网格模板行
  • 网格模板列
  • 网格模板区域
  • grid(这是上面三个属性的简写,等等)
  • grid-template-rows
  • grid-template-columns
  • grid-template-areas
  • grid (which is the shorthand for the three properties above, among others)

...您可以通过设置以下规则使网格区域跨越所有列:

... you can make a grid area span all columns by setting this rule:

grid-column: 1 / -1;

这告诉网格区域从第一列线跨越到最后一列线,我相信这符合您的既定目标:

That tells the grid area to span from the first column line to the last column line, which I believe meets your stated objective:

我们需要应用类似 grid-column: span ALL 的东西(如果存在类似的东西),这意味着 ALL = 直到当前行的末尾."em>

"We would need to apply something like grid-column: span ALL (if something like that exists), with meaning that ALL = till the end of current row."

jsFiddle 演示

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  background-color: silver;
}

.grid-second {
  grid-column: 2 / -1;
  background-color: red;
}


/* Not important fancy styles */

.grid div {
  height: 40px;
  text-align: center;
  padding-top: 20px;
}

.grid-another {
  background-color: purple;
  border: 1px solid gray;
}

<div class="grid">
  <div class="grid-first">First</div>
  <div class="grid-second">Second (Want till end)</div>
</div>
<!-- Another same grid -->
<div class="grid">
  <div class="grid-another">1</div>
  <div class="grid-another">2</div>
  <div class="grid-another">3</div>
  <div class="grid-another">4</div>
  <div class="grid-another">1</div>
  <div class="grid-another">2</div>
  <div class="grid-another">3</div>
  <div class="grid-another">4</div>
  <div class="grid-another">1</div>
  <div class="grid-another">2</div>
  <div class="grid-another">3</div>
  <div class="grid-another">4</div>
</div>

这篇关于使网格列跨越整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui tree树组件怎么自定义添加图标
经常用到layui的朋友都知道,layui tree默认是不能自定义图标的,那么我们要自定义的话要怎么操作呢? 首先用编辑器软件(修改时候用编辑器记得编码),打开layui.js。搜索: i class="layui-icon layui-icon-file" 改为如下代码: i class="'+ (i.icon || "l...
2024-10-26 前端开发问题
493

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13

getFullYear 在一年的第一天返回前一年
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)...
2024-04-20 前端开发问题
6

如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?
How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社...
2024-04-20 前端开发问题
6