How do you get centered content using Twitter Bootstrap?
How do you get centered content using Twitter Bootstrap?
Question
I'm trying to follow a very basic example. Using the starter page and the grid system, I was hoping the following:
<div class="row">
<div class="span12">
<h1>Bootstrap starter template</h1>
<p>Example text.</p>
</div>
</div>
...would produce centered text.
However, it still appears on the far left. What am I doing wrong?
Accepted Answer
This is for Text Centering (which is what the question was about)
For other types of content, see Flavien's answer.
Update: Bootstrap 2.3.0+ Answer
The original answer was for an early version of bootstrap. As of bootstrap 2.3.0, you can simply give the div the class .text-center
.
Original Answer (pre 2.3.0)
You need to define one of the two classes, row
or span12
with a text-align: center
. See http://jsfiddle.net/xKSUH/ or http://jsfiddle.net/xKSUH/1/
Read more… Read less…
NOTE: this was removed in Bootstrap 3.
Pre-Bootstrap 3, you could use the CSS class pagination-centered
like this:
<div class="span12 pagination-centered">
Centered content.
</div>
Class pagination-centered
is already in bootstrap.css (or bootstrap.min.css) and has the only one rule:
.pagination-centered{text-align:center;}
With Bootstrap 2.3.0. just use class text-center
I guess most of the people here are actually searching for the way to center the whole div
and not only the text content (which is trivial…).
The second way (instead of using text-align:center) to center things in HTML is to have an element with a fixed width and auto margin (left and right). With Bootstrap, the style defining auto margins is the "container" class.
<div class="container">
<div class="span12">
"Centered stuff there"
</div>
</div>
Take a look here for a fiddle: http://jsfiddle.net/D2RLR/7788/
Update 2019 - Bootstrap 4
"Centered content" can mean many different things, and Bootstrap centering has changed a lot since the original post.
Horizontal Center
Bootstrap 3
text-center
is used fordisplay:inline
elementscenter-block
to centerdisplay:block
elementscol-*offset-*
to center grid columns- see this answer to center the navbar
Demo Bootstrap 3 Horizontal Centering
Bootstrap 4
text-center
is still used fordisplay:inline
elementsmx-auto
replacescenter-block
to centerdisplay:block
elementsoffset-*
ormx-auto
can be used to center grid columnsjustify-content-center
inrow
can also be used to centercol-*
mx-auto
(auto x-axis margins) will center display:block
or display:flex
elements that have a defined width, (%
, vw
, px
, etc..). Flexbox is used by default on grid columns, so there are also various flexbox centering methods.
Demo Bootstrap 4 Horizontal Centering
Vertical Center
Now that Bootstrap 4 is flexbox by default there are many different approaches to vertical alignment using: auto-margins, flexbox utils, or the display utils along with vertical align utils. At first "vertical align utils" seems obvious but these only work with inline and table display elements. Here are some Bootstrap 4 vertical centering options..
1 - Vertical Center Using Auto Margins:
Another way to vertically center is to use my-auto
. This will center the element within it's container. For example, h-100
makes the row full height, and my-auto
will vertically center the col-sm-12
column.
<div class="row h-100">
<div class="col-sm-12 my-auto">
<div class="card card-block w-25">Card</div>
</div>
</div>
Vertical Center Using Auto Margins Demo
my-auto
represents margins on the vertical y-axis and is equivalent to:
margin-top: auto;
margin-bottom: auto;
2 - Vertical Center with Flexbox:
Since Bootstrap 4 .row
is now display:flex
you can simply use align-self-center
on any column to vertically center it...
<div class="row">
<div class="col-6 align-self-center">
<div class="card card-block">
Center
</div>
</div>
<div class="col-6">
<div class="card card-inverse card-danger">
Taller
</div>
</div>
</div>
or, use align-items-center
on the entire .row
to vertically center align all col-*
in the row...
<div class="row align-items-center">
<div class="col-6">
<div class="card card-block">
Center
</div>
</div>
<div class="col-6">
<div class="card card-inverse card-danger">
Taller
</div>
</div>
</div>
Vertical Center Different Height Columns Demo
3 - Vertical Center Using Display Utils:
Bootstrap 4 has display utils that can be used for display:table
, display:table-cell
, display:inline
, etc.. These can be used with the vertical alignment utils to align inline, inline-block or table cell elements.
<div class="row h-50">
<div class="col-sm-12 h-100 d-table">
<div class="card card-block d-table-cell align-middle">
I am centered vertically
</div>
</div>
</div>
Bootstrap 3.1.1 has a .center-block
class for centering divs. See: http://getbootstrap.com/css/#helper-classes-center.
Center content blocks Set an element to
display: block
and center viamargin
. Available as a mixin and class.
<div class="center-block">...</div>
Or, as others have already said, use the .text-center
class to centre text.
The best way to do this is define a css style:
.centered-text {
text-align:center
}
Then where ever you need centered text you add it like so:
<div class="row">
<div class="span12 centered-text">
<h1>Bootstrap starter template</h1>
<p>Use this document as a way to quick start any new project.<br> All you get is this message and a barebones HTML document.</p>
</div>
</div>
or if you just want the p tag centered:
<div class="row">
<div class="span12 centered-text">
<h1>Bootstrap starter template</h1>
<p class="centered-text">Use this document as a way to quick start any new project.<br> All you get is this message and a barebones HTML document.</p>
</div>
</div>
The less inline css you use the better.