I have searched and searched, but haven't been able to find a solution for my requirement.
I have a plain ol' HTML table. I want round corners for it, without using images or JS, i.e. pure CSS only. Like this:
Rounded corners for corner cells, and 1px
thick border for the cells.
So far I have this:
table {
-moz-border-radius: 5px !important;
border-collapse: collapse !important;
border: none !important;
}
table th,
table td {
border: none !important
}
table th:first-child {
-moz-border-radius: 5px 0 0 0 !important;
}
table th:last-child {
-moz-border-radius: 0 5px 0 0 !important;
}
table tr:last-child td:first-child {
-moz-border-radius: 0 0 0 5px !important;
}
table tr:last-child td:last-child {
-moz-border-radius: 0 0 5px 0 !important;
}
table tr:hover td {
background-color: #ddd !important
}
But that leaves me without any borders for the cells. If I add borders, they aren't rounded!
Any solutions?
This question is related to
html
css
html-table
rounded-corners
Seems to work fine in FF and Chrome (haven't tested any others) with separate borders: http://jsfiddle.net/7veZQ/3/
Edit: Here's a relatively clean implementation of your sketch:
table {_x000D_
border-collapse:separate;_x000D_
border:solid black 1px;_x000D_
border-radius:6px;_x000D_
-moz-border-radius:6px;_x000D_
}_x000D_
_x000D_
td, th {_x000D_
border-left:solid black 1px;_x000D_
border-top:solid black 1px;_x000D_
}_x000D_
_x000D_
th {_x000D_
background-color: blue;_x000D_
border-top: none;_x000D_
}_x000D_
_x000D_
td:first-child, th:first-child {_x000D_
border-left: none;_x000D_
}
_x000D_
<table>_x000D_
<thead>_x000D_
<tr>_x000D_
<th>blah</th>_x000D_
<th>fwee</th>_x000D_
<th>spoon</th>_x000D_
</tr>_x000D_
</thead>_x000D_
<tr>_x000D_
<td>blah</td>_x000D_
<td>fwee</td>_x000D_
<td>spoon</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>blah</td>_x000D_
<td>fwee</td>_x000D_
<td>spoon</td>_x000D_
</tr>_x000D_
</table>
_x000D_
This is css3
, only recent non-IE<9 browser will support it.
Check out here, it derives the round property for all available browsers
The best solution I've found for rounded corners and other CSS3 behavior for IE<9 can be found here: http://css3pie.com/
Download the plug-in, copy to a directory in your solution structure. Then in your stylesheet make sure to have the behavior tag so that it pulls in the plug-in.
Simple example from my project which gives me rounded corners, color gradient, and box shadow for my table:
.table-canvas
{
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
overflow:hidden;
border-radius: 10px;
-pie-background: linear-gradient(#ece9d8, #E5ECD8);
box-shadow: #666 0px 2px 3px;
behavior: url(Include/PIE.htc);
overflow: hidden;
}
Don't worry if your Visual Studio CSS intellisense gives you the green underline for unknown properites, it still works when you run it. Some of the elements are not very clearly documented, but the examples are pretty good, especially on the front page.
To adapt @luke flournoy's brilliant answer - and if you're not using th
in your table, here's all the CSS you need to make a rounded table:
.my_table{
border-collapse: separate;
border-spacing: 0;
border: 1px solid grey;
border-radius: 10px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
}
.my_table tr:first-of-type {
border-top-left-radius: 10px;
}
.my_table tr:first-of-type td:last-of-type {
border-top-right-radius: 10px;
}
.my_table tr:last-of-type td:first-of-type {
border-bottom-left-radius: 10px;
}
.my_table tr:last-of-type td:last-of-type {
border-bottom-right-radius: 10px;
}
The following is something I used that worked for me across browsers so I hope it helps someone in the future:
#contentblock th:first-child {
-moz-border-radius: 6px 0 0 0;
-webkit-border-radius: 6px 0 0 0;
border-radius: 6px 0 0 0;
behavior: url(/images/border-radius.htc);
border-radius: 6px 0 0 0;
}
#contentblock th:last-child {
-moz-border-radius: 0 6px 0 0;
-webkit-border-radius: 0 6px 0 0;
border-radius: 0 6px 0 0;
behavior: url(/images/border-radius.htc);
border-radius: 0 6px 0 0;
}
#contentblock tr:last-child td:last-child {
border-radius: 0 0 6px 0;
-moz-border-radius: 0 0 6px 0;
-webkit-border-radius: 0 0 6px 0;
behavior: url(/images/border-radius.htc);
border-radius: 0 0 6px 0;
}
#contentblock tr:last-child td:first-child {
-moz-border-radius: 0 0 0 6px;
-webkit-border-radius: 0 0 0 6px;
border-radius: 0 0 0 6px;
behavior: url(/images/border-radius.htc);
border-radius: 0 0 0 6px;
}
Obviously the #contentblock
portion can be replaced/edited as needed and you can find the border-radius.htc
file by doing a search in Google or your favorite web browser.
Through personal expeirence I've found that it's not possible to round corners of an HTML table cell with pure CSS. Rounding a table's outermost border is possible.
You will have to resort to using images as described in this tutorial, or any similar :)
CSS:
table {
border: 1px solid black;
border-radius: 10px;
border-collapse: collapse;
overflow: hidden;
}
td {
padding: 0.5em 1em;
border: 1px solid black;
}
For a bordered and scrollable table, use this (replace variables, $
starting texts)
If you use thead
, tfoot
or th
, just replace tr:first-child
and tr-last-child
and td
with them.
#table-wrap {
border: $border solid $color-border;
border-radius: $border-radius;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
table td { border: $border solid $color-border; }
table td:first-child { border-left: none; }
table td:last-child { border-right: none; }
table tr:first-child td { border-top: none; }
table tr:last-child td { border-bottom: none; }
table tr:first-child td:first-child { border-top-left-radius: $border-radius; }
table tr:first-child td:last-child { border-top-right-radius: $border-radius; }
table tr:last-child td:first-child { border-bottom-left-radius: $border-radius; }
table tr:last-child td:last-child { border-bottom-right-radius: $border-radius; }
HTML:
<div id=table-wrap>
<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>
</div>
NOTE: The HTML/CSS code below should be viewed in IE only. The code will not be rendered correctly in Chrome!
We need to remember that:
A table has a border: its outer boundary (which can also have a border-radius.)
The cells themselves ALSO have borders (which too, can also have a border-radius.)
The table and cell borders can interfere with each other:
The cell border can pierce through the table border (ie: table boundary).
To see this effect, amend the CSS style rules in the code below as follows:
i. table {border-collapse: separate;}
ii. Delete the style rules which round the corner cells of the table.
iii. Then play with border-spacing so you can see the interference.
However, the table border and cell borders can be COLLAPSED (using: border-collapse: collapse;).
When they are collapsed, the cell and table borders interfere in a different way:
i. If the table border is rounded but cell borders remain square, then the cell's shape takes precedence and the table loses its curved corners.
ii. Conversely, if the corner cell's are curved but the table boundary is square, then you will see an ugly square corner bordering the curvature of the corner cells.
Given that cell's attribute takes precedence, the way to round the table's four corner's then, is by:
i. Collapsing borders on the table (using: border-collapse: collapse;).
ii. Setting your desired curvature on the corner cells of the table.
iii. It does not matter if the table's corner's are rounded (ie: Its border-radius can be zero).
.zui-table-rounded {_x000D_
border: 2px solid blue;_x000D_
/*border-radius: 20px;*/_x000D_
_x000D_
border-collapse: collapse;_x000D_
border-spacing: 0px;_x000D_
}_x000D_
_x000D_
.zui-table-rounded thead th:first-child {_x000D_
border-radius: 30px 0 0 0;_x000D_
}_x000D_
_x000D_
.zui-table-rounded thead th:last-child {_x000D_
border-radius: 0 10px 0 0;_x000D_
}_x000D_
_x000D_
.zui-table-rounded tbody tr:last-child td:first-child {_x000D_
border-radius: 0 0 0 10px;_x000D_
}_x000D_
_x000D_
.zui-table-rounded tbody tr:last-child td:last-child {_x000D_
border-radius: 0 0 10px 0;_x000D_
}_x000D_
_x000D_
_x000D_
.zui-table-rounded thead th {_x000D_
background-color: #CFAD70;_x000D_
}_x000D_
_x000D_
.zui-table-rounded tbody td {_x000D_
border-top: solid 1px #957030;_x000D_
background-color: #EED592;_x000D_
}
_x000D_
<table class="zui-table-rounded">_x000D_
<thead>_x000D_
<tr>_x000D_
<th>Name</th>_x000D_
<th>Position</th>_x000D_
<th>Height</th>_x000D_
<th>Born</th>_x000D_
<th>Salary</th>_x000D_
</tr>_x000D_
</thead>_x000D_
<tbody>_x000D_
<tr>_x000D_
<td>DeMarcus Cousins</td>_x000D_
<td>C</td>_x000D_
<td>6'11"</td>_x000D_
<td>08-13-1990</td>_x000D_
<td>$4,917,000</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Isaiah Thomas</td>_x000D_
<td>PG</td>_x000D_
<td>5'9"</td>_x000D_
<td>02-07-1989</td>_x000D_
<td>$473,604</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Ben McLemore</td>_x000D_
<td>SG</td>_x000D_
<td>6'5"</td>_x000D_
<td>02-11-1993</td>_x000D_
<td>$2,895,960</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Marcus Thornton</td>_x000D_
<td>SG</td>_x000D_
<td>6'4"</td>_x000D_
<td>05-05-1987</td>_x000D_
<td>$7,000,000</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Jason Thompson</td>_x000D_
<td>PF</td>_x000D_
<td>6'11"</td>_x000D_
<td>06-21-1986</td>_x000D_
<td>$3,001,000</td>_x000D_
</tr>_x000D_
</tbody>_x000D_
</table>
_x000D_
The selected answer is terrible. I would implement this by targeting the corner table cells and applying the corresponding border radius.
To get the top corners, set the border radius on the first and last of type of the th elements, then finish by setting the border radius on the last and first of td type on the last of type tr to get the bottom corners.
th:first-of-type {
border-top-left-radius: 10px;
}
th:last-of-type {
border-top-right-radius: 10px;
}
tr:last-of-type td:first-of-type {
border-bottom-left-radius: 10px;
}
tr:last-of-type td:last-of-type {
border-bottom-right-radius: 10px;
}
Add between <head>
tags:
<style>
td {background: #ffddaa; width: 20%;}
</style>
and in the body:
<div style="background: black; border-radius: 12px;">
<table width="100%" style="cell-spacing: 1px;">
<tr>
<td style="border-top-left-radius: 10px;">
Noordwest
</td>
<td> </td>
<td>Noord</td>
<td> </td>
<td style="border-top-right-radius: 10px;">
Noordoost
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>West</td>
<td> </td>
<td>Centrum</td>
<td> </td>
<td>Oost</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td style="border-bottom-left-radius: 10px;">
Zuidwest
</td>
<td> </td>
<td>Zuid</td>
<td> </td>
<td style="border-bottom-right-radius: 10px;">
Zuidoost
</td>
</tr>
</table>
</div>
The cell color, contents and formatting are of course for example;
it's about spacing color-filled cells within a div.
Doing so, the black cell borders/table border are actually the div background color.
Note that you'll need to set the div-border-radius about 2 values greater than the separate cell corner border radii, to take a smooth rounded corner effect.
For me, the Twitter Bootstrap Solution looks good. It excludes IE < 9 (no round corners in IE 8 and lower), but that's O.K. I think, if you develop prospective Web-Apps.
CSS/HTML:
table { _x000D_
border: 1px solid #ddd;_x000D_
border-collapse: separate;_x000D_
border-left: 0;_x000D_
border-radius: 4px;_x000D_
border-spacing: 0px;_x000D_
}_x000D_
thead {_x000D_
display: table-header-group;_x000D_
vertical-align: middle;_x000D_
border-color: inherit;_x000D_
border-collapse: separate;_x000D_
}_x000D_
tr {_x000D_
display: table-row;_x000D_
vertical-align: inherit;_x000D_
border-color: inherit;_x000D_
}_x000D_
th, td {_x000D_
padding: 5px 4px 6px 4px; _x000D_
text-align: left;_x000D_
vertical-align: top;_x000D_
border-left: 1px solid #ddd; _x000D_
}_x000D_
td {_x000D_
border-top: 1px solid #ddd; _x000D_
}_x000D_
thead:first-child tr:first-child th:first-child, tbody:first-child tr:first-child td:first-child {_x000D_
border-radius: 4px 0 0 0;_x000D_
}_x000D_
thead:last-child tr:last-child th:first-child, tbody:last-child tr:last-child td:first-child {_x000D_
border-radius: 0 0 0 4px;_x000D_
}
_x000D_
<table>_x000D_
<thead>_x000D_
<tr><th>xxx</th><th>xxx</th><th>xxx</th></tr>_x000D_
</thead>_x000D_
<tbody>_x000D_
<tr><td>xxx</td><td>xxx</td><td>xxx</td></tr>_x000D_
<tr><td>xxx</td><td>xxx</td><td>xxx</td></tr>_x000D_
<tr><td>xxx</td><td>xxx</td><td>xxx</td></tr>_x000D_
<tr><td>xxx</td><td>xxx</td><td>xxx</td></tr>_x000D_
<tr><td>xxx</td><td>xxx</td><td>xxx</td></tr>_x000D_
</tbody>_x000D_
</table>
_x000D_
You can play with that here (on jsFiddle)
Firstly, you'll need more than just -moz-border-radius
if you want to support all browsers. You should specify all variants, including plain border-radius
, as follows:
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
Secondly, to directly answer your question, border-radius
doesn't actually display a border; it just sets how the corners look of the border, if there is one.
To turn on the border, and thus get your rounded corners, you also need the border
attribute on your td
and th
elements.
td, th {
border:solid black 1px;
}
You will also see the rounded corners if you have a background colour (or graphic), although of course it would need to be a different background colour to the surrounding element in order for the rounded corners to be visible without a border.
It's worth noting that some older browsers don't like putting border-radius
on tables/table cells. It may be worth putting a <div>
inside each cell and styling that instead. However this shouldn't affect current versions of any browsers (except IE, that doesn't support rounded corners at all - see below)
Finally, not that IE doesn't support border-radius
at all (IE9 beta does, but most IE users will be on IE8 or less). If you want to hack IE to support border-radius, look at http://css3pie.com/
[EDIT]
Okay, this was bugging me, so I've done some testing.
Here's a JSFiddle example I've been playing with
It seems like the critical thing you were missing was border-collapse:separate;
on the table element. This stops the cells from linking their borders together, which allows them to pick up the border radius.
Hope that helps.
Sample HTML
<table class="round-corner" aria-describedby="caption">
<caption id="caption">Table with rounded corners</caption>
<thead>
<tr>
<th scope="col">Head1</th>
<th scope="col">Head2</th>
<th scope="col">Head3</th>
</tr>
</thead>
<tbody>
<tr>
<td scope="rowgroup">tbody1 row1</td>
<td scope="rowgroup">tbody1 row1</td>
<td scope="rowgroup">tbody1 row1</td>
</tr>
<tr>
<td scope="rowgroup">tbody1 row2</td>
<td scope="rowgroup">tbody1 row2</td>
<td scope="rowgroup">tbody1 row2</td>
</tr>
</tbody>
<tbody>
<tr>
<td scope="rowgroup">tbody2 row1</td>
<td scope="rowgroup">tbody2 row1</td>
<td scope="rowgroup">tbody2 row1</td>
</tr>
<tr>
<td scope="rowgroup">tbody2 row2</td>
<td scope="rowgroup">tbody2 row2</td>
<td scope="rowgroup">tbody2 row2</td>
</tr>
</tbody>
<tbody>
<tr>
<td scope="rowgroup">tbody3 row1</td>
<td scope="rowgroup">tbody3 row1</td>
<td scope="rowgroup">tbody3 row1</td>
</tr>
<tr>
<td scope="rowgroup">tbody3 row2</td>
<td scope="rowgroup">tbody3 row2</td>
<td scope="rowgroup">tbody3 row2</td>
</tr>
</tbody>
<tfoot>
<tr>
<td scope="col">Foot</td>
<td scope="col">Foot</td>
<td scope="col">Foot</td>
</tr>
</tfoot>
</table>
SCSS, easily converted to CSS, use sassmeister.com
// General CSS
table,
th,
td {
border: 1px solid #000;
padding: 8px 12px;
}
.round-corner {
border-collapse: collapse;
border-style: hidden;
box-shadow: 0 0 0 1px #000; // fake "border"
border-radius: 4px;
// Maybe there's no THEAD after the caption?
caption + tbody {
tr:first-child {
td:first-child,
th:first-child {
border-top-left-radius: 4px;
}
td:last-child,
th:last-child {
border-top-right-radius: 4px;
border-right: none;
}
}
}
tbody:first-child {
tr:first-child {
td:first-child,
th:first-child {
border-top-left-radius: 4px;
}
td:last-child,
th:last-child {
border-top-right-radius: 4px;
border-right: none;
}
}
}
tbody:last-child {
tr:last-child {
td:first-child,
th:first-child {
border-bottom-left-radius: 4px;
}
td:last-child,
th:last-child {
border-bottom-right-radius: 4px;
border-right: none;
}
}
}
thead {
tr:last-child {
td:first-child,
th:first-child {
border-top-left-radius: 4px;
}
td:last-child,
th:last-child {
border-top-right-radius: 4px;
border-right: none;
}
}
}
tfoot {
tr:last-child {
td:first-child,
th:first-child {
border-bottom-left-radius: 4px;
}
td:last-child,
th:last-child {
border-bottom-right-radius: 4px;
border-right: none;
}
}
}
// Reset tables inside table
table tr th,
table tr td {
border-radius: 0;
}
}
Add a <div>
wrapper around the table, and apply the following CSS
border-radius: x px;
overflow: hidden;
display: inline-block;
to this wrapper.
You can try this if you want the rounded corners on each side of the table without touching the cells : http://jsfiddle.net/7veZQ/3983/
<table>
<tr class="first-line"><td>A</td><td>B</td></tr>
<tr class="last-line"><td>C</td><td>D</td></tr>
</table>
It is a little rough, but here is something I put together that is comprised entirely of CSS and HTML.
This example also makes use of the :hover
pseudo class for each data cell <td>
. Elements can be easily updated to meet your needs, and the hover can quickly be disabled.
(However, I have not yet gotten the :hover
to properly work for full rows <tr>
. The last hovered row does not display with rounded corners on the bottom. I'm sure there is something simple that is getting overlooked.)
table.dltrc {_x000D_
width: 95%;_x000D_
border-collapse: separate;_x000D_
border-spacing: 0px;_x000D_
border: solid black 2px;_x000D_
border-radius: 8px;_x000D_
}_x000D_
_x000D_
tr.dlheader {_x000D_
text-align: center;_x000D_
font-weight: bold;_x000D_
border-left: solid black 1px;_x000D_
padding: 2px_x000D_
}_x000D_
_x000D_
td.dlheader {_x000D_
background: #d9d9d9;_x000D_
text-align: center;_x000D_
font-weight: bold;_x000D_
border-left: solid black 1px;_x000D_
border-radius: 0px;_x000D_
padding: 2px_x000D_
}_x000D_
_x000D_
tr.dlinfo,_x000D_
td.dlinfo {_x000D_
text-align: center;_x000D_
border-left: solid black 1px;_x000D_
border-top: solid black 1px;_x000D_
padding: 2px_x000D_
}_x000D_
_x000D_
td.dlinfo:first-child,_x000D_
td.dlheader:first-child {_x000D_
border-left: none;_x000D_
}_x000D_
_x000D_
td.dlheader:first-child {_x000D_
border-radius: 5px 0 0 0;_x000D_
}_x000D_
_x000D_
td.dlheader:last-child {_x000D_
border-radius: 0 5px 0 0;_x000D_
}_x000D_
_x000D_
_x000D_
/*===== hover effects =====*/_x000D_
_x000D_
_x000D_
/*tr.hover01:hover,_x000D_
tr.hover02:hover {_x000D_
background-color: #dde6ee;_x000D_
}*/_x000D_
_x000D_
_x000D_
/* === ROW HOVER === */_x000D_
_x000D_
_x000D_
/*tr.hover02:hover:last-child {_x000D_
background-color: #dde6ee;_x000D_
border-radius: 0 0 6px 6px;_x000D_
}*/_x000D_
_x000D_
_x000D_
/* === CELL HOVER === */_x000D_
_x000D_
td.hover01:hover {_x000D_
background-color: #dde6ee;_x000D_
}_x000D_
_x000D_
td.hover02:hover {_x000D_
background-color: #dde6ee;_x000D_
}_x000D_
_x000D_
td.hover02:first-child {_x000D_
border-radius: 0 0 0 6px;_x000D_
}_x000D_
_x000D_
td.hover02:last-child {_x000D_
border-radius: 0 0 6px 0;_x000D_
}
_x000D_
<body style="background:white">_x000D_
<br>_x000D_
<center>_x000D_
<table class="dltrc" style="background:none">_x000D_
<tbody>_x000D_
<tr class="dlheader">_x000D_
<td class="dlheader">Subject</td>_x000D_
<td class="dlheader">Title</td>_x000D_
<td class="dlheader">Format</td>_x000D_
</tr>_x000D_
<tr class="dlinfo hover01">_x000D_
<td class="dlinfo hover01">One</td>_x000D_
<td class="dlinfo hover01">Two</td>_x000D_
<td class="dlinfo hover01">Three</td>_x000D_
</tr>_x000D_
<tr class="dlinfo hover01">_x000D_
<td class="dlinfo hover01">Four</td>_x000D_
<td class="dlinfo hover01">Five</td>_x000D_
<td class="dlinfo hover01">Six</td>_x000D_
</tr>_x000D_
<tr class="dlinfo hover01">_x000D_
<td class="dlinfo hover01">Seven</td>_x000D_
<td class="dlinfo hover01">Eight</td>_x000D_
<td class="dlinfo hover01">Nine</td>_x000D_
</tr>_x000D_
<tr class="dlinfo2 hover02">_x000D_
<td class="dlinfo hover02">Ten</td>_x000D_
<td class="dlinfo hover01">Eleven</td>_x000D_
<td class="dlinfo hover02">Twelve</td>_x000D_
</tr>_x000D_
</tbody>_x000D_
</table>_x000D_
</center>_x000D_
</body>
_x000D_
Source: Stackoverflow.com