【发布时间】:2015-12-10 13:28:03
【问题描述】:
考虑以下设置:
$(function() {
var $cols = $("td:nth-child(2), th:nth-child(2)");
$cols.hover(function() {
$cols.addClass("highlight");
}, function() {
$cols.removeClass("highlight");
});
});
div {
background: #edf0f1;
padding: 20px;
}
table {
table-layout: fixed;
width: 500px;
border-collapse: separate;
border-spacing: 0;
}
td {
padding: 10px;
background-color: #fff;
border: 1px solid #edf0f1;
border-right-width: 10px;
border-left-width: 10px;
}
td.highlight,
th.highlight {
border-right-color: black;
border-left-color: black;
}
tr:last-child td {
border-bottom-width: 10px;
}
tr:last-child td.highlight {
border-bottom-color: black;
}
th {
border: 1px solid #edf0f1;
border-top-width: 10px;
border-right-width: 10px;
border-left-width: 10px;
}
th.highlight {
border-top: 10px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<table>
<thead>
<tr>
<th>Important</th>
<th>Information</th>
<th>Interchange</th>
</tr>
</thead>
<tbody>
<tr>
<td>Hello world, how are you today</td>
<td>again</td>
<td>and we're done</td>
</tr>
<tr>
<td>More things</td>
<td>Cow level is real!!1111</td>
<td>over 9000%</td>
</tr>
</tbody>
</table>
</div>
如您所见,突出显示的表格在悬停时从边框显示难看的“箭头”:
我怎样才能摆脱这些?
【问题讨论】:
-
这些颜色变淡的箭头是 td 单元格的边界顶部和边界底部的末端。您不能将边框的一部分绘制为一种颜色,而将部分边框绘制为另一种颜色。您将需要重新考虑整个 HTML 布局(您需要表格吗?或者 div 可以工作吗?)。
-
我尝试了 div,但我没有找到一种方法让它们像表格一样动态地处理文本长度的变化和中断。例如。当列文本变得大于它所拥有的空间时,整行将相应地调整大小。标题列等也是如此。据我发现,
divs 在更改时无法更改其他divs 的高度... -
问题是表格是基于行的,而你试图实现基于列的视觉效果。它只是行不通。您将能够得到的最接近的是这样的:jsfiddle.net/gep8c4jq 除非您编写一些非常复杂的 JavaScript(我不知道)来检测三个元素中的任何一个何时悬停,然后应用适当的风格各有千秋。如果您在问题中添加javascript 和/或jquery 标签,您可能会得到一些使用这些标签的解决方案。
-
仅供参考,将来会有另一种选择; CSS Grids:drafts.csswg.org/css-grid-1 它允许您按网格、列或行设置内容的样式和对齐内容,但在很长一段时间内将无法使用。
-
@TylerH 我找到了解决方案。无论如何感谢您的输入 :-) 无需等待 CSS 网格 :D
标签: html css html-table