如何使用CSS伪选择器链接两个表格?

huangapple go评论67阅读模式
英文:

How can link two table with css pseudo-selectors?

问题

我正在尝试使用CSS伪选择器使两个不同的表格能够相互监听,我创建了一个数据表格和一个自动编号的表格,它与数据表格不同。我正在尝试实现当用户悬停在数据表格的td上时,整行都会突出显示,并且与之对应的自动编号行也会突出显示,当用户悬停在自动编号行的td上时,数据表格的tr也会突出显示。

我尝试了一些方法,如使用useState,但我希望找到纯CSS的解决方案。如果有人有一些好的见解,请分享。

另外,如果无法通过纯CSS实现这个效果,是否可以使用伪选择器来处理事件,这也将很有帮助。请分享您的想法。

Codepen

.sticky-table tbody tr:hover td,
.auto-number-table tbody tr:hover td {
  background-color: #e0e0e0;
}
英文:

I am trying to make two different table able to listen each other with CSS pseudo-selectors, I have a created a data table and a auto-numbered table which is different table from data table. I am trying to achieve when user hover over td of data table whole row gets highlighted along with corresponding auto-numbered row, same when user hover over td of auto-numbered row, tr of data table get highlighted along with it.

I tried few approaches like using useState but I am aiming for pure CSS solutions. If anyone have some good insights do share them.

Also if this cant be achieved with pure css, can we use event handler with pseudo selector that would be also helpful. Please share your thoughts on it too.

Codepen

.sticky-table tbody tr:hover td,
.auto-number-table tbody tr:hover td {
  background-color: #e0e0e0;
}

答案1

得分: 1

You could using the :has() pseudo class but it would be a lot of CSS and isn't supported in firefox. Marked up example below. The last css rule picks up where any element with a specific data-table attribute is hovered over, it highlights any table row with the same attribute value.

您可以使用:has()伪类,但这将生成大量CSS并且不受Firefox支持。下面是一个示例。最后一个CSS规则会在悬停在具有特定data-table属性的任何元素上时,突出显示具有相同属性值的表格行。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->
table {
  border-collapse: collapse;
  margin-bottom: 1rem;
}

td {
  border: 1px solid lightgray;
  padding-inline: 0.25rem;
}

:has([data-row='1']:hover) [data-row='1'],
:has([data-row='2']:hover) [data-row='2'],
:has([data-row='3']:hover) [data-row='3'] {
  background-color: red;
}

<!-- language: lang-html -->
Data table
<table>
  <tr data-row='1'>
    <td>Row</td>
    <td>1</td>
  </tr>
  <tr data-row='2'>
    <td>Row</td>
    <td>2</td>
  </tr>
  <tr data-row='3'>
    <td>Row</td>
    <td>3</td>
  </tr>
</table>

Autonumber table
<table>
  <tr data-row='1'>
    <td>Auto Row</td>
    <td>1</td>
  </tr>
  <tr data-row='2'>
    <td>Auto Row</td>
    <td>2</td>
  </tr>
  <tr data-row='3'>
    <td>Auto Row</td>
    <td>3</td>
  </tr>
</table>

<!-- end snippet -->

Edited 我注意到在您的示例中,您的自动编号表格与数据表格相邻。为什么不将它们都放在一个表格中,然后使用CSS从第二列中删除样式,以使其看起来像是两个单独的表格呢?然后,您不需要使用JS或复杂的CSS来控制行悬停效果。下面是一个示例:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->
.container {
  max-width: 600px;
  margin: 0 auto;
  display: flex;
}

.sticky-table {
  flex-grow: 1;
  margin-left: 10px;
  border-collapse: collapse;
  table-layout: fixed;
}

.sticky-table td:nth-child(2) {
  background-color: transparent;
}

.sticky-table th:not(:nth-child(2)),
.sticky-table td:not(:nth-child(2)) {
  border: 1px solid #ccc;
  padding: 8px;
  text-align: left;
}

.sticky-table th {
  position: sticky;
  top: 0;
  z-index: 2;
}

.sticky-table th:nth-child(2) {
  width: 10px;
}

