Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: TD background color causing borders to disappear

Tags:

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> 
like image 393
keewee279 Avatar asked Jun 05 '15 16:06

keewee279


People also ask

Does background color include margin?

The background-color property sets the background color of an element. The background of an element is the total size of the element, including padding and border (but not the margin). Tip: Use a background color and a text color that makes the text easy to read.

How do you apply borders to table cells and how do you fill their backgrounds with color or images?

To change the border's color, use the attribute bordercolor="color" where color is the same format as all the other web colors we've been using. The table below has the bordercolor set to #ff00ff with the table tag <table bordercolor="#ff00ff">. To change the background color, use the attribute bgcolor="color".

How do you add borders to TD?

Using pseudo elements: You can use a pseudo element to achieve this. Just absolutely position the pseudo element relative to the parent td element. Make the pseudo element fill the entire dimensions of the parent element, and then add the border to it. Save this answer.


1 Answers

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://stackoverflow.com/a/16337203/1156476

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

like image 125
pusle Avatar answered Sep 22 '22 17:09

pusle