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?
Update 2018:
In Bootstrap 4.1.3, content can be centered using class
mx-auto
.Reference: https://getbootstrap.com/docs/4.1/utilities/spacing/#horizontal-centering
Update 2018 - 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 columnsDemo 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 centerdisplay:block
ordisplay: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, andmy-auto
will vertically center thecol-sm-12
column.Vertical Center Using Auto Margins Demo
my-auto
represents margins on the vertical y-axis and is equivalent to:2 - Vertical Center with Flexbox:
Since Bootstrap 4
.row
is nowdisplay:flex
you can simply usealign-self-center
on any column to vertically center it...or, use
align-items-center
on the entire.row
to vertically center align allcol-*
in the row...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.Vertical Center Using Display Utils Demo
Any text-align utility class would do the trick. Bootstrap has been using
.text-center
class for centering text for a long time now.Or you can create your own utilities. Some variations I've seen over the years:
I created this class to keep the centered regardless of screen size while maintaining responsive features:
The best way to do this is define a css style:
Then where ever you need centered text you add it like so:
or if you just want the p tag centered:
The less inline css you use the better.
On my side I define new
CSS
styles ready to use and easy to remember:Is it a good idea? Is there any good practice for this?