TD background color causing borders to disappear
Asked Answered
H

2

35

I have a large HTML table that is created dynamically. The table has a standard structure, incl. colgroup, thead and tbody and the below styles.

So far everything works as intended but when I add the class "bgGrey" to the TDs in one column (see below) in order to give the cells in this column a background color (which is only needed on one column) then all borders of this column disappear in IE11, except for the left border, and the :hover::before style doesn't work anymore in Chrome (version 43).
Without adding the class "bgGrey" I have no issues in both browsers.

It seems that somehow the background color overlaps the border causing this.

My CSS (relevant part):

#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;
}

My HTML (example 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>
Hollander answered 5/6, 2015 at 16:42 Comment(0)
P
-2

Please remove border-collapse: collapse; from #myTable td, which causes the border to disappear. Avoid giving that for td.

Add like this instead:

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

Also please can you try removing "position:relative" from the CSS?

Peaceable answered 5/6, 2015 at 16:53 Comment(9)
Thanks for this. I tried but this doesnt change it.Hollander
Can you please share url?Peaceable
I am sorry, in this case I can't as the code is too large and the table is created dynamically but all TRs are set up the same way and I posted all the relevant styles.Hollander
Oh if url is there i think it will be simple to edit and show you than trial and error thats whyPeaceable
Did you tried removing the position:relative css given?Peaceable
I cannot remove this as it is needed for the hover color.Hollander
I think we can give hover in other way without using position:absolute for tdPeaceable
Thanks - this helped me to find a workaround. It works fine now.Hollander
Removing the what from the CSS?Shope
R
68

I just came upon this problem myself, but I didn't like the solution presented here, so I kept googling. I found this answer: https://mcmap.net/q/215874/-borders-not-shown-in-firefox-with-border-collapse-on-table-position-relative-on-tbody-or-background-color-on-cell

Here, a simple addition to the table cell fixes the borders:

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

Check browser support at Caniuse

And an explanation of the property can be found at Standardista

Rikki answered 28/2, 2018 at 11:39 Comment(1)
Linked explanation gives me a security warning, here is an article on MDN developer.mozilla.org/en-US/docs/CSS/background-clipServomotor
P
-2

Please remove border-collapse: collapse; from #myTable td, which causes the border to disappear. Avoid giving that for td.

Add like this instead:

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

Also please can you try removing "position:relative" from the CSS?

Peaceable answered 5/6, 2015 at 16:53 Comment(9)
Thanks for this. I tried but this doesnt change it.Hollander
Can you please share url?Peaceable
I am sorry, in this case I can't as the code is too large and the table is created dynamically but all TRs are set up the same way and I posted all the relevant styles.Hollander
Oh if url is there i think it will be simple to edit and show you than trial and error thats whyPeaceable
Did you tried removing the position:relative css given?Peaceable
I cannot remove this as it is needed for the hover color.Hollander
I think we can give hover in other way without using position:absolute for tdPeaceable
Thanks - this helped me to find a workaround. It works fine now.Hollander
Removing the what from the CSS?Shope

© 2022 - 2024 — McMap. All rights reserved.