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. 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:
- this is a very lightweight site, and I'd like to keep JavaScript to the barest minimum
- 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 specification, not poor CSS3 support, so any solution will still be relevant and useful when CSS3 has more widespread support.
If you want a CSS-only solution (no need to set
cellspacing=0
in the HTML) that allows for 1px borders (which you can't do with theborder-spacing: 0
solution), I prefer to do the following:border-right
andborder-bottom
for your table cells (td
andth
)border-top
border-left
first-child
andlast-child
selectors, round the appropriate corners for the table cells in the four corners.See a demo here.
Given the following HTML:
SEE example below:
As Ian said, the solution is to nest the table inside a div and set it like that:
With
overflow:hidden
, the square corners won't bleed through the div.Actually you can add your
table
inside adiv
as its wrapper. and then assign theseCSS
codes to wrapper:For a bordered and scrollable table, use this (replace variables,
$
starting texts)If you use
thead
,tfoot
orth
, just replacetr:first-child
andtr-last-child
andtd
with them.HTML:
I started experiment with "display" and I found that:
border-radius
,border
,margin
,padding
, in atable
are displayed with:For example
But we need set a
width
of every columnI figured it out. You just have to use some special selectors.
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:
Now everything rounds properly, except that there's still the issue of
border-collapse: collapse
breaking everything. A workaround is to setcellspacing="0"
in the html instead (thanks, Joel).