表格的边框半径效果不如预期。

101

我想在整个表格周围添加圆角边框。但以下代码在最新版本的Firefox和Google Chrome中均无法正常工作。

table {
  border-spacing: 0;
  width: 600px;
  margin: 30px;
  border: 1px solid #CCCCCC;
  border-radius: 6px 6px 6px 6px;
  -moz-border-radius: 6px 6px 6px 6px;
  -webkit-border-radius: 6px 6px 6px 6px;
  box-shadow: 0 1px 1px #CCCCCC;
}

table th:first-child {
  border-radius: 6px 0 0 0;
  -moz-border-radius: 6px 0 0 0;
  -webkit-border-radius: 6px 0 0 0;
}

table th:last-child {
  border-radius: 0 6px 0 0;
  -moz-border-radius: 0 6px 0 0;
  -webkit-border-radius: 0 6px 0 0;
}

table td:first-child,
.bordered th:first-child {
  border-left: medium none;
}

table th {
  background-color: #DCE9F9;
  background-image: -moz-linear-gradient(center top, #F8F8F8, #ECECEC);
  background-image: -webkit-gradient(linear, 0 0, 0 bottom, from(#F8F8F8), to(#ECECEC), color-stop(.4, #F8F8F8));
  border-top: medium none;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.8) inset;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
}

table td,
table th {
  border-left: 1px solid #CCCCCC;
  border-top: 1px solid #CCCCCC;
  padding: 10px;
  text-align: left;
}
<table class="bordered">
  <thead>
    <tr>
      <th><label>Labels</label></th>
      <th><label>Labels</label></th>
      <th><label>Labels</label></th>
      <th><label>Labels</label></th>
      <th><label>Labels</label></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><label>Value</label></td>
      <td><label>Value</label></td>
      <td><label>Value</label></td>
      <td><label>Value</label></td>
      <td><label>Value</label></td>
    </tr>
  </tbody>
</table>

JSFiddle

9个回答

137

border-collapse: separate !important;起作用了。

谢谢。

HTML

<table class="bordered">
    <thead>
        <tr>
            <th><label>Labels</label></th>
            <th><label>Labels</label></th>
            <th><label>Labels</label></th>
            <th><label>Labels</label></th>
            <th><label>Labels</label></th>
        </tr>
    </thead>

    <tbody>
        <tr>
            <td><label>Value</label></td>
            <td><label>Value</label></td>
            <td><label>Value</label></td>
            <td><label>Value</label></td>
            <td><label>Value</label></td>                            
        </tr>
    </tbody>                    
</table>

CSS

table {
    border-collapse: separate !important;
    border-spacing: 0;
    width: 600px;
    margin: 30px;
}
.bordered {
    border: solid #ccc 1px;
    -moz-border-radius: 6px;
    -webkit-border-radius: 6px;
    border-radius: 6px;
    -webkit-box-shadow: 0 1px 1px #ccc;
    -moz-box-shadow: 0 1px 1px #ccc;
    box-shadow: 0 1px 1px #ccc;
}
.bordered tr:hover {
    background: #ECECEC;    
    -webkit-transition: all 0.1s ease-in-out;
    -moz-transition: all 0.1s ease-in-out;
    transition: all 0.1s ease-in-out;
}
.bordered td, .bordered th {
    border-left: 1px solid #ccc;
    border-top: 1px solid #ccc;
    padding: 10px;
    text-align: left;
}
.bordered th {
    background-color: #ECECEC;
    background-image: -webkit-gradient(linear, left top, left bottom, from(#F8F8F8), to(#ECECEC));
    background-image: -webkit-linear-gradient(top, #F8F8F8, #ECECEC);
    background-image: -moz-linear-gradient(top, #F8F8F8, #ECECEC);    
    background-image: linear-gradient(top, #F8F8F8, #ECECEC);
    -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
    -moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset;
    box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
    border-top: none;
    text-shadow: 0 1px 0 rgba(255,255,255,.5);
}
.bordered td:first-child, .bordered th:first-child {
    border-left: none;
}
.bordered th:first-child {
    -moz-border-radius: 6px 0 0 0;
    -webkit-border-radius: 6px 0 0 0;
    border-radius: 6px 0 0 0;
}
.bordered th:last-child {
    -moz-border-radius: 0 6px 0 0;
    -webkit-border-radius: 0 6px 0 0;
    border-radius: 0 6px 0 0;
}
.bordered th:only-child{
    -moz-border-radius: 6px 6px 0 0;
    -webkit-border-radius: 6px 6px 0 0;
    border-radius: 6px 6px 0 0;
}
.bordered 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;
}
.bordered tr:last-child td:last-child {
    -moz-border-radius: 0 0 6px 0;
    -webkit-border-radius: 0 0 6px 0;
    border-radius: 0 0 6px 0;
} 

jsFiddle


2
border-collapse: separate !important; 这个对我也有效。 - Bipon Biswas

57

它能够工作,但是使用的工具存在问题:jsFiddle标准化 CSS 隐藏了浏览器默认设置导致问题...
请参见http://jsfiddle.net/XvdX9/5/

编辑:
来自 jsFiddle 的 normalize.css 样式表将 border-collapse:collapse 指令添加到所有表格中,并在 CSS2.1 中完全不同地呈现它们:

这两种模型之间的区别可以在此其他 fiddle 中看到:http://jsfiddle.net/XvdX9/11/ (带有一些单元格的透明度和顶部左侧单元格上的巨大边界半径,以查看表格与其单元格之间发生了什么)。

在关于 HTML 表格的同样的 CSS2.1 页面中,还有有关在分隔边框模型中浏览器应该/可以如何处理空单元格的解释,折叠边框模型中 border-style: noneborder-style: hidden 之间的区别,如何计算宽度以及如果表格、行和单元格元素在同一边框上定义了3个不同的样式,则应显示哪个边框。


你能解释一下为什么标准化(normalize)会导致这个问题吗?当我检查fiddle中的元素时,我找不到任何可能导致border-radius失效的东西。 - Fresheyeball
谢谢。我也找到了以下解决方案。 :) - Tapas Bose
@Fresheyeball 我添加了文档和比较的链接。我很高兴几年前直接学习了CSS,而不是在表格布局被(将要)滥用以及使用spacer.gif的时候在该领域工作。 - FelipeAls
7
如果您希望 border-radius 生效,应将 overflow 设为 hidden - Boris D. Teoharov
@dudelgrincen 它将在单元格上“剪切”矩形背景,类似于蒙版,如果您确定没有任何内容需要显示在表格外面,这是一个好主意(比四个角落单元格的边框半径更简单)。但是我经常有工具提示甚至在复杂的表格中有下拉菜单(过滤器等)。 - FelipeAls
1
@BorisD.Teoharov 这应该包含在答案中。谢谢。 - Dylan Kerler

34
这是我使用包装器的解决方案,仅仅移除 border-collapse 可能不总是有用的,因为您可能希望拥有边框。

这是我的解决方案,使用了包装器,在不总是移除border-collapse会有所帮助的情况下,因为您可能需要边框。

.wrapper {
  overflow: auto;
  border-radius: 6px;
  border: 1px solid red;
}

table {
  border-spacing: 0;
  border-collapse: collapse;
  border-style: hidden;

  width:100%;
  max-width: 100%;
}

th, td {
  padding: 10px;
  border: 1px solid #CCCCCC;
}
<div class="wrapper">
  <table>
    <thead>
      <tr>
        <th>Column 1</th>
        <th>Column 2</th>
        <th>Column 3</th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <td>Foo Bar boo</td>
        <td>Lipsum</td>
        <td>Beehuum Doh</td>
      </tr>
      <tr>
        <td>Dolor sit</td>
        <td>ahmad</td>
        <td>Polymorphism</td>
      </tr>
      <tr>
        <td>Kerbalium</td>
        <td>Caton, gookame kyak</td>
        <td>Corona Premium Beer</td>
      </tr>
    </tbody>
  </table>  
</div>

这篇文章有所帮助:https://css-tricks.com/table-borders-inside/


我不得不在DIV和TABLE上都添加border-radius: 6px;,因为我不想出于其他原因在DIV上使用overflow: auto样式。在TABLE和DIV上使用border-radius可以避免使用overflow: auto - Roland Pihlakas
这是最好的解决方案,对我来说完美地运作了。 - James Stewart

13

只需在应用了border-radius的表格中添加overflow:hidden即可。

.tablewithradius {
  overflow:hidden ;
  border-radius: 15px;
}

4
这个方法可以起作用,但是在我的电脑上会出现一个视觉上的问题,边框会溢出到角落处。 - Johan Aspeling

9

针对这个旧问题的说明:

我的 reset.css 文件设置了 border-spacing: 0,导致圆角被切掉。我需要将其设置为 3px 才能使半径正常工作(值将取决于所涉及的半径大小)。


7
<div class="leads-search-table">
                    <div class="row col-md-6 col-md-offset-2 custyle">
                    <table class="table custab bordered">
                    <thead>

                        <tr>
                            <th>ID</th>
                            <th>Title</th>
                            <th>Parent ID</th>
                            <th class="text-center">Action</th>
                        </tr>
                    </thead>
                            <tr>
                                <td>1</td>
                                <td>News</td>
                                <td>News Cate</td>
                                <td class="text-center"><a class='btn btn-info btn-xs' href="#"><span class="glyphicon glyphicon-edit"></span> Edit</a> <a href="#" class="btn btn-danger btn-xs"><span class="glyphicon glyphicon-remove"></span> Del</a></td>
                            </tr>
                            <tr>
                                <td>2</td>
                                <td>Products</td>
                                <td>Main Products</td>
                                <td class="text-center"><a class='btn btn-info btn-xs' href="#"><span class="glyphicon glyphicon-edit"></span> Edit</a> <a href="#" class="btn btn-danger btn-xs"><span class="glyphicon glyphicon-remove"></span> Del</a></td>
                            </tr>
                            <tr>
                                <td>3</td>
                                <td>Blogs</td>
                                <td>Parent Blogs</td>
                                <td class="text-center"><a class='btn btn-info btn-xs' href="#"><span class="glyphicon glyphicon-edit"></span> Edit</a> <a href="#" class="btn btn-danger btn-xs"><span class="glyphicon glyphicon-remove"></span> Del</a></td>
                            </tr>
                    </table>
                    </div>
                </div>

Css

.custab{
    border: 1px solid #ccc;
    margin: 5% 0;
    transition: 0.5s;
    background-color: #fff;
    -webkit-border-radius:4px;
    border-radius: 4px;
    border-collapse: separate;
}

这个答案中缺少DIV的CSS。 - Roland Pihlakas

5

不用担心,只需添加 overflow: hidden; 然后应用圆角边框。圆角边框将应用于所有四个边。


1
欢迎来到Stackoverflow!请花些时间阅读SO针对新用户的建议。您可以参观一下导览,然后仔细阅读这里的主题范围 - Sourcerer
谢谢回答。但是我对CSS有点不了解。我知道基础知识。但是我不知道为什么我们应该应用 overflow: "hidden"。你能说一下原因吗? - Kasir Barati

2

要使用边框半径,我在表格中设置了20px的边框半径,并将边框半径应用于表头的第一个子元素 (th) 和最后一个子元素。

table {
  border-collapse: collapse;
  border-radius:20px;
  padding: 10px;
}

table th:first-child {
  /* border-radius = top left, top right, bottom right, bottom left */
    border-radius: 20px 0 0 0; /* curves the top left */
    padding-left: 15px;
}

table th:last-child {
    border-radius: 0 20px 0 0; /* curves the top right */
}

然而,如果使用表格数据(td)进行操作,则此方法将无法奏效,因为它会在每行表格上添加曲线。如果您的表格中仅有2行,则这不是问题,但任何额外的行都会在内部行上添加曲线。 您只想在表格的外部看到这些曲线。 因此,为最后一行添加一个id。 然后,您可以将曲线应用于它们。

/* curves the first tableData in the last row */

#lastRow td:first-child {
    border-radius: 0 0 0 20px; /* bottom left curve */
}

/* curves the last tableData in the last row */

#lastRow td:last-child {
    border-radius: 0 0 20px 0; /* bottom right curve */
}

0
只需添加这个:
table {
   border-radius: 8px;
   border-collapse: separate;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接