Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table border color in CSS with border collapse

I want to put a line above some field in a table, to indicate that it is a sum of the above values. However, the table already has borders by default.

Here is an example: I have a table with borders collapsed. I set the border-bottom on one field, and the border-top on the field below it. Both of these specify the same border. The CSS for the top one is used. Is there a way to use the bottom one?

<html>     <head>         <style type="text/css">             table { border-collapse: collapse; }             td.first { border-bottom: solid red 1px; }             td.second { border-top: solid gold 1px; }         </style>      <body>         <table>             <tr><td class="first">Hello</td></tr>             <tr><td class="second">World</td></tr>         </table>     </body> </html> 

This shows two cells with a red line between them. Is there way to get a gold line?

like image 863
Sjoerd Avatar asked Nov 04 '10 11:11

Sjoerd


People also ask

Which CSS property can be used for collapsing the border between table cells?

Choose the CSS property that can be used for collapsing the borders between table cells? Explanation: The border-collapse property sets whether the table borders are collapsed into a single border or detached as in standard HTML.


1 Answers

This is a defined behavior of border-collapse. Page 357 of the O'Reilly CSS Definitive Guide 3rd Edition says:

if collapsing borders have the same style and width, but differ in color, then ... from most to least preferred: cell, row, row group, column, column group, table.

if ... come from same type of element, such as two rows... then color is taken from borders that are the topmost and the leftmost.

So the topmost, which is red, wins out.

One way to override this may be to use cell for the color to win over the color for the row.

example: http://jsfiddle.net/Chapm/

The rules that have higher precedence than this "same color rule is"

wider border wins over narrower ones

and after that,

double wins over solid, then dashed, dotted, ridge, outset, groove, inset

You can use 2px for the gold for it to win over, and I tried in Chrome to use 1px but double, and it appears as 1px solid and it will win over the red as well. Although if you want to use this method, then it may be best to make sure the browsers you support behave the same using this technique.

http://jsfiddle.net/Chapm/2/

like image 81
nonopolarity Avatar answered Sep 26 '22 13:09

nonopolarity