【问题标题】:Add inner border to table cells为表格单元格添加内边框
【发布时间】:2019-06-26 03:26:54
【问题描述】:

我正在尝试为表格单元格添加内边框,如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">       
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
      </tr>
      <tr>
        <td style="border-top: 2px solid green; border-bottom: 2px solid green; border-left: 2px solid green; border-right: 1px solid green;">Mary</td>
        <td style="border-top: 2px solid red; border-bottom: 2px solid red; border-left: 2px solid red; border-right: 2px solid red;">Moe</td>
        <td>mary@example.com</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>july@example.com</td>
      </tr>
    </tbody>
  </table>
</div>

</body>
</html>

我正在尝试为边框添加间距,以便可以区分每个单元格边框。

【问题讨论】:

    标签: html css sass less


    【解决方案1】:

    您可以通过删除表格上的border-collapse并将border-spacing设置为0来达到预期的效果

    .table {
      border-collapse: unset;
      border-spacing: 0;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
    </head>
    
    <body>
    
      <div class="container">
        <table class="table table-bordered">
          <thead>
            <tr>
              <th>Firstname</th>
              <th>Lastname</th>
              <th>Email</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>John</td>
              <td>Doe</td>
              <td>john@example.com</td>
            </tr>
            <tr>
              <td style="border-top: 2px solid green; border-bottom: 2px solid green; border-left: 2px solid green; border-right: 1px solid green;">Mary</td>
              <td style="border-top: 2px solid red; border-bottom: 2px solid red; border-left: 1px solid red; border-right: 2px solid red;">Moe</td>
              <td>mary@example.com</td>
            </tr>
            <tr>
              <td>July</td>
              <td>Dooley</td>
              <td>july@example.com</td>
            </tr>
          </tbody>
        </table>
      </div>
    
    </body>
    
    </html>

    【讨论】:

      【解决方案2】:

      对于红色 td,您可以像我一样使用 nth-child() 来 CSS

      .table-bordered tr:nth-child(2) td:nth-child(2) {
          padding-left: 43px;
          text-align: center;
      }
      

      这只会影响红色的td

      【讨论】:

        【解决方案3】:

        你可以使用 box-shadow inset 来达到这个目的,像这样

        box-shadow: inset 0px 0px 0px 4px green;
        

        <head>
          <title>Bootstrap Example</title>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
          <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
        </head>
        
        <body>
        
          <div class="container">
            <table class="table table-bordered">
              <thead>
                <tr>
                  <th>Firstname</th>
                  <th>Lastname</th>
                  <th>Email</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td>John</td>
                  <td>Doe</td>
                  <td>john@example.com</td>
                </tr>
                <tr>
                  <td style="border-top: 2px solid red; border-bottom: 2px solid red; border-left: 1px solid red; border-right: 2px solid red; box-shadow: inset 0px 0px 0px 4px #00e200;">Mary</td>
                  <td style="border-top: 2px solid red; border-bottom: 2px solid red; border-left: 1px solid red; border-right: 2px solid red;">Moe</td>
                  <td>mary@example.com</td>
                </tr>
                <tr>
                  <td>July</td>
                  <td>Dooley</td>
                  <td>july@example.com</td>
                </tr>
              </tbody>
            </table>
          </div>
        
        </body>
        
        </html>

        【讨论】:

          【解决方案4】:

          您可以使用 css 伪元素 :after 或 :before 与绝对位置。重要的是 td 具有相对位置(实际上是静态的),因此伪元素是绝对的。

          table tr td {
            position: relative;
          }
          
          .red:after {
            content: '';
            position: absolute;
            top: 0px;
            left: 0px;
            width: 100%;
            height: 100%;
            border: solid 2px red;
          }
          
          .green:after {
            content: '';
            position: absolute;
            top: 0px;
            left: 0px;
            width: 100%;
            height: 100%;
            border: solid 2px green;
          }
          <head>
            <title>Bootstrap Example</title>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
          </head>
          
          <body>
            <div class="container">       
              <table class="table table-bordered">
                <thead>
                  <tr>
                    <th>Firstname</th>
                    <th>Lastname</th>
                    <th>Email</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td>John</td>
                    <td>Doe</td>
                    <td>john@example.com</td>
                  </tr>
                  <tr>
                    <td class="green">Mary</td>
                    <td class="red">Moe</td>
                    <td>mary@example.com</td>
                  </tr>
                  <tr>
                    <td>July</td>
                    <td>Dooley</td>
                    <td>july@example.com</td>
                  </tr>
                </tbody>
              </table>
            </div>
          </body>

          【讨论】:

            猜你喜欢
            • 2018-11-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-01-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多