使用margin auto将文本对齐到中心不适用

时间:2018-06-19 05:31:50

标签: html css twitter-bootstrap

我有下表,我正试图将th的文字居中。

<table id="tblProgressSUmmary" class="table table-hover table-striped table-bordered table-condensed responsive dataTable no-footer dtr-inline collapsed" role="grid" aria-describedby="tblProgressSUmmary_info" style="width: 2082px;">
    <thead>
        <tr role="row">
        <th class="sorting_asc" tabindex="0" aria-controls="tblProgressSUmmary" rowspan="1" colspan="1" style="width: 52px;" aria-sort="ascending" aria-label="Running Track: activate to sort column descending">Running Track</th>
        <th class="sorting" tabindex="0" aria-controls="tblProgressSUmmary" rowspan="1" colspan="1" style="width: 66px;" aria-label="Exc Type: activate to sort column ascending">Exc Type</th><th class="sorting" tabindex="0" aria-controls="tblProgressSUmmary" rowspan="1" colspan="1" style="width: 88px;" aria-label="Progress Quantity: activate to sort column ascending">Progress Quantity</th>
        <th class="sorting" tabindex="0" aria-controls="tblProgressSUmmary" rowspan="1" colspan="1" style="width: 88px;" aria-label="Avg Duration: activate to sort column ascending">Avg Duration</th>
        </tr>
    </thead>
    <tbody>
        <tr class="odd">
        <td valign="top" colspan="10" class="dataTables_empty">No data available in table</td>
        </tr>
    </tbody>
</table>

和css:

#tblCIPSUmmary  > thead {
    font-size:11px;
    letter-spacing:1px;
    margin:auto;
}

tr {
    margin:auto;
}

段:

@import url('https://fonts.googleapis.com/css?family=Alegreya+Sans|Poppins|Roboto+Mono');
body {
  padding-top: 50px;
  padding-bottom: 20px;
}

.body-content {
  padding-left: 15px;
  padding-right: 15px;
}

.dl-horizontal dt {
  white-space: normal;
}

input[type="text"],
input[type="password"],
input[type="email"],
input[type="tel"],
input[type="select"] {
  max-width: 280px;
}

@media screen and (min-width: 768px) {
  .jumbotron {
    margin-top: 20px;
  }
  .body-content {
    padding: 0;
  }
}

.dataTable {
  background: #ffffff;
  border-color: #e7eaec;
  border-image: none;
  border-style: solid solid none;
  color: #676a6c;
  font-family: 'Roboto Mono', monospace;
}

body {
  font-size: 13px;
  color: #676a6c;
  overflow-x: hidden;
}

#tblCIPSUmmary>thead {
  font-size: 11px;
  letter-spacing: 1px;
  margin: auto;
}

tr {
  margin: auto;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />


<table id="tblProgressSUmmary" class="table table-hover table-striped table-bordered table-condensed responsive dataTable no-footer dtr-inline collapsed" role="grid" aria-describedby="tblProgressSUmmary_info" style="width: 2082px;">
  <thead>
    <tr role="row">
      <th class="sorting_asc" tabindex="0" aria-controls="tblProgressSUmmary" rowspan="1" colspan="1" style="width: 52px;" aria-sort="ascending" aria-label="Running Track: activate to sort column descending">Running Track</th>
      <th class="sorting" tabindex="0" aria-controls="tblProgressSUmmary" rowspan="1" colspan="1" style="width: 66px;" aria-label="Exc Type: activate to sort column ascending">Exc Type</th>
      <th class="sorting" tabindex="0" aria-controls="tblProgressSUmmary" rowspan="1" colspan="1" style="width: 88px;" aria-label="Progress Quantity: activate to sort column ascending">Progress Quantity</th>
      <th class="sorting" tabindex="0" aria-controls="tblProgressSUmmary" rowspan="1" colspan="1" style="width: 88px;" aria-label="Avg Duration: activate to sort column ascending">Avg Duration</th>
    </tr>
  </thead>
  <tbody>
    <tr class="odd">
      <td valign="top" colspan="10" class="dataTables_empty">No data available in table</td>
    </tr>
  </tbody>
</table>

3 个答案:

答案 0 :(得分:5)

在您想要居中的text-center上添加th类引导程序

&#13;
&#13;
@import url('https://fonts.googleapis.com/css?family=Alegreya+Sans|Poppins|Roboto+Mono');

body {
  padding-top: 50px;
  padding-bottom: 20px;
}

.body-content {
  padding-left: 15px;
  padding-right: 15px;
}

.dl-horizontal dt {
  white-space: normal;
}

input[type="text"],
input[type="password"],
input[type="email"],
input[type="tel"],
input[type="select"] {
  max-width: 280px;
}

@media screen and (min-width: 768px) {
  .jumbotron {
    margin-top: 20px;
  }
  .body-content {
    padding: 0;
  }
}

.dataTable {
  background: #ffffff;
  border-color: #e7eaec;
  border-image: none;
  border-style: solid solid none;
  color: #676a6c;
  font-family: 'Roboto Mono', monospace;
}

body {
  font-size: 13px;
  color: #676a6c;
  overflow-x: hidden;
}

#tblCIPSUmmary>thead {
  font-size: 11px;
  letter-spacing: 1px;
  margin: auto;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />


<table id="tblProgressSUmmary" class="table table-hover table-striped table-bordered table-condensed responsive dataTable no-footer dtr-inline collapsed" role="grid" aria-describedby="tblProgressSUmmary_info" style="width: 2082px;">
  <thead>
    <tr role="row">
      <th class="sorting_asc text-center" tabindex="0" aria-controls="tblProgressSUmmary" rowspan="1" colspan="1" style="width: 52px;" aria-sort="ascending" aria-label="Running Track: activate to sort column descending">Running Track</th>
      <th class="sorting text-center" tabindex="0" aria-controls="tblProgressSUmmary" rowspan="1" colspan="1" style="width: 66px;" aria-label="Exc Type: activate to sort column ascending">Exc Type</th>
      <th class="sorting text-center" tabindex="0" aria-controls="tblProgressSUmmary" rowspan="1" colspan="1" style="width: 88px;" aria-label="Progress Quantity: activate to sort column ascending">Progress Quantity</th>
      <th class="sorting text-center" tabindex="0" aria-controls="tblProgressSUmmary" rowspan="1" colspan="1" style="width: 88px;" aria-label="Avg Duration: activate to sort column ascending">Avg Duration</th>
    </tr>
  </thead>
  <tbody>
    <tr class="odd">
      <td valign="top" colspan="10" class="dataTables_empty">No data available in table</td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

只需在代码中添加以下Css

  

thead tr th {   文本对齐:中心;   }

答案 2 :(得分:1)

Bootstrap有一个预定义的类名,用于居中对齐文本,您可以使用“ text-center ”类,以便您的元素与中心对齐。

.text-center{text-align:center;}