更改悬停时的 table 边框
Change table border upon hover
编辑
为了更清楚,下面是我要实现的目标。
我正在尝试创建一个 table,其中每一行都有一个带半径的像素边框(模仿折叠边框样式)。将鼠标悬停在一行上后,我希望所选行的边框改变颜色。我几乎用下面的代码实现了这一点,但是当鼠标悬停在一行上时,下一行会下降 1px 以便为悬停边框腾出空间。
table {
border-collapse: separate;
border-spacing: 0;
}
td {
border: solid 1px black;
border-style: solid none;
padding: 10px;
border-bottom: 0;
}
td:first-child {
border-left-style: solid;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
td:last-child {
border-right-style: solid;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
}
tr:last-child td {
border-bottom: solid 1px black;
}
tr:hover {
/*background-color: #cad6ed;*/
}
tr:hover td {
border: 1px solid #12A0F8;
border-style: solid none;
padding: 10px;
}
tr:hover td:first-child {
border-left-style: solid;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
tr:hover td:last-child {
border-right-style: solid;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
}
<table>
<tr>
<td>One</td><td>Two</td><td>Three</td>
</tr>
<tr>
<td>One</td><td>Two</td><td>Three</td>
</tr>
<tr>
<td>One</td><td>Two</td><td>Three</td>
</tr>
</table>
有更好的方法吗?
非常感谢。
编辑:包括 CSS var()
并更新 HTML 标记以包含该变量。
您需要在悬停时删除 border-width
属性 并仅使用 border-color
您还需要重叠 tr
元素,使边框看起来是一个像素.
我已将答案更新为使用 z-index
和一个负值 top
。请注意,这仅适用于三个 tr
元素,如果您需要动态解决方案,可以根据您生成 table 的方式更新答案。
table {
border-collapse: separate;
border-spacing: 0;
}
tr {
position: relative;
z-index: 1;
top: var(--rowCount);
}
td {
border: solid 1px black;
border-style: solid none;
padding: 10px;
}
td:first-child {
border-left-style: solid;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
td:last-child {
border-right-style: solid;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
}
tr:last-child td {
border-bottom: solid 1px black;
}
tr:hover {
/*background-color: #cad6ed;*/
z-index: 5;
}
tr:hover td {
border-color: #12A0F8;
border-style: solid none;
padding: 10px;
}
tr:hover td:first-child {
border-left-style: solid;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
tr:hover td:last-child {
border-right-style: solid;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
}
<table>
<tr style="--rowCount: 0px">
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr style="--rowCount: -1px">
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr style="--rowCount: -2px">
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr style="--rowCount: -3px">
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</table>
删除边框底部:0;在 td 标签中
table {
border-collapse: separate;
border-spacing: 0;
z-index:0;
}
td {
border: solid 1px black;
border-style: solid none;
padding: 10px;
border-bottom: 0;
}
td:first-child {
border-left-style: solid;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
td:last-child {
border-right-style: solid;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
}
tr:last-child td {
border-bottom: solid 1px black;
}
tr:hover td{
border-color: #12A0F8;
border-bottom: solid 1px #12A0F8;
padding-bottom: 9px;
}
<table>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</table>
更新了更好的解决方案
不知道为什么我没有从一开始就解决核心问题,但你的主要问题是下面的项目被推下了,你可以定位下一个项目并将边框顶部设置为 none
tr:hover + tr td {
border-top: none;
}
原始代码的其余部分保持不变
table {
border-collapse: separate;
border-spacing: 0;
}
td {
border: solid 1px black;
border-style: solid none;
padding: 10px;
border-bottom: 0;
}
tr:hover + tr td {
border-top: none;
}
td:first-child {
border-left-style: solid;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
td:last-child {
border-right-style: solid;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
}
tr:last-child td {
border-bottom: solid 1px black;
}
tr:hover {
/*background-color: #cad6ed;*/
}
tr:hover td {
border: 1px solid #12A0F8;
border-style: solid none;
padding: 10px;
}
tr:hover td:first-child {
border-left-style: solid;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
tr:hover td:last-child {
border-right-style: solid;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
}
<table>
<tr>
<td>One</td><td>Two</td><td>Three</td>
</tr>
<tr>
<td>One</td><td>Two dfds</td><td>Three</td>
</tr>
<tr>
<td>One</td><td>Two</td><td>Three</td>
</tr>
</table>
旧答案
您可以通过将 tr
设置为显示块来实现。我使用 margin-top -1px 来隐藏额外的边框。为了简化事情,我从 td 中删除了边框并将其放在 tr
上,因为无论如何 tds 之间没有边框
悬停时的 z-index 使悬停项弹出带有边框
除了第一个之外,我在所有 tr 上都设置了负边距,您可能可以在所有 tr
s
上使用它
table {
border-collapse: separate;
border-spacing: 0;
}
td {
padding: 10px;
}
tr {
border-radius: 10px;
border: 1px solid #000;
display: block;
box-sizing: border-box;
position: relative;
}
tr:not(:first-child) {
margin-top: -1px;
}
tr:hover {
z-index: 2;
border: 1px solid blue;
}
<table>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</table>
编辑
为了更清楚,下面是我要实现的目标。
我正在尝试创建一个 table,其中每一行都有一个带半径的像素边框(模仿折叠边框样式)。将鼠标悬停在一行上后,我希望所选行的边框改变颜色。我几乎用下面的代码实现了这一点,但是当鼠标悬停在一行上时,下一行会下降 1px 以便为悬停边框腾出空间。
table {
border-collapse: separate;
border-spacing: 0;
}
td {
border: solid 1px black;
border-style: solid none;
padding: 10px;
border-bottom: 0;
}
td:first-child {
border-left-style: solid;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
td:last-child {
border-right-style: solid;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
}
tr:last-child td {
border-bottom: solid 1px black;
}
tr:hover {
/*background-color: #cad6ed;*/
}
tr:hover td {
border: 1px solid #12A0F8;
border-style: solid none;
padding: 10px;
}
tr:hover td:first-child {
border-left-style: solid;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
tr:hover td:last-child {
border-right-style: solid;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
}
<table>
<tr>
<td>One</td><td>Two</td><td>Three</td>
</tr>
<tr>
<td>One</td><td>Two</td><td>Three</td>
</tr>
<tr>
<td>One</td><td>Two</td><td>Three</td>
</tr>
</table>
有更好的方法吗?
非常感谢。
编辑:包括 CSS var()
并更新 HTML 标记以包含该变量。
您需要在悬停时删除 border-width
属性 并仅使用 border-color
您还需要重叠 tr
元素,使边框看起来是一个像素.
我已将答案更新为使用 z-index
和一个负值 top
。请注意,这仅适用于三个 tr
元素,如果您需要动态解决方案,可以根据您生成 table 的方式更新答案。
table {
border-collapse: separate;
border-spacing: 0;
}
tr {
position: relative;
z-index: 1;
top: var(--rowCount);
}
td {
border: solid 1px black;
border-style: solid none;
padding: 10px;
}
td:first-child {
border-left-style: solid;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
td:last-child {
border-right-style: solid;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
}
tr:last-child td {
border-bottom: solid 1px black;
}
tr:hover {
/*background-color: #cad6ed;*/
z-index: 5;
}
tr:hover td {
border-color: #12A0F8;
border-style: solid none;
padding: 10px;
}
tr:hover td:first-child {
border-left-style: solid;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
tr:hover td:last-child {
border-right-style: solid;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
}
<table>
<tr style="--rowCount: 0px">
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr style="--rowCount: -1px">
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr style="--rowCount: -2px">
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr style="--rowCount: -3px">
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</table>
删除边框底部:0;在 td 标签中
table {
border-collapse: separate;
border-spacing: 0;
z-index:0;
}
td {
border: solid 1px black;
border-style: solid none;
padding: 10px;
border-bottom: 0;
}
td:first-child {
border-left-style: solid;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
td:last-child {
border-right-style: solid;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
}
tr:last-child td {
border-bottom: solid 1px black;
}
tr:hover td{
border-color: #12A0F8;
border-bottom: solid 1px #12A0F8;
padding-bottom: 9px;
}
<table>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</table>
更新了更好的解决方案
不知道为什么我没有从一开始就解决核心问题,但你的主要问题是下面的项目被推下了,你可以定位下一个项目并将边框顶部设置为 none
tr:hover + tr td {
border-top: none;
}
原始代码的其余部分保持不变
table {
border-collapse: separate;
border-spacing: 0;
}
td {
border: solid 1px black;
border-style: solid none;
padding: 10px;
border-bottom: 0;
}
tr:hover + tr td {
border-top: none;
}
td:first-child {
border-left-style: solid;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
td:last-child {
border-right-style: solid;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
}
tr:last-child td {
border-bottom: solid 1px black;
}
tr:hover {
/*background-color: #cad6ed;*/
}
tr:hover td {
border: 1px solid #12A0F8;
border-style: solid none;
padding: 10px;
}
tr:hover td:first-child {
border-left-style: solid;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
tr:hover td:last-child {
border-right-style: solid;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
}
<table>
<tr>
<td>One</td><td>Two</td><td>Three</td>
</tr>
<tr>
<td>One</td><td>Two dfds</td><td>Three</td>
</tr>
<tr>
<td>One</td><td>Two</td><td>Three</td>
</tr>
</table>
旧答案
您可以通过将 tr
设置为显示块来实现。我使用 margin-top -1px 来隐藏额外的边框。为了简化事情,我从 td 中删除了边框并将其放在 tr
上,因为无论如何 tds 之间没有边框
悬停时的 z-index 使悬停项弹出带有边框
除了第一个之外,我在所有 tr 上都设置了负边距,您可能可以在所有 tr
s
table {
border-collapse: separate;
border-spacing: 0;
}
td {
padding: 10px;
}
tr {
border-radius: 10px;
border: 1px solid #000;
display: block;
box-sizing: border-box;
position: relative;
}
tr:not(:first-child) {
margin-top: -1px;
}
tr:hover {
z-index: 2;
border: 1px solid blue;
}
<table>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</table>