CSS:TD背景颜色导致边框消失

问题描述 投票:4回答:2

我有一个动态创建的大型HTML表。桌子有标准结构,包括。 colgrouptheadtbody以及以下款式。

到目前为止,一切都按预期工作,但是当我在一列(见下文)中将“bgGrey”类添加到TD时,为了给这个列中的单元格提供背景颜色(仅在一列上需要),然后是所有边界此列在IE11中消失,除了左边框,并且:hover::before样式在Chrome中不再起作用(版本43)。 没有添加类"bgGrey"我在两个浏览器中都没有问题。

似乎某种程度上背景颜色与边界重叠导致了这种情况。

我的CSS(相关部分):

#myTable, #myTable tbody, #myTable thead, #myTable tr {
    width: 100%;
}
#myTable, #myTable th, #myTable td {
    border: 1px solid #000;
    border-collapse: collapse;
    margin: 0;
    padding: 4px;
    position: relative;
}
#myTable {
    font-size: 14px;
    table-layout: fixed;
}
#myTable th.editable:hover::before, #myTable td.editable:hover::before {
    border: 1px solid blue;
    content: '';
    position: absolute;
    top: 0; right: 0; bottom: 0; left: 0;
    z-index: -1;
}
#myTable .th1 {
    padding: 2px;
}
#myTable .th2 {
    font-weight: normal;
}

.bgGrey {
    background-color: #e6e6e6;
}

我的HTML(示例TR):

<tr>
    // ...
    <td class="editable"><div contenteditable="true"></div></td>
    <td class="bgGrey editable txtCenter"><div contenteditable="true"></div></td>
    <td class="editable txtRight"><div contenteditable="true"></div></td>
    // ...
</tr>
css border background-color border-color
2个回答
0
投票

请从border-collapse: collapse;中移除#myTable td,这会导致边界消失。避免给予td

改为添加如下:

#myTable, #myTable th, #myTable td {
    border: 1px solid #000;
    border-collapse: collapse;
    margin: 0;
    padding: 4px;
    position: relative; //REMOVE THIS
}

另外请你尝试从CSS中删除?


10
投票

我自己刚刚遇到这个问题,但我不喜欢这里提出的解决方案,所以我一直在谷歌上搜索。我找到了这个答案:https://stackoverflow.com/a/16337203/1156476

在这里,对表格单元格的简单添加可修复边框:

table td {
  border: 1px solid #000;
  border-collapse: collapse;
  margin: 0;
  padding: 4px;
  position: relative;
  background-clip: padding-box; /* Add this line */
}

检查Caniuse上的浏览器支持

有关该物业的解释可以在Standardista找到

© www.soinside.com 2019 - 2024. All rights reserved.