html - 在 colgroup 上设置行跨度?

标签 html css html-table semantics modular

简单(我希望),HTML 问题。

假设我有一个横跨 3 列的列组。但它也跨越 9 行。但实际上,我希望有 3 级列(所以基本上是 3 列,分为 9 行)。真正的唯一目标是:

a) 避免嵌入表格(出于各种原因) b) 保持各部分模块化。 c) 允许语义模块化区域的样式。

所以最后,我会得到类似这样的视觉效果:

   | col 1  | col 2 | col 3 |
   | row 1  | row 1 | row 1 |
   | row 2  | row 2 | row 2 |
   | row 3  | row 3 | row 3 |

   | col 4  | col 5 | col 6 |
   | row 4  | row 4 | row 4 |
   | row 5  | row 5 | row 5 |
   | row 6  | row 6 | row 6 |

   | col 7  | col 2 | col 3 |
   | row 7  | row 7 | row 7 |
   | row 8  | row 8 | row 8 |
   | row 9  | row 9 | row 9 |

我能够让列组工作以将 3 列保持在一起,但是添加“rowspan”的尝试失败了。尝试将行组包装在 tr 标签中一点也不好。据我所知,没有真正的“行组”标签。

更新:

在获得反馈后,我意识到我应该就我的想法提供更多细节。

我将使用术语四元组、超列、超行来指代数据组。举个例子:

               Quad 1       Quad 2
super-row1 | a | b | c || d | e | f |
super-row2 | 1 | 2 | 3 || 4 | 5 | 6 |
super-row3 | A | B | C || D | E | F |

               Quad 3       Quad 4
super-row4 | g | h | i || j | k | l |
super-row5 | 7 | 8 | 9 || 0 | 1 | 2 |
super-row6 | G | H | I || J | K | L |

为了简单起见,假设我在顶部写了 super-col 1 - 6。

因此,quad 1 中的数据都是相关的,super-row 1 中的数据都是相关的,super-col 1 中的数据都是相关的。因此,使用上述数据,

'a'与'f'、'C'和'G'有直接关系,但'f'、'C'和'G'彼此没有直接关系。

另一种思考方式是数独,其中每个四边形、列和行包含 1-9 的集合,使 81 个数据点中的任何一个与它共享一个行、列或四分之一,但不是任何数据点。

快速更新:

最后一件事,抱歉。重要的是,这些关系在 HTML 中按语义分组,这样,如果有人使用屏幕阅读器或非传统浏览器,他们可以知道它们在任何给定点在表格中的位置,即“你在 Quad 1,Super Col 1, Column 4, Super Row 1, Row 1。数据是‘Ohio’。”

这使得样式设置、跨浏览器兼容性、可访问性以及诸如 AccessKeys、Scope 等的移动变得更加容易。

最佳答案

鉴于<colgroup>是对列进行分组的唯一语义方法,<tbody>是对行进行分组的唯一语义方法,我建议坚持使用一些简单的方法:

<table>
    <colgroup id="colgroup1">
        <col id="colA" />
        <col id="colB" />
    </colgroup>
    <colgroup id="colgroup2">
        <col id="colC" />
        <col id="colD" />
    </colgroup>
    <tbody id="rowgroup1">
        <tr>
            <th>A</th>
            <th>B</th>
            <th>C</th>
            <th>D</th>
        </tr>
        <tr id="row1">
            <td>A1</td>
            <td>B1</td>
            <td>C1</td>
            <td>D1</td>
        </tr>
        <tr id="row2">
            <td>A2</td>
            <td>B2</td>
            <td>C2</td>
            <td>D2</td>
        </tr>
    </tbody>
    <tbody id="rowgroup2">
        <tr>
            <th>A</th>
            <th>B</th>
            <th>C</th>
            <th>D</th>
        </tr>
        <tr id="row3">
            <td>A3</td>
            <td>B3</td>
            <td>C3</td>
            <td>D3</td>
        </tr>
        <tr id="row4">
            <td>A4</td>
            <td>B4</td>
            <td>C4</td>
            <td>D4</td>
        </tr>
    </tbody>
</table>

这将允许您在设置四边形样式的同时仍然保持干净的语义结构。我不确定您在样式方面有多大的灵 active ,但您确实有一些选择:

<style type="text/css">
    table { border-collapse:collapse; font-family:sans-serif; font-size:small; }
    td, th { border:solid 1px #000; padding:2px 10px; text-align:center; }
    th { background-color:#000; color:#fff; font-size:x-small; }
    colgroup { border:solid 3px #000; }
    tbody { border:solid 3px #000; }
</style>

table {
  border-collapse: collapse;
  font-family: sans-serif;
  font-size: small;
}
td,
th {
  border: solid 1px #000;
  padding: 2px 10px;
  text-align: center;
}
th {
  background-color: #000;
  color: #fff;
  font-size: x-small;
}
colgroup {
  border: solid 3px #000;
}
tbody {
  border: solid 3px #000;
}
<table>
  <colgroup id="colgroup1">
    <col id="colA" />
    <col id="colB" />
  </colgroup>
  <colgroup id="colgroup2">
    <col id="colC" />
    <col id="colD" />
  </colgroup>
  <tbody id="rowgroup1">
    <tr>
      <th>A</th>
      <th>B</th>
      <th>C</th>
      <th>D</th>
    </tr>
    <tr id="row1">
      <td>A1</td>
      <td>B1</td>
      <td>C1</td>
      <td>D1</td>
    </tr>
    <tr id="row2">
      <td>A2</td>
      <td>B2</td>
      <td>C2</td>
      <td>D2</td>
    </tr>
  </tbody>
  <tbody id="rowgroup2">
    <tr>
      <th>A</th>
      <th>B</th>
      <th>C</th>
      <th>D</th>
    </tr>
    <tr id="row3">
      <td>A3</td>
      <td>B3</td>
      <td>C3</td>
      <td>D3</td>
    </tr>
    <tr id="row4">
      <td>A4</td>
      <td>B4</td>
      <td>C4</td>
      <td>D4</td>
    </tr>
  </tbody>
</table>

关于html - 在 colgroup 上设置行跨度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1006089/

相关文章:

html - 按钮类分割线

html - HTML 表格格式问题

javascript - 无法添加 particles.js

javascript - Tablesorter 动态设置输出小部件选项

php - 每个结果都出现在表格中而不是行中

javascript - Html/Javascript : Is it possible to set text style. 默认显示 = "none"?

html - 如何在 Bootstrap 中制作较小的容器

javascript - 在附加到 div 之前将 id 添加到动态生成的表中

html - bootstrap-vue表td元素样式

html - 详细信息时隐藏另一个元素[打开]