views:

8487

answers:

7

Edit - Original Title: Is there an alternative way to achieve border-collapse:collapse in CSS (in order to have a collapsed, rounded corner table)?

Since it turns out that simply getting the table's borders to collapse does not solve the root problem, I have updated the title to better reflect the discussion.

I am trying to make a table with rounded corners using the CSS3 border-radius property. The table styles I'm using look something like this:

table {
  -moz-border-radius:10px;
  -webkit-border-radius:10px;
  border-radius:10px}

Here's the problem. I also want to set the border-collapse:collapse property, and when that is set border-radius no longer works (at least in Firefox)(edit- I thought this might just be a difference in mozilla's implementation, but it turns out this is the way it's supposed to work according to the w3c). Is there a CSS-based way I can get the same effect as border-collapse:collapse without actually using it?

Edits:

I've made a simple page to demonstrate the problem here (Firefox/Safari only).

It seems that a large part of the problem is that setting the table to have rounded corners does not affect the corners of the corner td elements. If the table was all one color, this wouldn't be a problem since I could just make the top and bottom td corners rounded for the first and last row respectively. However, I am using different background colors for the table to differentiate the headings and for striping, so the inner td elements would show their rounded corners as well.

Summary of proposed solutions:

Surrounding the table with another element with round corners doesn't work because the table's square corners "bleed through."

Specifying border width to 0 doesn't collapse the table.

Bottom td corners still square after setting cellspacing to zero.

Using javascript instead- works by avoiding the problem.

Possible solutions:

The tables are generated in php, so I could just apply a different class to each of the outer th/tds and style each corner separately. I'd rather not do this, since it's not very elegant and a bit of a pain to apply to multiple tables, so please keep suggestions coming.

Possible solution 2 is to use javascript (jQuery, specifically) to style the corners. This solution also works, but still not quite what I'm looking for (I know I'm picky). I have two reservations: 1) this is a very lightweight site, and I'd like to keep javascript to the barest minimum 2) part of the appeal that using border-radius has for me is graceful degradation and progressive enhancement. By using border-radius for all rounded corners, I hope to have a consistently rounded site in CSS3-capable browsers and a consistently square site in others (I'm looking at you, IE).

I know that trying to do this with CSS3 today may seem needless, but I have my reasons. I would also like to point out that this problem is a result of the w3c speficication, not poor CSS3 support, so any solution will still be relevant and useful when CSS3 has more widespread support.

A: 

To the best of my knowledge, the only way you could do it would be to modify all the cells like so:

table td 
{
    border-right-width:0px;
    border-bottom-width:0px;
}

And then to get the border on the bottom and right back

table tr td:last-child
{
    border-right-width:1px;
}
table tr:last-child td
{
    border-bottom-width:1px;
}

:last-child is not valid in ie6, but if you are using border-radius I assume you don't care.

EDIT:

After looking at your example page, it appears that you may be able to work around this with cell spacing and padding.

The thick gray borders you are seeing are actually the background of the table (you can see this clearly if you change the border color to red). If you set the cellspacing to zero (or equivalently: "td, th { margin:0; }") the grey "borders" will disappear.

EDIT 2:

I can't find a way to do this with only one table. If you change your header row to a nested table, you might possibly be able to get the effect you want, but it'll be more work, and not dynamic.

Joel Potter
Thank you Ithi, but it's not working for me.
vamin
I've added an example with cellspacing=0, and it's much closer. The undesireable borders disappear, but the bottom corners still bleed out.
vamin
Thanks again for your help. The tables are generated in php, so I'm thinking if there isn't an elegent solution proposed I'll just assign a class to each corner th/td and style them separately.
vamin
+1  A: 

You'll probably have to put another element around the table and style that with a rounded border.

The working draft specifies that border-radius does not apply to table elements when ‘border-collapse’ is ‘collapse’.

That was something I considered as well, but if I create a div to surround the table and set it to have rounded corners, the square table corners still bleed through. See the newly-posted example.
vamin
The best compromise I could find was adding a THEAD block to the table and applying the grey background to it (with #eee on the table itself). The header cells overflowed behind the TABLE's border instead in front of it. Then I increased the table border to 3px to hide the overflow.
A: 

jquery.corner plugin provides a nice alternative:

http://www.malsup.com/jquery/corner/ has a nice demo of it's capabilities. You can specify which corners to alter and what style of alteration you want to perform by defining the radius, etc.

David Robbins
Thanks for the tip. That would work, but I am still looking for a CSS solution.
vamin
+13  A: 

I figured it out. You just have to use some special selectors.

Here's a working example. (edit- only works in Safari and Firefox for now)

The problem with rounding the corners of the table was that the td elements didn't also become rounded. You can solve that by doing something like this:

table tr:last-child td:first-child {
-moz-border-radius-bottomleft:10px;
-webkit-border-bottom-left-radius:10px;
border-bottom-left-radius:10px}

table tr:last-child td:last-child {
-moz-border-radius-bottomright:10px;
-webkit-border-bottom-right-radius:10px;
border-bottom-right-radius:10px}

Now everything rounds properly, except that there's still the issue of border-collapse:collapse breaking everything. A workaround is to set cellspacing=0 in the html instead (thanks, Joel).

vamin
A: 

for chrome following method works:

table{
  border-collapse: collapse;
 -webkit-box-shadow:0 0 1px #666; /* this item draw table border  */ 
 -webkit-border-radius:30px;
  border-style: hidden; /* hide standart table (collapsed) border */
}

td {
   border:1px solid #ccc;
}
cmrd.Kaash
A: 

Have you tried using table{border-spacing: 0} instead of table{border-collapse: collapse} ???

Cesar
A: 

I've been having the same issue and Cesar's suggestion to use table{border-spacing: 0;} worked nicely. Thanks, Cesar!

Dimeon