我试图使 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/