<tfoot id='YyhoK'></tfoot>

    1. <i id='YyhoK'><tr id='YyhoK'><dt id='YyhoK'><q id='YyhoK'><span id='YyhoK'><b id='YyhoK'><form id='YyhoK'><ins id='YyhoK'></ins><ul id='YyhoK'></ul><sub id='YyhoK'></sub></form><legend id='YyhoK'></legend><bdo id='YyhoK'><pre id='YyhoK'><center id='YyhoK'></center></pre></bdo></b><th id='YyhoK'></th></span></q></dt></tr></i><div id='YyhoK'><tfoot id='YyhoK'></tfoot><dl id='YyhoK'><fieldset id='YyhoK'></fieldset></dl></div>
    2. <small id='YyhoK'></small><noframes id='YyhoK'>

        <bdo id='YyhoK'></bdo><ul id='YyhoK'></ul>

      <legend id='YyhoK'><style id='YyhoK'><dir id='YyhoK'><q id='YyhoK'></q></dir></style></legend>
    3. 使网格列跨越整行

      时间:2023-08-02
        <tbody id='50H4W'></tbody>

      <tfoot id='50H4W'></tfoot>

          <bdo id='50H4W'></bdo><ul id='50H4W'></ul>
            1. <small id='50H4W'></small><noframes id='50H4W'>

            2. <i id='50H4W'><tr id='50H4W'><dt id='50H4W'><q id='50H4W'><span id='50H4W'><b id='50H4W'><form id='50H4W'><ins id='50H4W'></ins><ul id='50H4W'></ul><sub id='50H4W'></sub></form><legend id='50H4W'></legend><bdo id='50H4W'><pre id='50H4W'><center id='50H4W'></center></pre></bdo></b><th id='50H4W'></th></span></q></dt></tr></i><div id='50H4W'><tfoot id='50H4W'></tfoot><dl id='50H4W'><fieldset id='50H4W'></fieldset></dl></div>
              • <legend id='50H4W'><style id='50H4W'><dir id='50H4W'><q id='50H4W'></q></dir></style></legend>

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

                问题描述

                假设我们有 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>

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

              • <i id='MnOYo'><tr id='MnOYo'><dt id='MnOYo'><q id='MnOYo'><span id='MnOYo'><b id='MnOYo'><form id='MnOYo'><ins id='MnOYo'></ins><ul id='MnOYo'></ul><sub id='MnOYo'></sub></form><legend id='MnOYo'></legend><bdo id='MnOYo'><pre id='MnOYo'><center id='MnOYo'></center></pre></bdo></b><th id='MnOYo'></th></span></q></dt></tr></i><div id='MnOYo'><tfoot id='MnOYo'></tfoot><dl id='MnOYo'><fieldset id='MnOYo'></fieldset></dl></div>
                    <bdo id='MnOYo'></bdo><ul id='MnOYo'></ul>
                      <tfoot id='MnOYo'></tfoot>

                          <tbody id='MnOYo'></tbody>
                      1. <legend id='MnOYo'><style id='MnOYo'><dir id='MnOYo'><q id='MnOYo'></q></dir></style></legend>

                      2. <small id='MnOYo'></small><noframes id='MnOYo'>