带列的可滚动 HTML 表格

标签 html css

我正在用 html 创建一个表格,它需要可滚动。所以我将显示更改为 block 并进行溢出滚动。当我这样做时,它使列标题没有间隔,因此有很多多余的空间。我知道显示 block 设置是造成这种情况的原因,但我需要使其可滚动。我如何在 css 和 html 中设置它?

table {
  margin-bottom: 40px;
  width: 100%;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  height: 250px !important;
  overflow-y: scroll;
  display: block;
}

  <table>
    <thead>
      <tr class="rowTable header">
        <th class="cell">Date Submitted</th>
        <th class="cell">Bounty Name</th>
        <th class="cell">Company</th>
        <th class="cell">Paid</th>
        <th class="cell">Details</th>
        <th class="cell">Response</th>
      </tr>
    </thead>
    <tbody>
      {% for report in recentReports %}
      <tr class="rowTable">
        <td class="cell">{{report.date}}</td>
        <td class="cell"><a href="/_hunter/bounty/{{report.bountyID}}">{{report.name}}</td>
        <td class="cell"><a href="/_hunter/company/{{report.accountID}}">{{report.company}}</a></td>
        <td class="cell">{{report.amountPaid}}</td>
        <td class="cell">
          <button type="button" class="detailsButton" data-toggle="modal" 
          data-target="#detailsModal" data-whatever="@getbootstrap" 
          data-ID={{report.reportID}}>
            View
          </button>
        </td>
        <td class="cell">
          <button type="button" class="messageButton" data-toggle="modal" 
            data-target="#messageModal" data-whatever="@getbootstrap" 
            data-ID={{report.reportID}}>
            View
          </button>
        </td>
      </tr> 
      {% endfor %}
    </tbody>
  </table>

这是发生的事情的照片。我认为很明显我想要的是列均匀地占据整个表格。图像只是表格,而不是整个网页。

enter image description here

最佳答案

您需要按照@Johannes 的建议进行操作:

<div id="wrapper">
  <!-- put your table here-->
</div> 

将您的 Css 更改为:

#wrapper {
  margin-bottom: 40px;
  width: 100%;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  height: 250px !important;
  overflow-y: scroll;
  display: block;
}
table{
  width:100%
}

你就完成了。如果您觉得此答案有用,请为@Johannes 的答案投票。如果您觉得满意,请投票给我。

看例子:

https://jsfiddle.net/ex239ck6/

关于带列的可滚动 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34168374/

相关文章:

html - 矩形 HTML 和 CSS3 中的菱形

javascript - 如何提高scrollmagic的速度

html - 子元素清除父元素的CSS

html - 字体系列未应用于移动设备

javascript - 仅隐藏输入字段的内容

html - 删除 CSS 菜单中元素之间的间隙

javascript - 如何设置彼此相邻的图像列表

django - 如何在 Django 中从 html5 和 css3 创建 pdf?

html - 哪个更好 : class with single rule or inline style?

css - 如何在主 div 处于隐藏状态时使用另一个 CSS 样式