You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Bootstrap table-bordered类时滚动条出现后表格边框对齐异常求助

Hey there! Let's fix that annoying table border alignment issue when scrollbars appear with Bootstrap's table-bordered class. I’ve run into this exact problem before, so let’s break down what’s going wrong and how to fix it.

Why This Happens

Your current code sets display:table;width:100%;table-layout:fixed; directly on the <thead> element. This detaches the header from the table body (<tbody>) in terms of width calculation. When a scrollbar appears, the <tbody> gets squeezed by the scrollbar’s width, but the <thead> still uses the full container width—causing the borders to misalign.

Solution 1: Use Bootstrap’s Native table-responsive (Easiest Fix)

Bootstrap has a built-in container class designed to handle scrollable tables while keeping borders aligned. Wrap your table in a <div class="table-responsive"> and remove the custom display:table style from the <thead>. Here’s your updated code:

<title>UI</title>
<div ng-controller="appCtrl">
  <div class="row">
    <div class="col-md-12">
      <!-- Add Bootstrap's table-responsive container -->
      <div class="table-responsive">
        <table style="border-bottom: 2px solid #cccccc" class="table table-bordered">
          <thead style="table-layout:fixed;">
            <tr>
              <th style="text-align:center; width: 15%;">Column 1</th>
              <th style="text-align:center; width: 30%;">Column 2</th>
              <th style="text-align:center; width: 25%;">Column 3</th>
              <th style="text-align:center; width: 30%;">Column 4</th>
            </tr>
          </thead>
          <tbody>
            <!-- Your table rows here -->
            <tr>
              <td>Sample Data</td>
              <td>Longer sample data to trigger scroll</td>
              <td>More data</td>
              <td>Even more data that wraps</td>
            </tr>
            <!-- Add enough rows to make the table scroll -->
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

Solution 2: Custom Fixed Header with Aligned Borders

If you need a fixed header (stuck at the top while scrolling), the above method won’t work. Instead, we’ll use CSS to stick the header and JavaScript to sync column widths between <thead> and <tbody>:

Step 1: Add CSS for Fixed Header

/* Wrap your table in this container */
.table-scroll-container {
  overflow-y: auto;
  max-height: 400px; /* Adjust to your desired scrollable height */
}

.table-scroll-container table {
  width: 100%;
  table-layout: fixed; /* Critical for consistent column widths */
}

.table-scroll-container thead tr {
  position: sticky;
  top: 0;
  background-color: #fff; /* Prevent body content from showing through */
  z-index: 1;
}

Step 2: Add Angular Controller Code to Sync Columns

app.controller('appCtrl', function($scope, $timeout) {
  // Wait for DOM to render before calculating widths
  $timeout(function() {
    const table = document.querySelector('.table-bordered');
    const headerCells = table.querySelectorAll('thead th');
    const firstBodyRowCells = table.querySelectorAll('tbody tr:first-child td');

    // Sync each header cell width to match the corresponding body cell
    headerCells.forEach((headerCell, index) => {
      headerCell.style.width = `${firstBodyRowCells[index].offsetWidth}px`;
    });

    // Optional: Re-sync if window resizes
    window.addEventListener('resize', () => {
      headerCells.forEach((headerCell, index) => {
        headerCell.style.width = `${firstBodyRowCells[index].offsetWidth}px`;
      });
    });
  });
});

Step 3: Update Your HTML Structure

<title>UI</title>
<div ng-controller="appCtrl">
  <div class="row">
    <div class="col-md-12">
      <div class="table-scroll-container">
        <table style="border-bottom: 2px solid #cccccc" class="table table-bordered">
          <thead>
            <tr>
              <th style="text-align:center;">Column 1</th>
              <th style="text-align:center;">Column 2</th>
              <th style="text-align:center;">Column 3</th>
              <th style="text-align:center;">Column 4</th>
            </tr>
          </thead>
          <tbody>
            <!-- Your scrollable table rows here -->
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

Key Takeaways

  • Avoid setting display:table on <thead>—it breaks the table’s natural structure.
  • Use table-layout:fixed on the <table> element, not just the header, to enforce consistent column widths.
  • For most cases, Bootstrap’s table-responsive is the simplest and most reliable fix.

内容的提问来源于stack exchange,提问作者Xander

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:42:14