【问题标题】:Borders and margin between thead and tbodythead 和 tbody 之间的边框和边距
【发布时间】:2019-04-21 06:17:28
【问题描述】:

我正在尝试使用 CSS 显示 thead 和 tbody 之间的边框以及一些边距。

    table thead  {
    	border: solid red 1px;
    	margin-bottom: 10px;
    }
    
    table tbody {
        border: solid green 1px;
    }
  <table>
    <thead>
      <tr>
        <th scope="col">Thumbnail</th>
        <th scope="col">Title</th>
        <th scope="col">Date Created</th>
        <th scope="col">View</th>
        <th scope="col">Edit</th>
        <th scope="col">Delete</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><img src="https://picsum.photos/25/25/?random&r=1" /></td>
        <td>Lorem ipsum</td>
        <td>Nov 3, 2017</td>
        <td>Icon-View</td>
        <td>Icon-Edit</td>
        <td>Icon-Delete</span></td>
      </tr>
    </tbody>
  </table>

上面的 CSS 将边框和边距属性应用于thead,但不知何故边框正在折叠,我尝试将border-collapse: separate 改为table,但没有成功。

【问题讨论】:

    标签: html css


    【解决方案1】:

    可以使用outline: thin solid设置tbody和thead边框

    thead {
      outline: thin solid red;
    }
    
    tbody {
      outline: thin solid green;
    }
    
    td{
      padding-top: 20px;
    }
    <table>
      <thead>
        <tr>
          <th scope="col">Thumbnail</th>
          <th scope="col">Title</th>
          <th scope="col">Date Created</th>
          <th scope="col">View</th>
          <th scope="col">Edit</th>
          <th scope="col">Delete</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><img src="https://picsum.photos/25/25/?random&r=1" /></td>
          <td>Lorem ipsum</td>
          <td>Nov 3, 2017</td>
          <td>Icon-View</td>
          <td>Icon-Edit</td>
          <td>Icon-Delete</td>
        </tr>
      </tbody>
    </table>

    另一种选择是添加一个空行来模拟边距

    thead {
      outline: thin solid red;
    }
    
    tr{
      outline: thin solid green;
    }
    
    tr:first-child{
      outline: thin solid transparent;
      height: 10px;
    }
    <table>
      <thead>
        <tr>
          <th scope="col">Thumbnail</th>
          <th scope="col">Title</th>
          <th scope="col">Date Created</th>
          <th scope="col">View</th>
          <th scope="col">Edit</th>
          <th scope="col">Delete</th>
        </tr>
      </thead>
      <tbody>
        <tr>
        </tr>
        <tr>
          <td><img src="https://picsum.photos/25/25/?random&r=1" /></td>
          <td>Lorem ipsum</td>
          <td>Nov 3, 2017</td>
          <td>Icon-View</td>
          <td>Icon-Edit</td>
          <td>Icon-Delete</td>
        </tr>
      </tbody>
    </table>

    【讨论】:

    • 行间不能设置边距,但可以设置内边距
    • 啊,这就是问题所在,我为thead&gt;th 设置了单元格边框(仅在右侧),并且填充会增加它们的或单元格高度。
    • 查看我建议的第二个选项
    • 你也可以使用伪元素来模拟thead和tbody之间的边距,像这样:jsbin.com/sadawotaqi/edit?html,css,output
    • @Alohci 可爱... :)
    猜你喜欢
    • 2012-03-04
    • 2015-10-01
    • 2010-12-02
    • 2021-12-02
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    相关资源
    最近更新 更多