html - 将 th 文本与 margin auto 居中对齐不适用

标签 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>

最佳答案

在您想要居中的th上添加text-center类的 Bootstrap

@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;
}
<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>

关于html - 将 th 文本与 margin auto 居中对齐不适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50921200/

相关文章:

html - 单击时选项卡背景和字体颜色会发生变化

css - 响应式透明重叠 css 圆圈,文本垂直居中对齐

html - 当浏览器窗口缩小时,Bootstrap 网页无法正确显示

jquery - 使用 @font-face 动态添加 CSS

CSS 左浮动坐在彼此之上

html - 如何消除表格和 tr 之间的间隙

html - 调整 Bootstrap 下拉列表项中多个图标或文本之间的间距

html - div 旁边的图像 - float 重叠

jquery css() - 将属性设置为类集合中的一个元素

css - 如何使用 Bootstrap 4 flexbox 填充可用内容?