Centering text in a table in Twitter Bootstrap

后端 未结 10 1788
予麋鹿
予麋鹿 2020-12-12 15:48

For some reason, The text inside the table still is not centered. Why? How do I center the text inside the table?

To make it really Clear: For example, I want "L

相关标签:
10条回答
  • 2020-12-12 16:44

    In Bootstrap 3 (3.0.3) adding the "text-center" class to a td element works out of the box.

    I.e., the following centers some text in a table cell:

    <td class="text-center">some text</td>
    
    0 讨论(0)
  • 2020-12-12 16:45

    just give the surrounding <tr> a custom class like:

    <tr class="custom_centered">
      <td>1</td>
      <td>2</td>
      <td>3</td>
    </tr>
    

    and have the css only select <td>s that are inside an <tr> with your custom class.

    tr.custom_centered td {
      text-align: center;
    }
    

    like this you don't risk to override other tables or even override a bootstrap base class (like some of my predecessors suggested).

    0 讨论(0)
  • 2020-12-12 16:48

    add:

    <p class="text-center"> Your text here... </p>
    
    0 讨论(0)
  • 2020-12-12 16:50

    The .table td 's text-align is set to left, rather than center.

    Adding this should center all your tds:

    .table td {
       text-align: center;   
    }
    

    @import url('https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css');
    table,
    thead,
    tr,
    tbody,
    th,
    td {
      text-align: center;
    }
    
    .table td {
      text-align: center;
    }
    <table class="table">
      <thead>
        <tr>
          <th>1</th>
          <th>1</th>
          <th>1</th>
          <th>1</th>
          <th>2</th>
          <th>2</th>
          <th>2</th>
          <th>2</th>
          <th>3</th>
          <th>3</th>
          <th>3</th>
          <th>3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td colspan="4">Lorem</td>
          <td colspan="4">ipsum</td>
          <td colspan="4">dolor</td>
        </tr>
      </tbody>
    </table>

    Updated JSFIDDLE

    0 讨论(0)
提交回复
热议问题