.sticky-table th:not(:nth-child(2)) {
   background-color: #f0f0f0;
}

.sticky-table tbody tr:hover td:not(:nth-child(2)) {
  background-color: #e0e0f0;
}

.sticky-table td:first-child {
  text-align: center;
  width: 30px;
}

<!-- language: lang-html -->
<div class="container">
  <table class="sticky-table">
    <thead>
      <tr>
        <th></th>
        <th></th>
        <th>Header 1</th>
        <th>Header 2</th>
        <th>Header 3</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td></td>
        <td>Cell 1</td>
        <td>Cell 2</td>
        <td>Cell 3</td>
      </tr>
      <tr>
        <td>2</td>
        <td></td>
        <td>Cell 4</td>
        <td>Cell 5</td>
        <td>Cell 6</td>
      </tr>
    </tbody>
  </table>
</div>

<!-- end snippet -->
英文:

You could using the :has() pseudo class but it would be a lot of CSS and isn't supported in firefox. Marked up example below. The last css rule picks up where any element with a specific data-table attribute is hovered over, it highlights any table row with the same attribute value.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

table {
  border-collapse:collapse;
  margin-bottom: 1rem;
}

td {
  border: 1px solid lightgray;
  padding-inline: 0.25rem;
}

:has([data-row=&#39;1&#39;]:hover) [data-row=&#39;1&#39;],
:has([data-row=&#39;2&#39;]:hover) [data-row=&#39;2&#39;],
:has([data-row=&#39;3&#39;]:hover) [data-row=&#39;3&#39;] {
  background-color:red;
}

<!-- language: lang-html -->

Data table
&lt;table&gt;
  &lt;tr data-row=&#39;1&#39;&gt;
    &lt;td&gt;Row&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr data-row=&#39;2&#39;&gt;
    &lt;td&gt;Row&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr data-row=&#39;3&#39;&gt;
    &lt;td&gt;Row&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

Autonumber table
&lt;table&gt;
  &lt;tr data-row=&#39;1&#39;&gt;
    &lt;td&gt;Auto Row&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr data-row=&#39;2&#39;&gt;
    &lt;td&gt;Auto Row&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr data-row=&#39;3&#39;&gt;
    &lt;td&gt;Auto Row&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

Edited
I've noticed on your example that your autonumber table sits next to your data table. Why not just have them both as one table then use css to remove the styling from the 2nd column to make it look like they're two separate tables? Then you don't need JS or complex css to control the row hover effect. Example below:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
  max-width: 600px;
  margin: 0 auto;
  display: flex;
}

.sticky-table {
  flex-grow: 1;
  margin-left: 10px;
  border-collapse: collapse;
  table-layout: fixed;
}

.sticky-table td:nth-child(2) {
  background-color: transparent;
}

.sticky-table th:not(:nth-child(2)),
.sticky-table td:not(:nth-child(2)) {
  border: 1px solid #ccc;
  padding: 8px;
  text-align: left;
}

.sticky-table th {
  position: sticky;
  top: 0;
  z-index: 2;
}

.sticky-table th:nth-child(2) {
  width: 10px;
}

.sticky-table th:not(:nth-child(2)) {
   background-color: #f0f0f0;
}

.sticky-table tbody tr:hover td:not(:nth-child(2)) {
  background-color: #e0e0e0;
}

.sticky-table td:first-child {
  text-align: center;
  width: 30px;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;table class=&quot;sticky-table&quot;&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;&lt;/th&gt;
        &lt;th&gt;&lt;/th&gt;
        &lt;th&gt;Header 1&lt;/th&gt;
        &lt;th&gt;Header 2&lt;/th&gt;
        &lt;th&gt;Header 3&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;1&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;Cell 1&lt;/td&gt;
        &lt;td&gt;Cell 2&lt;/td&gt;
        &lt;td&gt;Cell 3&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;2&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;Cell 4&lt;/td&gt;
        &lt;td&gt;Cell 5&lt;/td&gt;
        &lt;td&gt;Cell 6&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月15日 17:10:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定