Добавить внутреннюю границу в ячейки таблицы - PullRequest
0 голосов
/ 25 июня 2019

Я пытаюсь добавить внутреннюю границу к ячейкам таблицы следующим образом:

enter image description here

<!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>

enter image description here

Я пытаюсь добавить интервал к границе, чтобы можно было различить каждую границу ячейки.

Ответы [ 4 ]

1 голос
/ 25 июня 2019

Вы можете достичь желаемого результата, убрав 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>
0 голосов
/ 25 июня 2019

Вы можете использовать псевдоэлементы 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>
0 голосов
/ 25 июня 2019

Для этой цели вы можете использовать вставку в виде рамки *, например:

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>
0 голосов
/ 25 июня 2019

Для красного цвета td вы можете использовать CSS с помощью nth-child (), как я это делал

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

, это повлияет только на красное td

...