【问题标题】:Table's border-radius does not function as expected表格的边框半径没有按预期工作
【发布时间】:2012-05-26 20:25:30
【问题描述】:

我想在整个表格周围添加一个边框半径。但是以下代码在最新版本的 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

【问题讨论】:

    标签: html css html-table


    【解决方案1】:

    不用担心..只需添加 溢出:隐藏; 然后应用边界半径。边框半径将应用于所有四个边。

    【讨论】:

    【解决方案2】:

    只需将overflow:hiddenborder-radius 添加到表中即可。

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

    【讨论】:

    • 这行得通,但在边角处为我添加了一个视觉错误,边界溢出
    【解决方案3】:

    要使用边框半径,我在表格中设置了 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 */
    }
    

    【讨论】:

      【解决方案4】:

      这是我使用包装器的解决方案,仅删除 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/

      【讨论】:

      • 我不得不将 border-radius: 6px; 添加到 DIV 和 TABLE 中,因为出于某些其他原因我不想在 DIV 上使用 overflow: auto 样式。在 TABLE 和 DIV 上使用 border-radius 使得 overflow: auto 不必要。
      【解决方案5】:
      <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。
      【解决方案6】:

      对这个老问题的注释:

      我的 reset.css 设置了border-spacing: 0,导致角落被切断。我必须将其设置为 3px 才能使我的半径正常工作(值取决于相关半径)。

      【讨论】:

        【解决方案7】:

        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

        【讨论】:

        • 边框折叠:分开!重要;是的,对我也有用。
        【解决方案8】:

        它有效,这是使用的工具的问题:jsFiddle 的 规范化 CSS 通过隐藏浏览器的默认值来导致问题...
        http://jsfiddle.net/XvdX9/5/

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

        这两个模型之间的差异可以在另一个小提琴中看到:http://jsfiddle.net/XvdX9/11/(单元格上有一些透明度,左上角有一个巨大的边框半径,以便查看表格和单元格上发生的情况)

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

        【讨论】:

        • 你能解释一下为什么 normalize 会导致这个问题吗?当我检查小提琴的元素时,我找不到任何会杀死边界半径的东西。
        • 谢谢。我还找到了以下解决方案。 :)
        • @Fresheyeball 我添加了指向文档和比较的链接。我很高兴几年前我直接学习了 CSS,并且当表格布局与spacer.gif 一起使用和滥用时并没有在该领域工作;)
        • overflow 应该设置为 hidden 如果您希望 border-radius 影响内容
        • @BorisD.Teoharov 这应该包含在答案中。谢谢。
        猜你喜欢
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-31
        • 2012-08-03
        • 2018-04-24
        相关资源
        最近更新 更多