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?
Bootstrap center heading
Asked Answered
.text-left {
text-align: left;
}
.text-right {
text-align: right;
}
.text-center {
text-align: center;
}
bootstrap has added three css classes for text align.
bootstrap needs to make this more clear on the docs..( 150 likes and counting = poor docs. ) –
Wilfredwilfreda
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.
I tried this and it didn't work for me. –
Getz
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>
Why all this complexity? Is not
<h1 class="text-center">
just more simple, as per the Sadegh-khan's answer? –
Scoville Using this allows the header to become centralised in the div when using bootstrap which using text-center didn't do. It makes the header appear in the middle of the screen which was what I was aiming for. –
Belaud
This helps you to center de
<tag>
and not the text –
Manon 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;
}
While true, the idea behind using twitter bootstrap is to use the existing classes as far as possible. See the answer by Jinpu Hu –
Ballerina
@Ballerina Frameworks function as suggestions towards what you can use in a project; They are by no means a given. While simpler to use the classes and styles already given in the bootstrap, if your project's requirements "require" you to make some drastic changes to the framework, per OP's question, than the simplest way to go about it is to just modify those changes at the root level. As opposed to adding a tonne of classes that you really don't need. –
Calling
@Ballerina Furthermore, those alignment classes did not exist at the time of writing. This answer is a year old. –
Calling
Bootstrap comes with many pre-build classes and one of them is class="text-left"
. Please call this class whenever needed. :-)
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>
© 2022 - 2024 — McMap. All rights reserved.
p
tag, or loose? – Calling