【问题标题】:Border issues on HTML TableHTML表格的边框问题
【发布时间】:2020-06-18 07:53:52
【问题描述】:

我有一张表格,我不需要任何内部边框,除了将标题与行分开的那个。

在我的 CSS 中,我有:

table { ...
    border-width: 2px;
    border-color: #ddd;
    border-style: solid;
}
table th {
  border-collapse: collapse;
  border-bottom: 2pt solid #830504;
  padding: 4px  8px ;
  text-align: center;
}
table tr {
  padding:4px;
}

table td {
  border-collapse: none;
  padding:4px;
}

我得到以下信息

如何去掉底部边框左右两个空格?

【问题讨论】:

  • 你能发布完整的代码吗?使用 HTML(或创建 JSFiddle)

标签: html css html-table border


【解决方案1】:

问题可能来自您在td 和th 上使用border-collapse 而不是table。

这就是区别:

.collapse {
  border-collapse: collapse
}

table {
  border: 1px solid black;
}

th {
  border-bottom: 1px solid black;
}
<table>
  <thead>
    <tr>
      <th>Head</th>
      <th>Header</th>
      <th>Bob</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Lorem Ipsum</td>
      <td>Lorem Ipsum</td>
      <td>Lorem Ipsum</td>
    </tr>
    <tr>
      <td>Lorem Ipsum</td>
      <td>Lorem Ipsum</td>
      <td>Lorem Ipsum</td>
    </tr>
  </tbody>
</table>

<table class="collapse">
  <thead>
    <tr>
      <th>Head</th>
      <th>Header</th>
      <th>Bob</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Lorem Ipsum</td>
      <td>Lorem Ipsum</td>
      <td>Lorem Ipsum</td>
    </tr>
    <tr>
      <td>Lorem Ipsum</td>
      <td>Lorem Ipsum</td>
      <td>Lorem Ipsum</td>
    </tr>
  </tbody>
</table>

【讨论】:

    【解决方案2】:

    将此作为最小的基本表css

    .tbl {
      width: 100%; /* you may change the width in % */
      margin: 0 auto;
      box-sizing: border-box;
      overflow-x: auto;
    }
    .tbl * {
      margin: 0;
      box-sizing: border-box;
    }
    table {
      width: 100%;
      border-spacing: 0;
      border-collapse: collapse;
      font-size: 0;
      background-color: transparent;
    }
    thead, tbody, tr {
      width: inherit;
    }
    th, td {
      vertical-align: top;
      text-align: left;
      hyphens: auto;
      white-space: normal;
    }
    @media (max-width: 767.9px) {
      table {
        border: 0;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        -ms-overflow-style: -ms-autohiding-scrollbar;
      }
    }
    

    有了这个html

    <div class="tbl">
      <table>
        <thead>
          <tr>
            <th>Header 1</th>
            <th>Header 2</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Data 11</td>
            <td>Data 12</td>
          </tr>
          <tr>
            <td>Data 21</td>
            <td>Data 22</td>
          </tr>
        </tbody>
      </table>
    </div>
    

    使用这个 css,您的表格可以响应所有屏幕尺寸,您只需为 th 和 td 提供一些颜色和/或边框

    【讨论】:

      【解决方案3】:

      使用您提供的 CSS,只需将 border-spacing: 0; 添加到您的表格样式中

      table {
                  border-width: 2px;
                  border-color: #ddd;
                  border-style: solid;
                  border-spacing: 0;
      
              }
      

      您可以在此处查看它的工作情况 https://jsfiddle.net/hd60zset/3/

      【讨论】:

      • @Aripheus 理想情况下,示例应该在堆栈片段中,而不是在外部链接中。
      • 由于这个答案解决了 OP 案例,我想指出它可能会导致 surprises 和 td/tr 和 table 边界之间的边界加倍在一般情况下,因为 border-collapse: collapse 属性未在 table 元素上设置。
      【解决方案4】:

      重置border-spacing,其默认值为 c.2px。

      如果您将此设置为0,则问题已解决。

      table {
        border-width: 2px;
        border-color: #ddd;
        border-style: solid;
        margin: 1em auto;
        border-spacing: 0;
        /* this */
      }
      
      table th {
        border-collapse: collapse;
        border-bottom: 10pt solid #830504;
        padding: 8px;
        text-align: center;
      }
      
      table tr {
        padding: 8px;
      }
      
      table td {
        padding: 4px;
        border-collapse: none;
      }
      <table>
        <tr>
          <th></th>
          <th></th>
          <th></th>
        </tr>
        <tr>
          <td>Item 1</td>
          <td>Item 2</td>
          <td>Item 3</td>
        </tr>
        <tr>
          <td>Item 1</td>
          <td>Item 2</td>
          <td>Item 3</td>
        </tr>
        <tr>
          <td>Item 1</td>
          <td>Item 2</td>
          <td>Item 3</td>
        </tr>
      </table>

      【讨论】:

        猜你喜欢
        • 2013-03-06
        • 2012-02-02
        • 2020-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-22
        • 1970-01-01
        • 2012-08-27
        相关资源
        最近更新 更多