admin管理员组文章数量:1332353
I want to be able to have each cell in the header row (at the top) keep the same width (in pixels) regardless of what the bottom table is. The image shows an example with 12 columns in the bottom table.
I am using python to generate the HTML, and so I can get the number of columns and then do colspan depending on the number to get the top row to be close enough. I'm wondering if there is a better way? I considered making the bottom table inside one cell, but I want it to look seamless and I'm not sure how to do that either
I want to be able to have each cell in the header row (at the top) keep the same width (in pixels) regardless of what the bottom table is. The image shows an example with 12 columns in the bottom table.
I am using python to generate the HTML, and so I can get the number of columns and then do colspan depending on the number to get the top row to be close enough. I'm wondering if there is a better way? I considered making the bottom table inside one cell, but I want it to look seamless and I'm not sure how to do that either
Share Improve this question edited Nov 21, 2024 at 0:27 Barmar 783k56 gold badges547 silver badges660 bronze badges asked Nov 21, 2024 at 0:25 AeneasAeneas 11 bronze badge 2 |1 Answer
Reset to default 0To have all rows in a table maintain columns of equal width, irrespective of the number of columns in other rows, we cab use flexbox layout.
.table {
display: flex;
flex-direction: column;
border: 1px solid #ddd;
width: 100%;
}
.row {
display: flex;
width: 100%;
}
.row .cell {
flex: 1;
border: 1px solid #ddd;
padding: 10px;
text-align: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="table">
<div class="row">
<div class="cell">Row 1 - Col 1</div>
<div class="cell">Row 1 - Col 2</div>
<div class="cell">Row 1 - Col 3</div>
</div>
<div class="row">
<div class="cell">Row 2 - Col 1</div>
<div class="cell">Row 2 - Col 2</div>
</div>
<div class="row">
<div class="cell">Row 3 - Col 1</div>
<div class="cell">Row 3 - Col 2</div>
<div class="cell">Row 3 - Col 3</div>
<div class="cell">Row 3 - Col 4</div>
</div>
</div>
</body>
</html>
There are some advantages to of this as well, like, no need to give specific width to the columns, and also, the table will adjust according to the screen width.
本文标签: htmlHow can I make my columns in a table have set width regardless of numberStack Overflow
版权声明:本文标题:html - How can I make my columns in a table have set width regardless of number? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742321029a2452786.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
style="width: XXX"
to specify the width of eachth
. – Barmar Commented Nov 21, 2024 at 0:28