我有一些 HTML 想要使用 display: table css 属性来控制。不幸的是,我实际上无法更改 HTML(或 JS),只能更改 CSS(长篇大论)。这是一个问题,因为现有 HTML 的结构给表格布局带来了麻烦。这是 HTML 和 CSS 的简化版本:
<style>
.like-table { display: table;}
.like-tr { display: table-row;}
.like-th { display: table-cell; border: 1px solid gray;}
.useless-div-1 { }
.useless-div-2 { }
.like-td { display: table-cell; border: 1px solid gray;}
</style>
<div class="like-table">
<div class="like-tr">
<div class="like-th">1</div>
<div class="like-th">22</div>
<div class="like-th">3</div>
</div>
<div class="useless-div-1"><div class="useless-div-2">
<div class="like-tr">
<div class="like-td">111</div>
<div class="like-td">2</div>
<div class="like-td">3</div>
</div>
<div class="like-tr">
<div class="like-td">11</div>
<div class="like-td">2</div>
<div class="like-td">333</div>
</div>
</div></div>
</div>
可悲的是,这使得标题和正文列的宽度不同:
如果我删除“useless-div-*”打开和关闭标签:
<div class="like-table">
<div class="like-tr">
<div class="like-th">1</div>
<div class="like-th">22</div>
<div class="like-th">3</div>
</div>
<div class="like-tr">
<div class="like-td">111</div>
<div class="like-td">2</div>
<div class="like-td">3</div>
</div>
<div class="like-tr">
<div class="like-td">11</div>
<div class="like-td">2</div>
<div class="like-td">333</div>
</div>
</div>
然后它呈现精细,对齐标题和正文列宽:
那么,我可以对 CSS 做些什么来使第一组 HTML 表现得像第二组一样吗? 请记住,我不能修改 HTML 或 JavaScript——只能修改 CSS!请不要问为什么...