CSS3's border-radius property and border-collapse:collapse don't mix. How can I use border-radius to create a collapsed table with rounded corners?
编辑 - 原始标题:是否有另一种方法可以在
因为事实证明只是让表格的边框崩溃并不能解决根本问题,所以我更新了标题以更好地反映讨论。
我正在尝试使用
1 2 3 4 5 | table { -moz-border-radius:10px; -webkit-border-radius:10px; border-radius:10px } |
这是问题所在。我还想设置
编辑:
我做了一个简单的页面来演示这里的问题(仅限Firefox / Safari)。
看起来很大一部分问题是将表设置为圆角不会影响角落
建议的解决方案摘要:
桌子周围有另一个圆角的元素不起作用,因为桌子的方角"渗透"。
将边框宽度指定为0不会折叠表。
将cellspacing设置为零后,底部
使用JavaScript代替 - 避免问题。
可能的解决方案:
这些表是用PHP生成的,所以我可以为每个外部的t / tds应用一个不同的类,并分别为每个角设置样式。我宁愿不这样做,因为它不是很优雅,并且应用于多个表格有点痛苦,所以请保持建议。
可能的解决方案2是使用JavaScript(特别是jQuery)来设置角落的样式。这个解决方案也有效,但仍然不是我想要的(我知道我很挑剔)。我有两个保留意见:
我知道今天尝试用CSS3做这件事似乎是不必要的,但我有我的理由。我还想指出,这个问题是w3c规范的结果,而不是CSS3支持不好,所以当CSS3得到更广泛的支持时,任何解决方案仍然具有相关性和实用性。
- jquery.corner插件提供了一个不错的选择:http://www.malsup.com/jquery/corner/有一个很好的演示它的功能。 您可以通过定义半径等来指定要更改的角和要执行的更改方式。
- 难道你不能将表包装在div中,设置border-radius和div上的"overflow:hidden"吗? 我刚刚测试过并且工作正常,除非您需要在具有固定宽度/高度的div或其父项中进行滚动/扩展。
- 我认为,最后一个CSS语句缺少分号。
- 这个问题在2009年被问到。我有点惊讶的是,2015年没有比下面列出的更好的解决方案。 几年前W3C应该已经解决了这个问题。
我想到了。你只需要使用一些特殊的选择器。
圆角的问题是td元素也没有变圆。您可以通过执行以下操作来解决此问题:
1 2 3 4 5 6 7 | table tr:last-child td:first-child { border-bottom-left-radius: 10px; } table tr:last-child td:last-child { border-bottom-right-radius: 10px; } |
现在一切正常,除了仍然存在
-
为什么不将
border-spacing: 0; 添加为边框样式,而不是使用HTML? - 我在设置TR标签的背景颜色而不是TD标签时遇到了问题。如果你正在剥离你的桌子,你要设置TD的背景颜色而不是TR。
- 那么如果你必须在TR上使用背景颜色会发生什么?有可能吗?
- tr.even> td {background-color:rgb(200,200,200); }?
- 拉蒙是对的。然后只需编辑border-radius:first-child和:last-child。
-
您也可以使用
table thead tr t[h|d] {border-top-[left|right]-radius: 1mm;} 和table tfoot tr t[h|d] {border-bottom-[left|right]-radius: 1mm;} -
只需像Ramon一样添加
border-spacing: 0; 就可以为我修复它。确保将border-radius 和border-spacing 样式添加到或 元素,而不是 或 。 - 不能在Chrome上使用Opera
-1 - 但是你的解决方案不起作用
- 我正在使用bootstrap,我通过使用Ramon的建议找到了解决方案,如下所示:
border-spacing: 0; border-collapse: separate;
以下方法通过使用
box-shadow 的扩展1px 而不是"真实"边框来工作(在Chrome中测试)。1
2
3
4
5
6
7
8
9
10table {
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;
}- 这是唯一对我有用的东西。虽然很难在桌边上获得正确的颜色。
- 如果您的桌子背景颜色与周围区域不同,则无法使用。
- 感谢您的代码,它也适用于Firefox 26.0
- @ g.pickardou可以通过在表元素上添加'overflow:hidden'来解决这个问题。
- 像魅力一样工作!
如果你想要一个只有CSS的解决方案(不需要在HTML中设置
cellspacing=0 ),允许1px边框(你不能用border-spacing: 0 解决方案),我更喜欢做以下事情:-
为表格单元格设置
border-right 和border-bottom (td 和th ) -
给第一行中的单元格
border-top -
给第一列中的单元格
border-left -
使用
first-child 和last-child 选择器,围绕四个角中的表格单元格的相应角。
在这里看一个演示。
给出以下HTML:
请参见下面的示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56.custom-table{margin:30px;}
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 th:last-child{
border-top:solid 1px #bbb;}
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;
}
table.Info tr th,
table.Info tr:first-child td
{
border-top: 1px solid #bbb;
}
/* top-left border-radius */
table tr:first-child th:first-child,
table.Info tr:first-child td:first-child {
border-top-left-radius: 6px;
}
/* top-right border-radius */
table tr:first-child th:last-child,
table.Info tr:first-child td: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;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18<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>- 请写下包含(永久)代码的答案。如果答案中有很多代码,只需发布??相关位并说明它们相关的原因。
- 这是一个很好的解决方案,但它有点难以阅读。我重写了一些样式规则并添加了代码的解释,所以希望这会有所帮助。
- 也将半径应用于表,否则当您将背景应用于表本身时,它看起来很奇怪。
-
table.Info 类与什么有什么关系?
您是否尝试使用
table{border-spacing: 0} 而不是table{border-collapse: collapse} ???- 谢谢,这让我做我需要做的事情(在'圆角'框的顶部包含一系列TH元素,包含下面的所有TD)
-
border-spacing: 0 的问题是你不能拥有1px边框,对吗?因为边框堆叠而不是折叠。 -
border-collapse: separate; border-spacing: 0; border-radius: 10px; overflow: hidden; 正是我所需要的。
您可能需要在表格周围放置另一个元素并使用圆形边框进行样式设置。
工作草案指定当
border-collapse 的值为collapse 时,border-radius 不适用于表元素。- 这也是我考虑过的,但是如果我创建一个div来围绕桌子并将其设置为圆角,那么方桌角落仍会渗透。请参阅新发布的示例。
- 我能找到的最好的折衷方案是在桌面上添加一个THEAD块并将灰色背景应用到它上面(桌面上有#eee)。标题单元格溢出TABLE的边框后面而不是它的前面。然后我将表格边框增加到3px以隐藏溢出。
-
@vamin"流血" - 如果您使用
overflow:hidden; 则不会 - 所以在这种情况下,每个人都可以从这些页面底部使用我的解决方案b2n.ir/table-radius
正如Ian所说,解决方案是将表嵌套在div中并将其设置为:
1
2
3
4.table_wrapper {
border-radius: 5px;
overflow: hidden;
}使用
overflow:hidden 时,方角不会通过div流血。-
请记住,无论谁想要使用它,使用
overflow: hidden 任何弹出/工具提示都会被包装器尺寸限制。
据我所知,你能做到的唯一方法是修改所有单元格,如下所示:
1
2
3
4table td {
border-right-width: 0px;
border-bottom-width: 0px;
}然后在底部和右后方获得边框
1
2
3
4
5
6table tr td:last-child {
border-right-width: 1px;
}
table tr:last-child td {
border-bottom-width: 1px;
}:last-child 在ie6中无效,但如果您使用border-radius ,我认为您不在乎。编辑:
查看您的示例页面后,您似乎可以使用单元格间距和填充来解决此问题。
您看到的厚灰色边框实际上是表格的背景(如果将边框颜色更改为红色,则可以清楚地看到这一点)。如果将cellspacing设置为零(或等效地:
td, th { margin:0; } ),则灰色"边框"将消失。编辑2:
我只能用一张桌子找不到办法。如果您将标题行更改为嵌套表,您可能可以获得所需的效果,但它将更多工作,而不是动态。
- 谢谢Ithi,但这不适合我。
- 我添加了一个cellspacing = 0的例子,它更接近了。不受欢迎的边框消失,但底角仍然流血。
- 再次感谢你的帮助。这些表是在php中生成的,所以我想如果没有提出一个优雅的解决方案,我只需要为每个角分配一个类并分别设置它们。
我尝试使用
thead th:first-child 和thead th:last-child 上的伪元素:before 和:after 进行解决与用
包裹桌子相结合 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22table 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;
}见jsFiddle
适用于我的Chrome(13.0.782.215)让我知道这是否适用于其他浏览器。
实际上,您可以在
div 中添加table 作为其包装。然后将这些CSS 代码分配给包装器:1
2
3
4
5
6
7
8
9.table-wrapper {
border: 1px solid #f00;
border-radius: 5px;
overflow: hidden;
}
table {
border-collapse: collapse;
}
我有同样的问题。
完全删除border-collapse 并使用:
cellspacing="0" cellpadding="0"
在html文档中。
例:1<table class="top_container" align="center" cellspacing="0" cellpadding="0">- 这有效,但它需要你之后使用firstchild / lastchild技巧来获得效果。
我刚刚写了一套疯狂的CSS,看起来效果很好:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59table {
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;
}
/* end complicated rounded table corners !*/
对于带边框和可滚动的表,使用此(替换变量,
$ 起始文本)如果您使用
thead ,tfoot 或th ,只需将tr:first-child 和tr-last-child 以及td 替换为它们。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#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:
1
2
3
4
5
6
7
8
9
10<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>
给定的答案仅在表格周围没有边框时才起作用,这是非常有限的!
我在SASS中有一个宏来完成这项工作,它完全支持外部和内部边框,实现与边框折叠相同的样式:折叠而不实际指定它。
在FF / IE8 / Safari / Chrome中测试过。
在所有浏览器中为纯CSS提供漂亮的圆形边框,但IE8(优雅地降级)因为IE8不支持border-radius :(
某些旧版浏览器可能需要供应商前缀才能使用
border-radius ,因此可以根据需要随意将这些前缀添加到代码中。这个答案不是最短的 - 但它确实有效。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26.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;
}要应用此样式,只需更改您的
1<table>标记为以下内容:
1<table class="roundedTable">并确保在HTML中包含上述CSS样式。
希望这可以帮助。
- 你不再需要border-radius的前缀,期望FF 3.6(-moz)。此外,肯定不再需要-khtml。
-
@JonatanLittke,如果您认为可以改进,您可以随时编辑答案。我删除了所有前缀,并添加了一个指向caniuse.com的链接,以便人们可以自己决定
border-radius 的前缀。
边界折叠的解决方案:表和显示分开:tbody和thead的内联表。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25table {
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;
}- 没有理由做出这个回答社区维基。这样做会使你从答案中得不到任何声誉。
我是HTML和CSS的新手,我也在寻找解决方案,我在这里找到了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24table,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
}我试试看,猜猜它的作用:)
遇到同样的问题后找到了这个答案,但发现它很简单:只需给表溢出:隐藏
不需要包装元素。当然,我不知道7年前这个问题最初会被问到是否会起作用,但它现在有效。
- 这是一个聪明的解决方案,但它也"移除"了实际的边界。在Chrome上,表格的右边框和下边框消失,所有圆角都没有边框。
带圆角和带边框的桌子。
使用@Ramon Tayag解决方案。关键是使用
border-spacing: 0 指出。使用SCSS的解决方案。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16$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;
}
}
我开始尝试"显示",我发现:
border-radius ,border ,margin ,padding ,在table 中显示:1display: inline-table;例如
1
2
3
4
5
6
7table tbody tr {
display: inline-table;
width: 960px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}但我们需要设置每列的
width 1
2
3
4
5
6tr td.first-column {
width: 100px;
}
tr td.second-column {
width: 860px;
}- 没有在FF 31.0中工作
以下是如何使用http://medialoot.com/preview/css-ui-kit/demo.html中的圆角实现表格的最新示例。它基于Joel Potter上面提出的特殊选择器。正如你所看到的,它还包括一些使IE变得有点快乐的魔力。它包含一些额外的样式来交替行的颜色:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90table-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;
}
我总是这样使用Sass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19table {
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;
}
}
}-
我想你可能没有得到它,他试图在启用
border-collapse: collapse 的情况下做到这一点。 - @giovannipds看看他自己的回复(接受回答)。我的方式只是另一种方式。现在删除"-1"。
-
哦,对不起,你肯定是对的,我的错误,他接受的回答似乎完全相同。我要坚持问题标题中的内容,他强调他想要边界崩溃,所以我直接去了。如果可以的话,我会删除-1,但我不能,你需要在答案中编辑一些内容以允许我这样做。试着提一下
border-collapse: collapse 无法做到的事情。再次原谅,我会等你的更新。
现在正式支持Border-radius。因此,在上述所有示例中,您可以删除"-moz-"前缀。
另一个技巧是使用与边框相同的颜色作为顶部和底部行。所有3种颜色相同,它融合在一起,看起来像一个完美的圆桌,即使它不是物理的。
Copyright © 码农家园 联系:[email protected] - 不能在Chrome上使用Opera