Twitter引导中心表列垂直和水平列内容

时间:2016-01-04 23:21:26

标签: html css twitter-bootstrap

假设我有下表:

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Summer is coming</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.3.5/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  <style type="text/css">
    /* centered columns styles */
    .row-centered {
      text-align: center;
    }
    .col-centered {
      display: inline-block;
      float: none;
      /* reset the text-align */
      text-align: left;
      /* inline-block space fix */
      margin-right: -4px;
    }
  </style>
</head>

<body>
  <div class="table-responsive">
    <table class="table table-striped table-bordered">
      <thead>
        <tr>
          <th>Time</th>
          <th>Monday</th>
          <th>Tuesday</th>
          <th>Wednesday</th>
          <th>Thursday</th>
          <th>Friday</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>0700 - 0730</td>
          <td>Stretches</td>
          <td>Stretches</td>
          <td>Stretches</td>
          <td>Stretches</td>
          <td>Stretches</td>
        </tr>
        <tr>
          <td>0730 - 0800</td>
          <td rowspan="2" class="col-centered row-centered">Eat breakfast (I want this centered)</td>
          <td rowspan="2">Eat breakfast</td>
          <td rowspan="2">Eat breakfast</td>
          <td rowspan="2">Eat breakfast</td>
          <td rowspan="2">Eat breakfast</td>
        </tr>
        <tr>
          <td>0800 - 0830</td>
        </tr>
      </tbody>
    </table>
  </div>

</body>

</html>

JSFiddle link

我试图将跨越多行的列的内容居中放在列的中心,但是按照我的方式,似乎没有任何事情发生。

我正在使用此css来实现居中,但它并不是内容的中心:

 /* centered columns styles */
 .row-centered {
   text-align: center;
 }

 .col-centered {
   display: inline-block;
   float: none;
   /* reset the text-align */
   text-align: left;
   /* inline-block space fix */
   margin-right: -4px;
 }

我该如何做到这一点?

1 个答案:

答案 0 :(得分:3)

好。您可以使用以下方法实现垂直对齐:

.table>tbody>tr>td {
    vertical-align: middle;
    display: table-cell; /* Courtesy: @Tracker1, for consistency across browsers */
}

Bootstrap有一个CSS规则来抵消这一点。它就像:

.table>tbody>tr>td, .table>tbody>tr>th, .table>tfoot>tr>td,    
.table>tfoot>tr>th, .table>thead>tr>td, .table>thead>tr>th {
    padding: 8px;
    line-height: 1.42857143;
    vertical-align: top;
    border-top: 1px solid #ddd;
}

以下是指向Bootstrap's CSS master repo中的行的链接。


注意:

确保您的CSS规则比Bootstrap更具体,或者在本地的Bootstrap CSS副本中覆盖它们。