【问题标题】:Hover on column and rows, with multiple background colors悬停在列和行上,具有多种背景颜色
【发布时间】:2018-04-03 14:06:12
【问题描述】:

我知道这可以用行来完成,但我无法让它也适用于列。我需要能够对我的列进行颜色编码,但在所有列中保持相同的悬停颜色。下面是我到目前为止的代码,我需要将第一列的背景颜色为灰色,其他所有内容都保持白色。同时在所有行和列中保持相同的悬停颜色。希望这是有道理的!

Codepen

.grey-column {
  background-color: grey;
}

.data-table {
  overflow: hidden;
  border: solid black;
  border-collapse: collapse;
  border-width: 1px 2px 2px 1px;
}

.data-cell {
  padding: 10px;
  font-size: 14px;
  position: relative;
  border: solid black;
  border-width: 1px 1px 1px 1px;
}

.data-row:hover {
  padding: 10px;
  background-color: #ffa;
}

.data-cell::before {
  content: "";
  position: absolute;
  top: 0;
  left: -5000px;
  width: 10000px;
  height: 100%;
  z-index: -10;
}

.data-cell:hover::after {
  content: "";
  position: absolute;
  background-color: #ffa;
  left: 0;
  top: -5000px;
  height: 10000px;
  width: 100%;
  z-index: -5;
}
<table class="data-table">
  <tbody>
    <tr class="data-row">
      <td class="grey-column">I should change to hover color</td>
      <td class="data-cell">Cell 2</td>
      <td class="data-cell">Cell 3</td>
      <td class="data-cell">Cell 4</td>
    </tr>
    <tr class="data-row">
      <td class="grey-column">I should change to hover color</td>
      <td class="data-cell">Cell 2</td>
      <td class="data-cell">Cell 3</td>
      <td class="data-cell">Cell 4</td>
    </tr>
    <tr class="data-row">
      <td class="grey-column">I should change to hover color</td>
      <td class="data-cell">Cell 2</td>
      <td class="data-cell">Cell 3</td>
      <td class="data-cell">Cell 4</td>
    </tr>
  </tbody>
</table>

【问题讨论】:

  • 你想要Color me Greygrey 是彩色的吗?
  • 让我换种说法,我希望灰色列在悬停时从灰色变为悬停颜色。

标签: html css styles stylesheet css-tables


【解决方案1】:

更新

我认为这对你有用。

我删除了background .grey-column 并添加了这个样式。

.grey-column:after {
  background-color: grey;
  position:absolute;
  left:0;
  right:0;
  z-index:-20;
  top:0;
  bottom:0;
  content:'';
 }
 .grey-column{
  position:relative;
 }

为您提供工作示例。

.grey-column:after {
  background-color: grey;
  position:absolute;
  left:0;
  right:0;
  z-index:-20;
  top:0;
  bottom:0;
  content:'';
}
.grey-column{
 position:relative;
}

.data-table {
  overflow: hidden;
  border: solid black;
  border-collapse: collapse;
  border-width: 1px 2px 2px 1px;
}

.data-cell {
  padding: 10px;
  font-size: 14px;
  position: relative;
  border: solid black;
  border-width: 1px 1px 1px 1px;
}

.data-row:hover {
  padding: 10px;
  background-color: #ffa;
}

.data-cell::before {
  content: "";
  position: absolute;
  top: 0;
  left: -5000px;
  width: 10000px;
  height: 100%;
  z-index: -10;
}

.data-cell:hover::after,.grey-column:hover::after {
  content: "";
  position: absolute;
  background-color: #ffa;
  left: 0;
  top: -5000px;
  height: 10000px;
  width: 100%;
  z-index: -5;
}
<table class="data-table">
  <tbody>
    <tr class="data-row">
      <td class="grey-column">I should change to hover color</td>
      <td class="data-cell">Cell 2</td>
      <td class="data-cell">Cell 3</td>
      <td class="data-cell">Cell 4</td>
    </tr>
    <tr class="data-row">
      <td class="grey-column">I should change to hover color</td>
      <td class="data-cell">Cell 2</td>
      <td class="data-cell">Cell 3</td>
      <td class="data-cell">Cell 4</td>
    </tr>
    <tr class="data-row">
      <td class="grey-column">I should change to hover color</td>
      <td class="data-cell">Cell 2</td>
      <td class="data-cell">Cell 3</td>
      <td class="data-cell">Cell 4</td>
    </tr>
  </tbody>
</table>

如果有任何疑问,请尽管询问。希望这对您有所帮助。

【讨论】:

  • @JeffFasulkey,很高兴我能帮上忙。还有一件事我建议对您的问题进行编辑,请考虑 [这是在 stackoverflow 中发布代码的理想方式]
  • 我有一个后续问题,将鼠标悬停在灰色列上时是否可以垂直?所以整个列“我应该更改为悬停颜色”也与悬停颜色相同?再次感谢您!
  • @JeffFasulkey 更新了我的答案,请检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多