Change Color of All Four Borders Even With `border-collapse: collapse;`
The following is a guest post by Daniel Jauch. It’s a quicky that covers something I bet many of us have run across at one time or another:
border-collapse is great, until it impedes on your ability to change a border color you’re trying to change because of the overlapping.
border-collapse property is great for making borders even all around
<table> cells. It makes for a clean look that I actually prefer.
But, there is something that can cause issues with how a browser draws the cells and their respective borders. Each cell is placed under the previous one when being drawn in order. This means that if tables have different colored borders, those borders are hidden on some sides.
In the code example below, hover over cells to see how borders can be hidden behind each other.
z-index doesn’t work on cells inside a
<table>, we have to work with the content inside the cells.
Fair warning: this solution will employ the use of negative margins. For those of you who don’t like negative margins, avert your eyes.
First, we put a wrapper around the content inside the cell. Move the border, padding, etc to the inside content to match.
This alone will actually reverse the problem: the borders on the bottom and right show up on top.
From there, we also have to add
z-index to the
:hover state so it stacks above the other elements. Because we’re z-indexing content inside the table cell, rather than the
<td> itself, it works.
Make sure the
z-index is not included in any
transition or you’ll see the previously hidden borders “swipe” into view rather than fade.