html - How can I make my columns in a table have set width regardless of number? - Stack Overflow

I want to be able to have each cell in the header row (at the top) keep the same width (in pixels) rega

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
  • use style="width: XXX" to specify the width of each th. – Barmar Commented Nov 21, 2024 at 0:28
  • Please edit the question and show your HTML and - if you used it - CSS. We can't improve on unknown code that apparently resulted in this image. So the output of what your program produced. – Peter B Commented Nov 21, 2024 at 0:37
Add a comment  | 

1 Answer 1

Reset to default 0

To 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742321030a4421810.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信