css - 将鼠标悬停在表格上时如何突出显示表格的行和列?

标签 css html-table hover background-color

我想做与本例中完全相同的事情:http://jsfiddle.net/ThinkingStiff/rUhCa/ .

但是,当我尝试更改表格的背景颜色时,悬停和突出显示停止工作。

下面你可以看到我的代码:

HTML

<table>
    <tr>
        <th></th>
        <th class="col">50kg</th>
        <th class="col">55kg</th>
        <th class="col">60kg</th>
    </tr>
    <tr>
        <th class="row">160cm</th>
        <td>20</td>
        <td>21</td>
        <td>23</td>
    </tr>
</table>

CSS

table {
   border-spacing: 0;
   border-collapse: collapse;
   overflow: hidden;
   z-index: 1;
}

td, th, .row, .col {
   cursor: pointer;
   padding: 10px;
   position: relative;
}

td:hover::before,
.row:hover::before { 
   background-color: #ffa;
   content: '\00a0';  
   height: 100%;
   left: -5000px;
   position: absolute;  
   top: 0;
   width: 10000px;   
   z-index: -1;        
}

td:hover::after,
.col:hover::after { 
   background-color: #ffa;
   content: '\00a0';  
   height: 10000px;    
   left: 0;
   position: absolute;  
   top: -5000px;
   width: 100%;
   z-index: -1;        
}

最佳答案

假设这是您正在寻找的东西。您设置 z-index: -1 会在 table 具有背景颜色时隐藏背景荧光笔,因为它的 z-index 值高于负值。但你真的根本不需要它们。您应该使用 RGBA 颜色 - 它指定颜色的不透明度来进行突出显示。希望对您有所帮助!

table {
   border-spacing: 0;
   border-collapse: collapse;
   overflow: hidden;
   background-color: tomato;
}

td, th {
   padding: 10px;
   position: relative;
}

tr:hover{
   background-color: rgba(255, 255, 0, 0.5);
}

td:hover::after,th:hover::after { 
   background-color: rgba(255, 255, 0, 0.5);
   content: '\00a0';  
   height: 10000px;    
   left: 0;
   position: absolute;  
   top: -5000px;
   width: 100%;   
}
<table>
    <tr>
        <th></th>
        <th>50kg</th>
        <th>55kg</th>
        <th>60kg</th>
        <th>65kg</th>
        <th>70kg</th>
    </tr>

    <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>

</table>


























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































<script>

/* by: thinkingstiff.com
license: http://creativecommons.org/licenses/by-nc-sa/3.0/us/ */

var headerUri = 'http://stackoverflow.com/q/848840/918414',
    headerCaption = 'cols, colgroups and css :hover psuedoclass';
    
document.body.insertAdjacentHTML( 
    'afterBegin',
      '<a href="' + headerUri + '" '
    + 'target="_top" '
    + 'onmouseover="this.style.opacity=\'.95\'" '
    + 'onmouseout="this.style.opacity=\'1\'" '
    + 'style="'
        + 'background-color: black;'
        + 'background-image: linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );'
        + 'background-image: -webkit-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );'
        + 'background-image: -moz-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );'
        + 'background-image: -o-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );'
        + 'background-image: -ms-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );'
        + 'border: 1px solid black;'
        + 'border-radius: 2px;'
        + 'color: white;'
        + 'display: block;'
        + 'font: normal 15px/26px Helvetica, Verdana, Tahoma;'
        + 'height: 26px;'
        + 'left: 0px;'
        + 'opacity: 1;'
        + 'overflow: hidden;'
        + 'padding: 2px 8px;'
        + 'position: fixed;'
        + 'right: 0px;'
        + 'text-decoration: none;'
        + 'text-overflow: ellipsis;'
        + 'text-shadow: -1px -1px black;'
        + 'top: 0px;'
        + 'white-space: nowrap;'
        + 'z-index: 9999;'
    + '"><img '
    + 'style="' 
        + 'display: block;'
        + 'float: left;'
        + 'margin-right: 8px;" '
    + 'src="http://thinkingstiff.com/images/stackoverflow.png" />'
    + headerCaption
    + '</a>'
    );

document.body.insertAdjacentHTML( 
    'afterBegin', 
    '<a href="http://thinkingstiff.com" '
    + 'target="_top" '
    + 'onmouseover="this.style.opacity=\'.95\'" '
    + 'onmouseout="this.style.opacity=\'1\'" '
    + 'style="'
        + 'background-color: black;'
        + 'background-image: linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );'
        + 'background-image: -webkit-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );'
        + 'background-image: -moz-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );'
        + 'background-image: -o-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );'
        + 'background-image: -ms-linear-gradient( top, rgba( 255, 255, 255, .3), rgba( 255, 255, 255, 0) );'
        + 'border: 1px solid black;'
        + 'border-radius: 2px;'
        + 'bottom: 0;'
        + 'color: white;'
        + 'display: block;'
        + 'font-family: Helvetica, Verdana, Tahoma;'
        + 'opacity: 1;'
        + 'padding: 4px 8px;'
        + 'position: fixed;'
        + 'right: 0;'
        + 'text-decoration: none;'
        + 'text-shadow: -1px -1px black;'
        + 'z-index: 999;'
    + '">thinkingstiff.com</a>' 
    ); 

document.head.insertAdjacentHTML( 'beforeEnd',
    '<style>'
    + 'body { margin-top: 40px !important; }'
    + '</style>'
    );
    
    
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-23915674-6']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
    
</script>

关于css - 将鼠标悬停在表格上时如何突出显示表格的行和列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54203811/

相关文章:

javascript - 如何使用 jQuery 在悬停时将 div 的图像源与另一个图像交换

html - CSS垂直下拉菜单,当悬停到其他菜单时子菜单显示

css - 如何在CSS中保持图像的纵横比?

html - Vue.js 2 - 从 body 标签中删除初始边距

如果 <td> 中有多个 <div>,则 <td> 上悬停效果的 CSS 无法正常工作

html - 如何使 HTML 表格 "Excel-like"可编辑多个单元格,即同时复制粘贴功能?

jQuery 悬停效果使用不透明度,IE 不支持

html - <dl> 的呈现在嵌入 <ol> 时显示为列表

html - 我可以在样式表中覆盖元素的内联样式吗?

html - <td> 大于指定值