HTML,CSS : Adjustment height of the table to the number of rows

标签 html css

<div class="modal-body">
              <div class="table-responsive">
                 <table id="table">
                   <thead>
                    <tr>
                       <th>...</th>
                           ...
                    </tr>
                   <thead>

                   <tbody>
                    <tr>
                       <td>...</td>
                           ...
                    </tr>
                   <thead>
                 </table> 
              </div>
        </div>

我想根据行数调整表格的高度。我想使用一些数字 (N px) 来限制高度,但如果行数较少(填充少于 N px),则表格应该更低。我尝试设置最大高度但没有成功(当时框是空的)。

编辑: 我试过了: fiddle

最佳答案

只需将高度添加到 tr,这样表格就可以乘以 tr,我也将你的 id="table" 更改为 class="mytable" 只是为了展示这个例子。

tr {
  height: 50px;
}

table *{
  border: 1px solid green;
  box-sizing: border-box;
}

.table-responsive{
  display: inline-block;
  max-height: 150px;
  overflow: auto;
}
<div class="modal-body">
  <div class="table-responsive">
    <table class="mytable">
      <thead>
        <tr>
          <th>tr here 111111</th>
          <th>tr here 222222</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th>tr here 22222</th>
          <td>td here 22222</td>
        </tr>
        <tr>
          <th>tr here 33333</th>
          <td>td here 33333</td>
        </tr>
        <tr>
          <th>tr here 44444</th>
          <td>td here 44444</td>
        </tr>
            <tr>
          <th>tr here 55555</th>
          <td>td here 55555</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

<hr>

<div class="modal-body">
  <div class="table-responsive">
    <table class="mytable">
      <thead>
        <tr>
          <th>tr here 111111</th>
          <th>tr here 222222</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th>tr here 22222</th>
          <td>td here 22222</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

关于HTML,CSS : Adjustment height of the table to the number of rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44271192/

相关文章:

html - 在媒体查询中将一个 div 放在另一个之上

css - 我可以/应该将 box-shadow 的大小添加到元素的总大小吗?

html - 对齐框彼此相邻

javascript - 使用 javascript 更改 iframe 中的链接

php - 使用 Typeform 制作产品订单并使用 PapPal 付款

jquery - 一个好看的导航菜单,只剩下一个任务

html - 媒体查询在移动 Gmail 应用程序中不起作用

javascript - 在 jQuery 中选择当前时如何禁用或停用复选框

jquery - 下拉菜单无法正常工作

html - DIV 已分组,当我向每个 div 添加文本时它们不会下降