Bootstrap center heading

Twitter BootstrapBootstrap 4Html Heading

Twitter Bootstrap Problem Overview


It is my first Twitter Bootstrap experience. I have added some headings (h1, h2, etc.) and they are aligned by left side. What is the right way to center headings?

Twitter Bootstrap Solutions


Solution 1 - Twitter Bootstrap

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

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

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

bootstrap has added three css classes for text align.

Solution 2 - Twitter Bootstrap

Just use class='text-center' in <h> element for center heading:

<h2 class="text-center">sample center heading</h2>

Use class='text-left' in <h> element for left heading, and use class='text-right' in <h> element for right heading.

Solution 3 - Twitter Bootstrap

Just use "justify-content-center" in the row's class attribute.

<div class="container">
  <div class="row justify-content-center">
    <h1>This is a header</h1>
  </div>
</div>

Solution 4 - Twitter Bootstrap

Per your comments, to center all headings all you have to do is add text-align:center to all of them at the same time, like so:

CSS

    h1, h2, h3, h4, h5, h6 {
        text-align: center;
    }

Solution 5 - Twitter Bootstrap

Bootstrap comes with many pre-build classes and one of them is class="text-left". Please call this class whenever needed. :-)

Solution 6 - Twitter Bootstrap

In the current version of bootstrap 5.2.0 it is: text-center, (center) text-end, (right) text-start, (left).

<p class="text-start">Start aligned text on all viewport sizes.</p>
<p class="text-center">Center aligned text on all viewport sizes.</p>
<p class="text-end">End aligned text on all viewport sizes.</p>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSiberianGuyView Question on Stackoverflow
Solution 1 - Twitter BootstrapJinpu HuView Answer on Stackoverflow
Solution 2 - Twitter BootstrapSadegh-khanView Answer on Stackoverflow
Solution 3 - Twitter BootstrapJufrenchView Answer on Stackoverflow
Solution 4 - Twitter BootstrapAndres IlichView Answer on Stackoverflow
Solution 5 - Twitter BootstrapTinsaeView Answer on Stackoverflow
Solution 6 - Twitter BootstrapLenin SantiagoView Answer on Stackoverflow