简体   繁体   English

td 与其边框元素之间的奇怪空间:CSS 表

[英]Weird space between td and its border element : CSS tables

I am trying to create a CSS table with border all over using the standard method of applying top and left borders to all td elements (apart from those in first column and first row).我正在尝试使用将顶部和左侧边框应用于所有td元素(除了第一列和第一行中的元素)的标准方法来创建一个带有边框的 CSS 表。 And then there is a border on the <table> .然后在<table>上有一个边框。 This method suits my use-case better than one in which we use border-collapse .这种方法比我们使用border-collapse的方法更适合我的用例。

But then there is a bug which resembles inner margin on td elements.但是有一个类似于 td 元素内边距的错误。 This margin like space isn't always there but only occurs on some viewports for some reason.这种像空间一样的边距并不总是存在,但由于某种原因只出现在某些视口上。 Here's my code:这是我的代码:

 *{ padding: 0; margin: 0; } html, body { height: 100%; width: 100%; } .table-container { display: flex; flex-direction: column; align-items: center; padding-bottom: 50px; font-size: 20px; } table { font-size: 15px; border-spacing: 0; border: 4px solid black; border-radius: 35px 35px 0 0; border-left: none; border-top: none; width: 90vw; background-color: white; margin-top: 50px; text-align: center; } td { box-sizing: content-box; border-left: 4px solid black; border-top: 4px solid black; height: 60px; } thead td:first-child { border-top-left-radius: 35px; } thead td:last-child { border-top-right-radius: 33px; } thead td { font-size: 20px; font-weight: 500; } table button { cursor: pointer; height: 100%; border: 0; background-color: white; color: #d90429; } .progress-bar{ background-color: orange; } .progress-bar > div{ background-color: green; height: 100%; width: 50% }
 <div class="table-container"> <table> <thead> <tr> <td>Task Type</td> <td>Target Time (in Minutes)</td> <td>Percentage Achieved</td> <td>Add/Delete Tasks</td> </tr> </thead> <tbody> <tr> <td>Maths</td> <td>120</td> <td class="progress-bar"> <div>50</div> </td> <td> <button type="button"> Delete </button> </td> </tr> </tbody> </table> </div>

Here's one viewport in which this bug can be seen in action:这是一个可以看到此错误的视口: 在此处输入图像描述

I guess running above code snippet and looking at the output in full screen will reproduce this bug我猜想运行上面的代码片段并全屏查看输出会重现这个错误

EDIT编辑

  1. As pointed out by KIKOSoftware in comments, the problem seems to be specific to chrome.正如 KIKOSoftware 在评论中指出的那样,这个问题似乎是特定于 chrome 的。
  2. My Version of Chrome happens to use 3.991px for border instead of 4px.我的 Chrome 版本恰好使用 3.991 像素而不是 4 像素作为边框。

on a personal level, having had a thousand problems with borders in HTML tables, I ended up opting for a radical solution: no border at all!在个人层面上,在 HTML 表格的边框方面遇到了一千个问题,我最终选择了一个激进的解决方案:根本没有边框!
I use border-collapse: separate;我使用border-collapse: separate; and I play on border-spacing我玩border-spacing
in addition it lightens the css to write另外它减轻了CSS的编写

I recommend it我推荐它

in your case:在你的情况下:

 * { padding : 0; margin : 0; } html, body { height : 100%; width : 100%; background-color: #658d8d; /* this one is mine... */ } .table-container { display : flex; flex-direction : column; align-items : center; padding-bottom : 50px; font-size : 20px; margin: 1em; } table { font-size : 15px; border-collapse : separate; border-spacing : 4px; background-color : black; border-radius : 33px 33px 0 0; width : 90vw; } thead td { font-size : 20px; font-weight : 500; } td { background-color : white; height : 60px; text-align : center; } thead td:first-child { border-top-left-radius: 29px; } thead td:last-child { border-top-right-radius: 29px; } table button { cursor : pointer; height : 100%; width : 100%; border : 0; background-color : white; color : #d90429; } .progress-bar { background-color: orange; } .progress-bar > div { background-color : green; height : 100%; width : 50% }
 <div class="table-container"> <table> <thead> <tr> <td>Task Type</td> <td>Target Time (in Minutes)</td> <td>Percentage Achieved</td> <td>Add/Delete Tasks</td> </tr> </thead> <tbody> <tr> <td>Maths</td> <td>120</td> <td class="progress-bar"><div>50</div></td> <td><button type="button">Delete</button> </td> </tr> </tbody> </table> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM