【问题标题】:CSS: TD background color causing borders to disappearCSS:TD背景颜色导致边框消失
【发布时间】:2015-08-20 17:00:17
【问题描述】:

我有一个动态创建的大型 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


    【解决方案1】:

    我自己也遇到了这个问题,但我不喜欢这里提供的解决方案,所以我一直在谷歌上搜索。我找到了这个答案: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找到

    【讨论】:

    【解决方案2】:

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

    改为这样添加:

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

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

    【讨论】:

    • 谢谢。我试过了,但这并没有改变它。
    • 可以分享一下网址吗?
    • 很抱歉,在这种情况下我不能,因为代码太大并且表格是动态创建的,但是所有 TR 的设置方式都相同,并且我发布了所有相关样式。跨度>
    • 哦,如果有 url,我认为编辑和显示比试错更简单,这就是为什么
    • 您是否尝试删除 position:relative css given?
    猜你喜欢
    • 2017-07-01
    • 2015-07-05
    • 2018-12-10
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 2015-03-07
    • 2017-09-19
    • 2014-05-24
    相关资源
    最近更新 更多