gpt4 book ai didi

CSS 表格 tr 悬停一种颜色/边框和 td 悬停另一种颜色/边框而不重要

转载 作者:技术小花猫 更新时间:2023-10-29 11:35:40 25 4
gpt4 key购买 nike

我有以下 fiddle .

编辑:应用行边框样式:

table,th,td
{
border:2px solid black;
border-collapse:collapse;
}
tr:hover td
{
background-color:grey;
border-top: 2px solid rgb(10, 0, 255);
border-bottom: 2px solid rgb(10, 0, 255);
}
td:hover
{
background-color:rgb(214,214,214) !important;
border:2px solid red !important;
border-top: 2px solid red;
border-bottom: 2px solid red;
}

我要突出显示 ROW 和 CELL。本来想做点十字线,但是列背景着色需要JavaScript,我暂时不做。

在此希望 ROW:hover 背景颜色变暗并强调边框。

然后 td:hover 变成不同的颜色,并且边框强调不同。

在 Chrome 中检查,而不是在 Firefox 中检查。

问题:需要使用!important。单元格左边框和顶部未着色为红色?

那么有没有一种方法可以不使用 important! 来编写 CSS,并在 TD 单元格上正确设置边框样式?

最佳答案

您只需使用 CSS(无需 Javascript)即可完成您最初的十字准线创意。使用 ::before::after用于突出显示的伪元素。使用此方法时 Firefox 存在一个小问题,因此我为此编写了一个修复程序。

border-collapse: collapse<tds> 上建立边界以奇怪的方式行事。相反,将边框放在 <trs> 上和 <cols> . border-collapse: collapse也切掉边缘。为了解决这个问题,您必须发挥创意并在所有外部绘制额外的边框 <tds><ths> .更奇怪的是,它“吃掉”了顶部的一个像素和底部的两个像素。所以你需要 3px在顶部获得 2px边框,和 4px在底部得到一个2px边框。

演示: jsFiddle

CSS:

table {
border-collapse: collapse;
overflow: hidden;
z-index: 1;
}
td, th, .row, .col, .ff-fix {
cursor: pointer;
padding: 10px;
position: relative;
}
tr:last-child td {
border-bottom: 4px solid black;
}
tr, col {
border: 2px solid black;
}
th {
border-top: 3px solid black;
}
th:last-child, td:nth-child(3n) {
border-right: 4px solid black;
}
td:first-child, th:first-child {
border-left: 3px solid black;
}
td:hover {
border: 2px solid red;
}
td:hover:first-child {
border-left: 3px solid red;
}
td:hover:nth-child(3n) {
border-right: 4px solid red;
}
tr:last-child td:hover {
border-bottom: 4px solid red;
}
td:hover::before,
.row:hover::before,
.ff-fix: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,
.ff-fix:hover::after {
background-color: #ffa;
content: '\00a0';
height: 10000px;
left: 0;
position: absolute;
top: -5000px;
width: 100%;
z-index: -1;
}

HTML:

<table>
<col /><col /><col />
<tr>
<th class="col">First Name</th>
<th class="col">Middle Name</th>
<th class="col">Last Name</th>
</tr>
<tr>
<td>Peter</td>
<td>Jeffery</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Marie</td>
<td>Griffin</td>
</tr>
<tr>
<td>Margie</td>
<td>Ann</td>
<td>Thatcher</td>
</tr>
</table>

脚本:

function firefoxFix() {
if ( /firefox/.test( window.navigator.userAgent.toLowerCase() ) ) {
var tds = document.getElementsByTagName( 'td' ),
ths = document.getElementsByTagName( 'th' );
for( var index = 0; index < tds.length; index++ ) {
tds[index].innerHTML = '<div class="ff-fix">' + tds[index].innerHTML + '</div>';
};
for( var index = 0; index < ths.length; index++ ) {
ths[index].innerHTML =
'<div class="' + ths[index].className + '">'
+ ths[index].innerHTML
+ '</div>';
ths[index].className = '';
};
var style = '<style>'
+ 'td, th { padding: 0 !important; }'
+ 'td:hover::before, td:hover::after { background-color: transparent !important; }'
+ '</style>';
document.head.insertAdjacentHTML( 'beforeEnd', style );
};
};

firefoxFix();

关于CSS 表格 tr 悬停一种颜色/边框和 td 悬停另一种颜色/边框而不重要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15269921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com