Table border color in CSS with border collapse

你离开我真会死。 提交于 2019-11-27 20:03:05

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/

Dalius I

Just change border-collapse to separate and set border-spacing to zero.

Note: IE8 supports the border-spacing property only if a !DOCTYPE is specified.

<!DOCTYPE html>
<html>

<head>
  <style type="text/css">
    table {
      border-collapse: separate;
      border-spacing: 0px;
    }
    
    td.first {
      border-bottom: solid red 1px;
    }
    
    td.second {
      border-top: solid gold 1px;
    }
  </style>
</head>

<body>
  <table>
    <tr>
      <td class="first">Hello</td>
    </tr>
    <tr>
      <td class="second">World</td>
    </tr>
  </table>
</body>

</html>

Tested on win7 with: Chrome 16, IE 9, FF 9, Safari 5.0.5.

Remove border-collapse: collapse; from your code, instead set cellspacing attribute to 0 for your table.

Just remove the td.first { border-bottom: solid red 1px; } from your style.

Or change red to gold in the td.first selector.

Example here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!