我试图使用CSS border-radius属性制作一个圆角表。我使用的表格样式是这样的:

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

问题来了。我还想设置border-collapse:collapse属性,当设置border-radius时,它不再起作用。是否有一种基于css的方式,我可以得到相同的效果边界崩溃:崩溃而不实际使用它?

似乎很大一部分问题是,将表格设置为圆角并不影响角td元素的角。如果表格都是一种颜色,这就不是问题,因为我可以让顶部和底部td角分别为第一行和最后一行圆角。但是,我使用了不同的背景色来区分标题和条纹,所以内部td元素也会显示它们的圆角。

用另一个圆角元素包围桌子是行不通的,因为桌子的方角“渗透进来”。

将边框宽度指定为0不会折叠表格。

在将cellspacing设置为零后,底部td角仍然是正方形。

这些表是用PHP生成的,所以我可以对每个外部th/tds应用不同的类,并分别设置每个角的样式。我宁愿不这样做,因为它不是很优雅,而且应用于多个表有点痛苦,所以请继续提出建议。

我希望不使用JavaScript。


当前回答

正如Ian所说,解决方案是将表嵌套在一个div中,并像这样设置:

.table_wrapper {
  border-radius: 5px;
  overflow: hidden;
}

使用overflow:隐藏,方角不会溢出div。

其他回答

正如Ian所说,解决方案是将表嵌套在一个div中,并像这样设置:

.table_wrapper {
  border-radius: 5px;
  overflow: hidden;
}

使用overflow:隐藏,方角不会溢出div。

到目前为止,最好的解决方案来自你自己的解决方案,它是这样的:

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>

给出的答案只有在没有国界的情况下才有效,这是非常有限的!

我在SASS中有一个宏来做这件事,它完全支持外部和内部边界,实现了与border-collapse相同的样式:在没有实际指定的情况下崩溃。

在FF/IE8/Safari/Chrome测试。

在所有浏览器中提供漂亮的纯CSS圆形边界,但IE8(优雅地退化),因为IE8不支持边界半径:(

一些较老的浏览器可能需要供应商前缀来使用border-radius,因此可以根据需要将这些前缀添加到代码中。

这个答案不是最短的,但很管用。

.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;
}

要应用此样式,只需更改您的

<table>

标签如下:

<table class="roundedTable">

并确保在你的HTML中包含上述CSS样式。

希望这能有所帮助。

我是新的HTML和CSS,我也在寻找这个解决方案,在这里我发现了什么。

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
}

我试了一下,猜猜效果如何:)

我开始实验“显示”,我发现:边界半径,边界,边距,填充,在一个表中显示:

display: inline-table;

例如

table tbody tr {
  display: inline-table;
  width: 960px; 
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}

但是我们需要为每一列设置一个宽度

tr td.first-column {
  width: 100px;
}
tr td.second-column {
  width: 860px;
}