我正在实施一种解决方案,以基于CssTricks ::before
和::after
+负面z-index
的黑客来交叉突出显示表行和列。
目标是尽可能地仅使用CSS。
我想将所有突出显示的单元格文本设置为红色。
对于行来说,这很容易,我们只需要设置tbody tr:hover td { color: red; }
但是对于列,我无法找到一种方法来做... 有没有办法用列来达到相同的目的?
table {
overflow: hidden;
}
th {
background-color: silver;
color: white;
}
td, th {
padding: 10px;
position: relative;
outline: 0;
}
body:not(.nohover) tbody tr:hover {
background-color: #ffa;
}
/* font settings for highlighted Row */
tbody tr:hover td {
color: red;
font-weight: bold;
}
/* font settings for highlighted Column??????? */
/* (how to do it?) */
td:hover::after,
thead th:not(:empty):hover::after,
td:focus::after,
thead th:not(:empty):focus::after {
content: '';
height: 10000px;
left: 0;
position: absolute;
top: -5000px;
width: 100%;
z-index: -1;
}
td:hover::after,
th:hover::after {
background-color: #ffa;
}
td:focus::after,
th:focus::after {
background-color: lightblue;
}
/* Focus stuff for mobile */
td:focus::before,
tbody th:focus::before {
background-color: lightblue;
content: '';
height: 100%;
top: 0;
left: -5000px;
position: absolute;
width: 10000px;
z-index: -1;
}
<main>
<table>
<thead>
<tr>
<th></th>
<th class="col">50kg</th>
<th class="col">55kg</th>
<th class="col">60kg</th>
<th class="col">65kg</th>
<th class="col">70kg</th>
</tr>
</thead>
<tbody>
<tr>
<th class="row">160cm</th>
<td>20</td>
<td>21</td>
<td>23</td>
<td>25</td>
<td>27</td>
</tr>
<tr>
<th class="row">165cm</th>
<td>18</td>
<td>20</td>
<td>22</td>
<td>24</td>
<td>26</td>
</tr>
<tr>
<th class="row">170cm</th>
<td>17</td>
<td>19</td>
<td>21</td>
<td>23</td>
<td>25</td>
</tr>
<tr>
<th class="row">175cm</th>
<td>16</td>
<td>18</td>
<td>20</td>
<td>22</td>
<td>24</td>
</tr>
<tbody>
</table>
</main>
参见安东尼奥·罗德里格斯(
Row and Column Highlighting with CSS Only)的钢笔@antonio-rodrigues
在CodePen上。
预期结果:
为突出显示的“列”的所有单元格设置color: red
,并使所有其他表单元格保持默认黑色。
如何仅实现机智的CSS?