Solving table rounded corner CSS - html

I have this CSS rule for rounded corner:
th, td { padding: 8px;
background: #E8ECE0;
text-align: center;
border: 1px solid #444;
border-bottom-width: 0px;
}
thead { background-color: #446bb3 ; color :#fff; padding:4px; line-height:30px }
tbody tr:nth-child(even) {background: #F6F6EC;}
tbody tr:nth-child(odd) {background: #FFF}
tr:first-child td, tr:first-child th {
border-top-left-radius: 12px; border-top-right-radius: 12px;
}
tr:last-child td {
border-bottom: 1px solid #444;
border-bottom-left-radius: 12px; border-bottom-right-radius: 12px;
}
table { border-spacing: 0; border: 0; margin:0px; width:100%; padding:5px}
td.pd {border-bottom-left-radius: 12px; border-bottom-right-radius: 12px;}
td.pu {border-top-left-radius: 12px; border-top-right-radius: 12px;}
My html table is:
<table >
<tbody>
<tr >
<td >Hello</td><td >Hello</td>
</tr>
<tr >
<td >Hello</td><td >Hello</td>
</tr>
<tr >
<td >Hello</td><td >Hello</td>
</tr>
<tr >
<td >Hello</td><td >Hello</td>
</tr>
</tbody>
</table>
which gives me this
How do I fix this problem, as the td elements within the table and in the middle of the table have rounded corners too? I only need the first row and last row to have rounded corners.

Assuming your table's html resembles the following:
<table>
<thead>
<tr>
<th>First column</th>
<th>Second column</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row one, cell one</td>
<td>Row one, cell two</td>
</tr>
<tr>
<td>Row two, cell one</td>
<td>Row two, cell two</td>
</tr>
<tr>
<td>Row three, cell one</td>
<td>Row four, cell two</td>
</tr>
</tbody>
</table>
The the following CSS should work:
table {
border-spacing: 0;
}
th, td {
border: 1px solid #000;
padding: 0.5em 1em;
}
/* the first 'th' within the first 'tr' of the 'thead': */
thead tr:first-child th:first-child {
border-radius: 0.6em 0 0 0;
}
/* the last 'th' within the first 'tr' of the 'thead': */
thead tr:first-child th:last-child {
border-radius: 0 0.6em 0 0;
}
/* the first 'td' within the last 'tr' of the 'tbody': */
tbody tr:last-child td:first-child {
border-radius: 0 0 0 0.6em;
}
/* the last 'td' within the last 'tr' of the 'tbody': */
tbody tr:last-child td:last-child {
border-radius: 0 0 0.6em 0;
}
JS Fiddle demo.
Edited in response to question from OP:
the border within the table is a little think, how do i make it 1px
The borders between cells are a little thick, because the left border of one cell is against the right border of the previous cells (and the same for top/bottom borders).
One way to remove that effect is to specify border-collapse: collapse; on the table element. Unfortunately the effect of this is to also remove/unset/override the border-radius declarations: demo.
The more complicated way is to manually remove top-borders for rows with a previous row, and the left-border of a cell that follows a cell, adding the following to the previous CSS:
thead + tbody tr td,
tr + tr td {
border-top: 0;
}
tr th + th,
tr td + td {
border-left: 0;
}
JS Fiddle demo.
Edited to reduce make the CSS more durable (for single-cell rows, for the addition of a tfoot or the removal of the thead):
table {
border-spacing: 0;
}
th, td {
border: 1px solid #000;
padding: 0.5em 1em;
}
thead tr:first-child th:first-child,
tbody:first-child tr:first-child td:first-child {
border-top-left-radius: 0.6em;
}
thead tr:first-child th:last-child,
tbody:first-child tr:first-child td:last-child {
border-top-right-radius: 0.6em;
}
thead + tbody tr:last-child td:first-child,
tfoot tr:last-child td:first-child {
border-bottom-left-radius: 0.6em;
}
thead + tbody tr:last-child td:last-child,
tfoot tr:last-child td:last-child {
border-bottom-right-radius: 0.6em;
}
thead + tbody tr td,
tfoot + tbody tr td,
tfoot tr td,
tbody + tbody tr td,
tr + tr td {
border-top: 0;
}
tr th + th,
tr td + td {
border-left: 0;
}
JS Fiddle demo.
There is a problem with multiple tbody elements, in the absence of a tfoot element, currently in which the first tbody will retain the border-radius on their lower borders.

You can just put table into div. Styles for div (example):
div {
border-radius: 4px;
-moz-border-radius: 4px
-webkit-border-radius: 4px;
overflow: hidden; /*notice*/
}
So the table corners will be hidden.

This answer doesn't answer your question directly, but a variant.
If you dont want the middle columns to have rounded corners, this is a possible solution:
Illustration:
Properties for the table row (tr): update the table data (td) for the left most column:
tbody tr td:first-child
{
border-radius: 0.6em 0 0 0.6em;
}
Properties for the table row (tr): update the table data (td) for the second column:
tbody td:nth-child(2)
{
border-radius: 0 0.6em 0.6em 0;
}
Here is an example: JS Fiddle demo
If you have more than one column (td or th) you simply add something like this:
tbody td:nth-child(2) /* This is now the middle element out of three */
{
border-radius: 0 0 0 0;
}
tbody td:nth-child(3) /* This is now the right most column */
{
boder-radius: 0 0.6em 0.6em 0;
}

You can reset the border radius of the td element. That should solve it.

You can give id to the td elements and using the id's of td elements set the border radius to 0px.

You should try a
clear:both;
and it will be reset.
Also you can try !important because maybe you forget other "inline css rules" in other html codes.

Though this is an old answer, I'd like to enhance it by adding my findings. In addition to David Thomas's super-smart answer, I found an edge case where it doesn't exactly fit: A single-cell row! for example:
<table>
<tr><th colspan="3">My header</th></tr>
<tr><td>row1-cell1</td><td>row1-cell2</td><td>row1-cell3</td></tr>
<tr><td>row2-cell1</td><td>row2-cell2</td><td>row2-cell3</td></tr>
<tr><th colspan="3">My footer</th></tr>
</table>
The rule for the top-right corner would overwrite the rule for the top-left corner (because it comes after it), and the rule for the bottom-right corner would overwrite the rule for the bottom left corner (for the same reason). See screen shot below:
The remedy for that is the css below (I added more selectors for various table-tr, tbody-tr, thead-tr combinations as needed, so you can also expand it to fit your markup):
table td,
table th{
border: 1px solid #666;
}
table{
width: 98%;
border-spacing: 0px;
}
/* alternating table row colors*/
tr:nth-child(even) { background-color:#f6f6f6; }
tr:nth-child(odd) { background-color:#ffffff; }
/* set all corner radii initially to zero */
th, td {
border-radius: 0;
}
/*set only specific radii per corner (don't use the border-radius shorthand)*/
thead tr:first-child th:first-child,
table tr:first-child td:first-child,
tbody tr:first-child th:first-child {
border-top-left-radius: 0.6em;
}
thead tr:first-child th:last-child,
table tr:first-child td:last-child,
tbody tr:first-child th:last-child {
border-top-right-radius: 0.6em;
}
tbody tr:last-child td:first-child,
table tr:last-child td:first-child,
tbody tr:last-child th:first-child {
border-bottom-left-radius: 0.6em;
}
tbody tr:last-child td:last-child,
table tr:last-child td:last-child,
tbody tr:last-child th:last-child {
border-bottom-right-radius: 0.6em;
}
thead + tbody tr td,
tr + tr td ,
tr + tr th {
border-top: 0;
}
tr th + th,
tr td + td {
border-left: 0;
}
/* shade th cells */
table th {
background-color: #888;
color: #FFF;
}
This results in the screenshot below, as desired:
All credit for this solution still goes to David Thomas, especially for the adjacent cells border trick!

Related

CSS: Why does an unreferenced class get priority? [duplicate]

This question already has answers here:
What do commas and spaces in multiple classes mean in CSS?
(9 answers)
Closed 11 months ago.
What I want
I want to reduce the .table padding for my th and td's to 5px.
Issues
I have referenced a child class .information-table but .data-table seems to be taking priority even though I have not referenced it.
https://jsfiddle.net/kcfyjdr2/32/
.table {
width:100%;
border-collapse: collapse;
border-spacing: 0px;
border: 1px solid #ddd;
}
.table th, td {
text-align: left;
padding: 12px;
}
.table .information-table th, td {
padding: 5px;
}
.table .data-table th, td {
padding: 10px;
}
<table class="table information-table">
<tbody>
<tr>
<td><b>Test Program:</b></td>
<td><b>Report Name:</b></td>
<td><b>Location of Test:</b></td>
</tr>
</tbody>
</table>
How can I fix this and please could I get an explanation?
it's because your selector is
.table .data-table th, td
the issue come from , td it will take each td use in your html
to fix use the selector
.table.data-table th, .table.data-table th td
moreover your selector for information table must be
.table.information-table th, .table.information-table td {
.table {
width:100%;
border-collapse: collapse;
border-spacing: 0px;
border: 1px solid #ddd;
}
.table th, td {
text-align: left;
padding: 12px;
}
.table.information-table th, .table.information-table td {
padding: 5px;
color: blue;
}
.table.data-table th, .table.data-table th td {
padding: 10px;
color: red;
}
<table class="table information-table">
<tbody>
<tr>
<td><b>Test Program:</b></td>
<td><b>Report Name:</b></td>
<td><b>Location of Test:</b></td>
</tr>
</tbody>
</table>

How can I round table borders without doubling borders or using border-collapse?

I've been trying to use a Stack Ooverflow answer I've found for rounding table's corners:
/*
table {
border-collapse: collapse;
}*/
table, tr, td, th{
border: 1px solid;
text-align: center;
/*padding: 10px;*/
}
table{
border-spacing: 0;
width: 100%;
display: table;
}
table tr:last-child td:first-child, tr:last-child, table {
border-bottom-left-radius: 25px;
}
table tr:last-child td:last-child, tr:last-child, table {
border-bottom-right-radius: 25px;
}
table tr:first-child th:first-child, tr:first-child, table {
border-top-left-radius: 25px;
}
table tr:first-child th:last-child, tr:first-child, table {
border-top-right-radius: 25px;
}
<table>
<tr>
<th>Num</th><th>Lett</th><th>Lat</th>
</tr>
<tr>
<td>1</td><td>A</td><td>I</td>
</tr>
<tr>
<td>2</td><td>B</td><td>II</td>
</tr>
<tr>
<td>3</td><td>C</td><td>III</td>
</tr>
</table>
This works fine. But if I uncomment border-collapse, the rounding disappears. If I uncomment padding: 10px, it would double the border:
I need the border-collapse to avoid bold inner borders. I need padding so that text in table cells had distance from its borders. How do I achieve it while having a rounded outer borders?
JSFiddle: https://jsfiddle.net/eszuhvxj/1/
you can remove top and right border on td:
table {
border-collapse: separate;
border:none;
border-spacing: 0;
width: 30em;
--radius-border: 15px;
}
table td, table th {
border: 1px solid;
text-align: center;
padding: 10px;
}
table td {
border-top: none;
}
table tr:last-child td:first-child{
border-bottom-left-radius: var(--radius-border);
}
table tr:last-child td:last-child {
border-bottom-right-radius: var(--radius-border);
}
table tr:first-child th:first-child {
border-top-left-radius: var(--radius-border);
}
table tr:first-child th:last-child {
border-top-right-radius: var(--radius-border);
}
table tr th:not(:last-child),
table tr td:not(:last-child) {
border-right: none;
}
<table>
<tr>
<th>Num</th><th>Lett</th><th>Lat</th>
</tr>
<tr>
<td>1</td><td>A</td><td>I</td>
</tr>
<tr>
<td>2</td><td>B</td><td>II</td>
</tr>
<tr>
<td>3</td><td>C</td><td>III</td>
</tr>
</table>
Here is the solution:
table {
border-spacing: 0;
width: 100%;
}
th, td {
border: 1px solid #000;
padding: 0.5em 1em;
text-align:center;
}
/* the first 'th' within the first 'tr' of the 'thead': */
tr:first-child th:first-child {
border-radius: 0.6em 0 0 0;
}
/* the last 'th' within the first 'tr' of the 'thead': */
tr:first-child th:last-child {
border-radius: 0 0.6em 0 0;
}
/* the first 'td' within the last 'tr' of the 'tbody': */
tr:last-child td:first-child {
border-radius: 0 0 0 0.6em;
}
/* the last 'td' within the last 'tr' of the 'tbody': */
tr:last-child td:last-child {
border-radius: 0 0 0.6em 0;
}
<table>
<tr>
<th>Num</th><th>Lett</th><th>Lat</th>
</tr>
<tr>
<td>1</td><td>A</td><td>I</td>
</tr>
<tr>
<td>2</td><td>B</td><td>II</td>
</tr>
<tr>
<td>3</td><td>C</td><td>III</td>
</tr>
</table>

trying to place a table inside a bootstrap row but the width of the table jumps across the row

I am trying to place a table inside a bootstrap row but the width of the table keeps jumping across the with of the table. I have added a maximum value of 50% to the width of the table but problem still persists.
this is the css of the table
**EDITTED*****
<div class="col-sm-10" style="min-height:280px">
<div class="panel panel-default">
<div class="panel-body">
<style type="text/css">
table {
max-width: 50%;
background-color: transparent;
border-collapse: collapse;
border-spacing: 0;
}
.table thead th {
vertical-align: bottom;
}
.table th {
font-weight: bold;
}
.table th, .table td {
padding: 8px;
line-height: 20px;
text-align: left;
vertical-align: top;
}
.table td {
border-top: 1px solid #dddddd;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
.table-striped tbody > tr:nth-child(odd) > td, .table-striped tbody > tr:nth-child(odd) > th {
background-color: #f9f9f9;
}
.table-bordered {
border: 1px solid #dddddd;
border-collapse: separate;
border-left: 0;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
.table-bordered th, .table-bordered td {
border-left: 1px solid #dddddd;
}
</style>
then in my view i have html implementation of the css code in the jsp page
<c:when test="${messageList != null}">
<c:forEach items="${messageList}" var="mesList">
<div class="row">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Message</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<tr>
<td>Mark</td>
<td>${mesList.message}</td>
<td>${mesList.timestamp}</td>
</tr>
</tbody>
</table>
</div>
</c:forEach>
</c:when>
</div></div></div>
Please how can I place the table into the div row.
The class .table is overriding the styles applied to the table tag. Apply your styles to the class .table rather than applying them to the tag table.
When dealing with stylesheet you always have to remember that they are cascading. The styles of a class will override the styles of the tag the class is applied to.
When your css has no effect it pays off to look in the developer tools of the browser. In Chrome, the styles that are being overridden will be marked out.

Table th border radius not rounding OR using border-collapse makes rest of table mess up

The border around my <th> element isn't rounding with the radius.
Here's my code: http://jsfiddle.net/5zUT8/
I can use border-collapse: separate; but that makes borders inside the table look thicker.
When I remove the 1px border it makes the table header look 1px smaller then the rest of the table.
Not sure what to do in order to get the border-radius working on the header without compromising the rest of the style.
Something like this? — http://jsfiddle.net/5zUT8/1/
I'm basically adding a 1px border to the right and bottom of each td, except for the first one in each tr, which includes a border to the left. This overcomes the 2 × 1px "thick" border you would see otherwise.
td {
border-color:#ccc;
border-style:solid;
border-width:0 1px 1px 0;
}
td:first-child {
border-width:0 1px 1px 1px;
}
table.border {
border-collapse: separate;
border-spacing: 0;
min-width: 350px;
}
table.border tr th,
table.border tr td {
padding: 0.25em 1em;
vertical-align: middle;
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
padding: 5px;
}
table.border tr th:first-child,
table.border tr td:first-child {
border-left: 1px solid #ccc;
}
table.border tr th {
background:#222a3d;
border: 1px solid #222a3d;
color: #bdccd3;
text-align: left;
}
/* top-left border-radius */
table.border tr:first-child th:first-child {
border-top-left-radius: 6px;
}
/* top-right border-radius */
table.border tr:first-child th:last-child {
border-top-right-radius: 6px;
}
/* bottom-left border-radius */
table.border tr:last-child td:first-child {
border-bottom-left-radius: 6px;
}
/* bottom-right border-radius */
table.border tr:last-child td:last-child {
border-bottom-right-radius: 6px;
}
<table class="border" cellspacing="0">
<thead>
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
<th>Heading 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Content Left</td>
<td>Content Middle</td>
<td>Content Right</td>
</tr>
<tr>
<td colspan="3" >Bottom Full Section</td>
</tr>
</tbody>

The border-radius property and border-collapse:collapse don't mix. How can I use border-radius to create a collapsed table with rounded corners?

I am trying to make a table with rounded corners using the CSS 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?
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.
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.
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.
I'd like to do this without JavaScript.
I 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:
table tr:last-child td:first-child {
border: 2px solid orange;
border-bottom-left-radius: 10px;
}
table tr:last-child td:last-child {
border: 2px solid green;
border-bottom-right-radius: 10px;
}
<table>
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>
Now everything rounds properly, except that there's still the issue of border-collapse: collapse breaking everything.
A workaround is to add border-spacing: 0 and leave the default border-collapse: separate on the table.
The following method works (tested in Chrome) by using a box-shadow with a spread of 1px instead of a "real" border.
table {
border-collapse: collapse;
border-radius: 30px;
border-style: hidden; /* hide standard table (collapsed) border */
box-shadow: 0 0 0 1px #666; /* this draws the table border */
}
td {
border: 1px solid #ccc;
}
<table>
<thead>
<tr>
<th>Foo</th>
<th>Bar</th>
</tr>
</thead>
<tbody>
<tr>
<td>Baz</td>
<td>Qux</td>
</tr>
<tr>
<td>Life is short</td>
<td rowspan="3">and</td>
</tr>
<tr>
<td>Love</td>
</tr>
<tr>
<td>is always over</td>
</tr>
<tr>
<td>In the</td>
<td>Morning</td>
</tr>
</tbody>
</table>
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 the border-spacing: 0 solution), I prefer to do the following:
Set a border-right and border-bottom for your table cells (td and th)
Give the cells in the first row a border-top
Give the cells in the first column a border-left
Using the first-child and last-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:
table {
border-collapse: separate;
border-spacing: 0;
min-width: 350px;
}
table tr th,
table tr td {
border-right: 1px solid #bbb;
border-bottom: 1px solid #bbb;
padding: 5px;
}
table tr th:first-child,
table tr td:first-child {
border-left: 1px solid #bbb;
}
table tr th:first-child,
table tr td:first-child {
border-left: 1px solid #bbb;
}
table tr th {
background: #eee;
text-align: left;
border-top: solid 1px #bbb;
}
/* top-left border-radius */
table tr:first-child th:first-child {
border-top-left-radius: 6px;
}
/* top-right border-radius */
table tr:first-child th:last-child {
border-top-right-radius: 6px;
}
/* bottom-left border-radius */
table tr:last-child td:first-child {
border-bottom-left-radius: 6px;
}
/* bottom-right border-radius */
table tr:last-child td:last-child {
border-bottom-right-radius: 6px;
}
<div>
<table>
<tr>
<th>item1</th>
<th>item2</th>
</tr>
<tr>
<td>item1</td>
<td>item2</td>
</tr>
<tr>
<td>item1</td>
<td>item2</td>
</tr>
<tr>
<td>item1</td>
<td>item2</td>
</tr>
</table>
</div>
Have you tried using table{border-spacing: 0} instead of table{border-collapse: collapse} ???
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 the value of border-collapse is collapse.
As Ian said, the solution is to nest the table inside a div and set it like that:
.table_wrapper {
border-radius: 5px;
overflow: hidden;
}
With overflow:hidden, the square corners won't bleed through the div.
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.
Here is a way:
div {
border: 2px solid red;
overflow: hidden;
border-radius: 14px;
transform: rotate(0deg);
}
table {
border-spacing: 0;
background-color: blue;
height: 100%;
width: 100%;
}
<div>
<table>
<tr>
<td><br></td>
</tr>
</table>
</div>
Or
div {
...
overflow: hidden;
border-radius: 14px;
position: relative;
z-index: 1;
}
Actually you can add your table inside a div as its wrapper. and then assign these CSS codes to wrapper:
.table-wrapper {
border: 1px solid #f00;
border-radius: 5px;
overflow: hidden;
}
table {
border-collapse: collapse;
}
I tried a workaround using the pseudo elements :before and :after on the thead th:first-child and thead th:last-child
In combination with wrapping the table with a <div class="radius borderCCC">
table thead th:first-child:before{
content:" ";
position:absolute;
top:-1px;
left:-1px;
width:15px;
height:15px;
border-left:1px solid #ccc;
border-top:1px solid #ccc;
-webkit-border-radius:5px 0px 0px;
}
table thead th:last-child:after{
content:" ";
position:absolute;
top:-1px;
right:-1px;
width:15px;
height:15px;
border-right:1px solid #ccc;
border-top:1px solid #ccc;
-webkit-border-radius:0px 5px 0px 0px;
}
see jsFiddle
Works for me in chrome (13.0.782.215) Let me know if this works for you in other browsers.
The given answers only work when there are no borders around the table, which is very limiting!
I have a macro in SASS to do this, which fully supports external and internal borders, achieving the same styling as border-collapse: collapse without actually specifying it.
Tested in FF/IE8/Safari/Chrome.
Gives nice rounded borders in pure CSS in all browsers but IE8 (degrades gracefully) since IE8 doesn't support border-radius :(
Some older browsers may require vendor prefixes to work with border-radius, so feel free to add those prefixes to your code as necessary.
This answer is not the shortest - but it works.
.roundedTable {
border-radius: 20px / 20px;
border: 1px solid #333333;
border-spacing: 0px;
}
.roundedTable th {
padding: 4px;
background: #ffcc11;
border-left: 1px solid #333333;
}
.roundedTable th:first-child {
border-left: none;
border-top-left-radius: 20px;
}
.roundedTable th:last-child {
border-top-right-radius: 20px;
}
.roundedTable tr td {
border: 1px solid #333333;
border-right: none;
border-bottom: none;
padding: 4px;
}
.roundedTable tr td:first-child {
border-left: none;
}
To apply this style simply change your
<table>
tag to the following:
<table class="roundedTable">
and be sure to include the above CSS styles in your HTML.
Hope this helps.
I just wrote a crazy set of CSS for this that seems to work perfectly:
table {
border-collapse: separate;
border-spacing: 0;
width: 100%;
}
table td,
table th {
border-right: 1px solid #CCC;
border-top: 1px solid #CCC;
padding: 3px 5px;
vertical-align: top;
}
table td:first-child,
table th:first-child {
border-left: 1px solid #CCC;
}
table tr:last-child td,
table tr:last-child th {
border-bottom: 1px solid #CCC;
}
table thead + tbody tr:first-child td {
border-top: 0;
}
table thead td,
table th {
background: #EDEDED;
}
/* complicated rounded table corners! */
table thead:first-child tr:last-child td:first-child {
border-bottom-left-radius: 0;
}
table thead:first-child tr:last-child td:last-child {
border-bottom-right-radius: 0;
}
table thead + tbody tr:first-child td:first-child {
border-top-left-radius: 0;
}
table thead + tbody tr:first-child td:last-child {
border-top-right-radius: 0;
}
table tr:first-child td:first-child,
table thead tr:first-child td:first-child {
border-top-left-radius: 5px;
}
table tr:first-child td:last-child,
table thead tr:first-child td:last-child {
border-top-right-radius: 5px;
}
table tr:last-child td:first-child,
table thead:last-child tr:last-child td:first-child {
border-bottom-left-radius: 5px;
}
table tr:last-child td:last-child,
table thead:last-child tr:last-child td:last-child {
border-bottom-right-radius: 5px;
}
<table>
<thead>
<tr>
<th>
Table Head
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
Table Data
</td>
</tr>
</tbody>
</table>
/* end complicated rounded table corners !*/
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>
I had the same problem.
remove border-collapse entirely and use:
cellspacing="0" cellpadding="0"
in the html document.
example:
<table class="top_container" align="center" cellspacing="0" cellpadding="0">
Solution with border-collapse:separate for table and display:inline-table for tbody and thead.
table {
width: 100%;
border-collapse: separate;
border-spacing: 0px;
background: transparent;
}
table thead {
display: inline-table;
width: 100%;
background: #fc0 url(../images/bg-heading.png) repeat-x 0% 0;
-webkit-border-top-left-radius: 7px;
-moz-border-radius-topleft: 7px;
-webkit-border-top-right-radius: 7px;
-moz-border-radius-topright: 7px;
border-radius: 7px 7px 0px 0px;
padding: 1px;
padding-bottom: 0;
}
table tbody {
border: 1px solid #ddd;
display: inline-table;
width: 100%;
border-top: none;
}
I am new with HTML and CSS and I was also looking for solution for this, here what I find.
table,th,td {
border: 1px solid black;
border-spacing: 0
}
/* add border-radius to table only*/
table {
border-radius: 25px
}
/* then add border-radius to top left border of left heading cell */
th:first-child {
border-radius: 25px 0 0 0
}
/* then add border-radius to top right border of right heading cell */
th:last-child {
border-radius: 0 25px 0 0
}
/* then add border-radius to bottom left border of left cell of last row */
tr:last-child td:first-child {
border-radius: 0 0 0 25px
}
/* then add border-radius to bottom right border of right cell of last row */
tr:last-child td:last-child {
border-radius: 0 0 25px 0
}
I try it, guess what it works :)
Found this answer after running into the same problem, but found it's pretty simple: just give the table overflow:hidden
No need for a wrapping element. Granted, I don't know if this would have worked 7 years ago when the question was initially asked, but it works now.
I started experiment with "display" and I found that: border-radius, border, margin, padding, in a table are displayed with:
display: inline-table;
For example
table tbody tr {
display: inline-table;
width: 960px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
But we need set a width of every column
tr td.first-column {
width: 100px;
}
tr td.second-column {
width: 860px;
}
Here is a recent example of how to implement a table with rounded-corners from http://medialoot.com/preview/css-ui-kit/demo.html. It's based on the special selectors suggested by Joel Potter above. As you can see, it also includes some magic to make IE a little happy. It includes some extra styles to alternate the color of the rows:
table-wrapper {
width: 460px;
background: #E0E0E0;
filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#E9E9E9', endColorstr='#D7D7D7');
background: -webkit-gradient(linear, left top, left bottom, from(#E9E9E9), to(#D7D7D7));
background: -moz-linear-gradient(top, #E9E9E9, #D7D7D7);
padding: 8px;
-webkit-box-shadow: inset 0px 2px 2px #B2B3B5, 0px 1px 0 #fff;
-moz-box-shadow: inset 0px 2px 2px #B2B3B5, 0px 1px 0 #fff;
-o-box-shadow: inset 0px 2px 2px #B2B3B5, 0px 1px 0 #fff;
-khtml-box-shadow: inset 0px 2px 2px #B2B3B5, 0px 1px 0 #fff;
box-shadow: inset 0px 2px 2px #B2B3B5, 0px 1px 0 #fff;
-webkit-border-radius: 10px;
/*-moz-border-radius: 10px; firefox doesn't allow rounding of tables yet*/
-o-border-radius: 10px;
-khtml-border-radius: 10px;
border-radius: 10px;
margin-bottom: 20px;
}
.table-wrapper table {
width: 460px;
}
.table-header {
height: 35px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
text-align: center;
line-height: 34px;
text-decoration: none;
font-weight: bold;
}
.table-row td {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
text-align: left;
text-decoration: none;
font-weight: normal;
color: #858585;
padding: 10px;
border-left: 1px solid #ccc;
-khtml-box-shadow: 0px 1px 0px #B2B3B5;
-webkit-box-shadow: 0px 1px 0px #B2B3B5;
-moz-box-shadow: 0px 1px 0px #ddd;
-o-box-shadow: 0px 1px 0px #B2B3B5;
box-shadow: 0px 1px 0px #B2B3B5;
}
tr th {
border-left: 1px solid #ccc;
}
tr th:first-child {
-khtml-border-top-left-radius: 8px;
-webkit-border-top-left-radius: 8px;
-o-border-top-left-radius: 8px;
/*-moz-border-radius-topleft: 8px; firefox doesn't allow rounding of tables yet*/
border-top-left-radius: 8px;
border: none;
}
tr td:first-child {
border: none;
}
tr th:last-child {
-khtml-border-top-right-radius: 8px;
-webkit-border-top-right-radius: 8px;
-o-border-top-right-radius: 8px;
/*-moz-border-radius-topright: 8px; firefox doesn't allow rounding of tables yet*/
border-top-right-radius: 8px;
}
tr {
background: #fff;
}
tr:nth-child(odd) {
background: #F3F3F3;
}
tr:nth-child(even) {
background: #fff;
}
tr:last-child td:first-child {
-khtml-border-bottom-left-radius: 8px;
-webkit-border-bottom-left-radius: 8px;
-o-border-bottom-left-radius: 8px;
/*-moz-border-radius-bottomleft: 8px; firefox doesn't allow rounding of tables yet*/
border-bottom-left-radius: 8px;
}
tr:last-child td:last-child {
-khtml-border-bottom-right-radius: 8px;
-webkit-border-bottom-right-radius: 8px;
-o-border-bottom-right-radius: 8px;
/*-moz-border-radius-bottomright: 8px; firefox doesn't allow rounding of tables yet*/
border-bottom-right-radius: 8px;
}
I see a lot of weird hacks and workarounds so I would like to suggest my solution for creating a table with border-radius and the same visual effect as border: collapse; by simply targeting nested rows and cells to turn borders off.
You can get more in-depth to suit your needs using other pseudo selectors like first-child, etc, but this is the minimal solution:
table {
width: 100%;
border-spacing: 0;
border-radius: 4px;
border: 1px solid #ccc;
}
th, td {
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
}
th:last-child, td:last-child {
border-right: none;
}
tr:last-child td {
border-bottom: none;
}
<table>
<thead>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
</tbody>
</table>
Here is a solution using SCSS. It creates a table with rounded corners and bordered cells.
This solution uses the approach from #Ramon Tayag. The key is to use border-spacing: 0, as he points out.
$line: 1px solid #979797;
$radius: 5px;
table {
border: $line;
border-radius: $radius;
border-spacing: 0;
th,
tr:not(:last-child) td {
border-bottom: $line;
}
th:not(:last-child),
td:not(:last-child) {
border-right: $line;
}
}
easiest way...
table {
border-collapse: inherit;
border: 1px solid black;
border-radius: 5px;
}
Some of the other answers are good, but none of them consider you using thead, tbody and tfoot. Or cases, when you can either combination of those. And when you apply them, you can get some unnecessary rounding or borders.
Thus I tried adjusting css-only answer from #NullUserException and this is what I got:
table {
border-radius: 5px;
border-width: 2px;
border-style: solid;
border-color: darkgreen;
border-spacing: 0;
border-collapse: separate;
width: 100%;
}
table tr td,
table tr th {
border-right-width: 2px;
border-right-style: solid;
border-right-color: darkgreen;
border-bottom-width: 2px;
border-bottom-style: solid;
border-bottom-color: darkgreen;
}
table tr th:last-child,
table tr td:last-child {
border-right-width: 2px;
border-right-style: none;
border-right-color: darkgreen;
}
table tr:last-child td,
table tr:last-child th {
border-bottom-width: 2px;
border-bottom-style: none;
border-bottom-color: darkgreen;
}
/* top-left border-radius */
table :not(tfoot) tr:first-child th:first-child,
table :not(tfoot) tr:first-child td:first-child {
border-top-left-radius: 5px;
border-bottom-left-radius: 0;
}
/* top-right border-radius */
table :not(tfoot) tr:first-child th:last-child,
table :not(tfoot) tr:first-child td:last-child {
border-top-right-radius: 5px;
border-bottom-right-radius: 0;
}
/* bottom-left border-radius */
table :not(thead) tr:last-child th:first-child,
table :not(thead) tr:last-child td:first-child {
border-bottom-left-radius: 5px;
}
/* bottom-right border-radius */
table :not(thead) tr:last-child th:last-child,
table :not(thead) tr:last-child td:last-child{
border-bottom-right-radius: 5px;
}
/*Handle thead and tfoot borders*/
table thead tr:first-child th,
table thead tr:first-child td {
border-top-style: none;
}
table thead tr:last-child th,
table thead tr:last-child td {
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: darkgreen;
}
table tfoot tr:last-child th,
table tfoot tr:last-child td {
border-bottom-style: none;
}
table tfoot tr:first-child th,
table tfoot tr:first-child td {
border-top-style: solid;
border-top-width: 2px;
border-top-color: darkgreen;
}
table tr:first-child th,
table tr:first-child td {
border-top-style: none;
}
darkgreen is used to clearly show that borders are correct everywhere across the whole table. Essentially, wherever you see darkgreen - is where you style the table's borders.
This codepen shows regular table and the one with thead, tbody and tfoot. CSS is identical to the one above with only the addition of style reset for th. At the moment of writing, you can see, that they both render identically.
I always do this way using Sass
table {
border-radius: 0.25rem;
thead tr:first-child th {
&:first-child {
border-top-left-radius: 0.25rem;
}
&:last-child {
border-top-right-radius: 0.25rem;
}
}
tbody tr:last-child td {
&:first-child {
border-bottom-left-radius: 0.25rem;
}
&:last-child {
border-bottom-right-radius: 0.25rem;
}
}
}
The best solution so far comes from your own solution and it goes like this:
table, tr, td, th{
border: 1px solid;
text-align: center;
}
table{
border-spacing: 0;
width: 100%;
display: table;
}
table tr:last-child td:first-child, tr:last-child, table {
border-bottom-left-radius: 25px;
}
table tr:last-child td:last-child, tr:last-child, table {
border-bottom-right-radius: 25px;
}
table tr:first-child th:first-child, tr:first-child, table {
border-top-left-radius: 25px;
}
table tr:first-child th:last-child, tr:first-child, table {
border-top-right-radius: 25px;
}
<table>
<tr>
<th>Num</th><th>Lett</th><th>Lat</th>
</tr>
<tr>
<td>1</td><td>A</td><td>I</td>
</tr>
<tr>
<td>2</td><td>B</td><td>II</td>
</tr>
<tr>
<td>3</td><td>C</td><td>III</td>
</tr>
</table>
table {
width: 200px;
text-align: center;
border-radius: 12px;
overflow: hidden;
}
table td {
border-width: 1px 0 0 1px;
}
table tr:first-child td {
border-top: none;
}
table tr td:first-child {
border-left: none;
}
div {
background-color: lime;
}
<table cellspacing="0" cellpadding="0" border="1">
<tr>
<td><div>1</div></td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
</table>
Use "overflow: hidden" with "border-radius"
This works with "collapse" table as well
Example:
border-radius: 1em;
overflow: hidden;