Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering Table Header Text In Bootstrap

I am trying to center the text in my table header to align with the text below. I just cannot seem to get the header to align.

<div class="container">
<div class="text-center">
    <h2>Lorem Ipsum</h2>
    <p>Lorem Ipsum
    </p>
    <div class="table-responsive">
        <table class="table table-striped">
        <thead>
        <tr>
            <th>Table Header 1
            </th>
            <th>Table Header 2
            </th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>Lorem Ipsum
            </td>
            <td>Lorem Ipsum
            </td>
        </tr>
        </tbody>
        </table>
    </div>
</div>

like image 786
TDMatter Avatar asked Oct 23 '17 15:10

TDMatter


1 Answers

class="text-center" will work for this but strangely enough you need to target the <th> elements directly instead of placing it on the <tr> parent element.

According to this question, the following is why text-center does not affect <thead> when applied to the <table> element:

We set th { text-center: left; }, which is more specific than the class on the parent and thus prevents the header cell from inheriting the centering styles. Putting the .text-center class directly on the <th> should work though (confirmed in the docs).

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="text-center">
    <h2>Lorem Ipsum</h2>
    <p>Lorem Ipsum
    </p>
    <div class="table-responsive">
      <table class="table table-striped table-bordered">
        <thead>
          <tr>
            <th class="text-center">Table Header 1
            </th>
            <th class="text-center">Table Header 2
            </th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Lorem Ipsum
            </td>
            <td>Lorem Ipsum
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
like image 200
Master Yoda Avatar answered Sep 29 '22 09:09

Master Yoda