html - 表格和 div 宽度之间的差异

标签 html css html-table flexbox

我试图使 th 与嵌套 div 的宽度相同。所以基本上我希望列的宽度与 div 相同,当表和父 div 的大小相同时。

我设法使它们几乎相同,但表格单元格中仍有一些我无法弄清楚的填充。

Codesandbox 链接: https://codesandbox.io/s/wyzz001kyk

html:

<div class='header'>
  <div class='one'>One</div>
  <div class='two'>Two</div>
</div>
<table class='table'>
  <th class='one'>One</th>
   <th class='two'>Two</th>
</table>

CSS:

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.table {
  width: 100%;
  text-align: left;
  border-spacing: 0;
  font-weight: 500;
}

.one {
  width: 60%;
}

.two {
  width: 40%;
}

最佳答案

明确删除 th 和 td 元素的填充:

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.table {
  width: 100%;
  text-align: left;
  border-spacing: 0;
  font-weight: 500;
}
.one {
  width: 60%;
}
.two {
  width: 40%;
}
/* this */
.table th,
.table td {
  padding: 0;
}
<div class='header'>
  <div class='one'>One</div>
  <div class='two'>Two</div>
</div>
<table class='table'>
  <th class='one'>One</th>
  <th class='two'>Two</th>
</table>

关于html - 表格和 div 宽度之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53614573/

相关文章:

javascript - 使用 iframe 避免 Web 应用程序的缓存问题

html - 除了 JavaScript 和 VBScript,还有哪些客户端 Web 脚本语言?

jquery - 在 jQuery 中交替非连续行颜色

html - html 表格中等高缩放单元格

javascript - 更改 JavaScript 请求的查询字符串参数

javascript - 如何将 div 标签放在 Canvas 上

html - 无法将固定标题与内容 div 对齐

jquery - 复选框详细 View

jquery - 如何将图像添加到此 JQueryUI 自动完成插件的结果中?

javascript - 错误为 "Cannot set property ' innerHTML' of null”