首页 > 解决方案 > 表格和 div 宽度之间的差异

问题描述

我试图使宽度与嵌套 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%;
}

标签: htmlcsshtml-tableflexbox

解决方案


从 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>


推荐阅读