【问题标题】:Moving table columns toward center of table, but not center aligned, in Bootstrap 3在 Bootstrap 3 中将表列移向表中心,但不居中对齐
【发布时间】:2020-07-14 05:57:34
【问题描述】:

我有两个使用 Bootstrap 3 的并排表格。唯一的问题是我希望将列“移入”到表格的中心,左右两端的间距相等(但是我不希望文本居中对齐)。

我可以通过将表格单元格包装在 <div> 中并给它们一些填充来做到这一点。但我不确定是否有更灵敏的方式来实现这一目标。

编辑:澄清一下,我知道我可以使用text-align: center 将列居中对齐,这会将内容移向中心。但我真的不希望文本居中。我只是想让内容向内移动。

下面是一个例子。表 1 在我添加任何填充之前。表 2 在第一个 <div> 中添加了填充。表 2 看起来更接近我想要的,第一列移向中心。但理想情况下,我希望两列的左侧和右侧有相等的空白。有什么方法可以在不滥用 CSS 或破坏 Bootstrap 响应能力的情况下实现这一目标?

td>div {
  padding-left: 35px;
}
<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>
<div class="container">
  <div class="row">
    <div class="col-sm-6">
      <table class="table table-striped table-condensed table-borderless">
        <thead>
          <tr>
            <th>Table 1</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>
              Some Field Name
            </td>
            <td>428 mg/L</td>
          </tr>
          <tr>
            <td>
              Some Field Name
            </td>
            <td>428 mg/L</td>
          </tr>
          <tr>
            <td>
              Some Field Name
            </td>
            <td>428 mg/L</td>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="col-sm-6">
      <table class="table table-striped table-condensed">
        <thead>
          <tr>
            <th>Table 2</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>
              <div>
                Some Field Name</div>
            </td>
            <td>
              <div>
                428 mg/L</div>
            </td>

          </tr>
          <tr>
            <td>
              <div>
                Some Field Name</div>
            </td>
            <td>
              <div>
                428 mg/L</div>
            </td>
          </tr>
          <tr>
            <td>
              <div>
                Some Field Name</div>
            </td>
            <td>
              <div>
                428 mg/L</div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</div>

【问题讨论】:

    标签: html css twitter-bootstrap-3


    【解决方案1】:

    使用文本对齐:居中;

        table,
    thead,
    tr,
    tbody,
    th,
    td {
      text-align: center;
    }
    
    .table td {
      text-align: center;
    }
    <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>
    <div class="container">
      <div class="row">
        <div class="col-sm-6">
          <table class="table table-striped table-condensed table-borderless">
            <thead>
              <tr>
                <th>Table 1</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>
                  Some Field Name
                </td>
                <td>428 mg/L</td>
              </tr>
              <tr>
                <td>
                  Some Field Name
                </td>
                <td>428 mg/L</td>
              </tr>
              <tr>
                <td>
                  Some Field Name
                </td>
                <td>428 mg/L</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="col-sm-6">
          <table class="table table-striped table-condensed">
            <thead>
              <tr>
                <th>Table 2</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>
                  <div>
                    Some Field Name</div>
                </td>
                <td>
                  <div>
                    428 mg/L</div>
                </td>
    
              </tr>
              <tr>
                <td>
                  <div>
                    Some Field Name</div>
                </td>
                <td>
                  <div>
                    428 mg/L</div>
                </td>
              </tr>
              <tr>
                <td>
                  <div>
                    Some Field Name</div>
                </td>
                <td>
                  <div>
                    428 mg/L</div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>

    【讨论】:

      【解决方案2】:

      如果你使用的是桌子

      {
          text-align: center; 
          vertical-align: middle;
      }
      

      最好做内容中心。

      但我真的建议你使用 Flex 来构建表格,它在样式和放置方面为你提供了很多选择。

      flex 上的表格示例:

          <div class="screening-table">
              <div class="flex table-heading">
                  <div class="min-width table-heading__cell heading__cell--1">Sample</div>
                  <div class="min-width table-heading__cell heading__cell--2">Target</div>
                  <div class="min-width table-heading__cell heading__cell--3">Target Form</div>
                  <div class="min-width table-heading__cell heading__cell--4">Target Amt.</div>
                  <div class="min-width table-heading__cell heading__cell--5">Supplement</div>
                  <div class="min-width table-heading__cell heading__cell--6">Purpose</div>
                  <div class="min-width table-heading__cell heading__cell--7">Expected Tracer Behavior/Explanation
                  </div>
              </div>
      
              <div class="table-all-body">
                  <div class="flex table-body">
                      <div class="flex min-width table-body__cell heading__cell--1">
                          <div class="more-icon">
                              <img src="images/screening/ic_more_vert_24px.png" alt="view more">
                          </div>
                          1
                      </div>
                      <div class="min-width table-body__cell heading__cell--2">NTRK4</div>
                      <div class="flex min-width table-body__cell heading__cell--3">
                          <div class="info-icon">
                              <img src="images/screening/ic_info_24px.png" alt="info">
                          </div>
                          <span>Avi-NTRK4(1-796) DP</span>
                      </div>
                      <div class="min-width table-body__cell heading__cell--4">250 pmol</div>
                      <div class="flex min-width table-body__cell heading__cell--5">
                          <div class="info-icon">
                              <img src="images/screening/ic_info_24px.png" alt="info"></div>
                          <span>None</span>
                      </div>
                      <div class="min-width table-body__cell heading__cell--6">
                          Identify small molecules that bind the non-phosphorylated full-length target
                      </div>
                      <div class="min-width table-body__cell heading__cell--7">Unclear whether this tracer binds
                          the
                          non-phosphorylated and phosphorylated NTRK4 with similar affinities
                      </div>
                  </div>
                  <div class="rectangle"></div>
      

      CSS:

      .screening-table {
          margin-top: 72px;
          // margin-left: 26px;
          // margin-top: 56px;
          margin-left: 246px;
      
          .min-width {
              min-width: 42px;
          }
      
          .table-heading {
              flex-direction: row;
          }
      
          .table-heading {
              background: $catsill-white;
              box-shadow: 0px -1px 0px $catsill-white, 0px 1px 0px $catsill-white;
              padding-top: 14px;
              padding-bottom: 14px;
          }
      
          .table-heading__cell {
              font-style: normal;
              font-weight: 500;
              font-size: 12px;
              line-height: 14px;
              margin-left: 14px;
              margin-right: 14px;
              color: $gull-gray;
          }
      
          .table-body {
              background: $primary-color;
              box-shadow: 0px 2px 6px rgba($text-color--black, 0.102874);
              border-radius: 3px;
          }
      
          .table-body__cell {
              margin-left: 14px;
              margin-right: 14px;
              padding-top: 20px;
              padding-bottom: 10px;
      
              font-style: normal;
              font-weight: normal;
              font-size: 16px;
              line-height: 19px;
      
              color: $plantation;
      
          }
      
          .info-icon {
              width: 24px;
              height: 24px;
          }
      
          .more-icon {
              margin-left: 8px;
          }
      
          .rectangle {
              border: 1px solid $catsill-white;
          }
      
      
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2019-05-01
        • 1970-01-01
        • 2014-04-30
        • 1970-01-01
        • 2020-01-31
        • 2013-11-27
        • 2017-11-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多