300

Is there any way to center html elements vertically or horizontally inside the main parents?

isherwood
  • 58,414
  • 16
  • 114
  • 157
itsme
  • 48,972
  • 96
  • 224
  • 345

13 Answers13

259

Update: while this answer was likely correct back in early 2013, it should not be used anymore. The proper solution uses offsets, like so:

class="mx-auto"

As for other users suggestion there are also native bootstrap classes available like:

class="text-center"
class="pagination-centered"
TylerH
  • 20,799
  • 66
  • 75
  • 101
itsme
  • 48,972
  • 96
  • 224
  • 345
  • 3
    Using `text-center` for layout is very bad practice. The proper way to align columns in Bootstrap is by using `col-XX-offset-Y` classes. http://getbootstrap.com/css/#grid-offsetting. This answer was likely right in 2013 but should not be used anymore today. – Niels Keurentjes Oct 12 '16 at 21:44
78

From the Bootstrap documentation:

Set an element to display: block and center via margin. Available as a mixin and class.

<div class="center-block">...</div>
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
m0000g
  • 913
  • 6
  • 6
56

If you are trying to center an image in a div, but the image won't center, this could describe your problem:

JSFiddle Demo of the problem

<div class="col-sm-4 text-center">
    <img class="img-responsive text-center" src="myPic.jpg" />
</div>

The img-responsive class adds a display:block instruction to the image tag, which stops text-align:center (text-center class) from working.

SOLUTION:

<div class="col-sm-4">
    <img class="img-responsive center-block" src="myPic.jpg" />
</div>

JSFiddle demo of solution

Adding the center-block class overrides the display:block instruction put in by using the img-responsive class. Without the img-responsive class, the image would center just by adding text-center class


Also, you should know the basics of flexbox and how to use it, since Bootstrap 4 now uses it natively.

Here is an excellent, fast-paced video tutorial by Brad Schiff

Here is a great cheat sheet

TylerH
  • 20,799
  • 66
  • 75
  • 101
cssyphus
  • 37,875
  • 18
  • 96
  • 111
37

In Bootstrap 4, the centering methods have changed.

Horizontal Center in Bootstrap 4

  • text-center is still used for display:inline elements
  • mx-auto replaces center-block to center display:flex children
  • offset-* or mx-auto can be used to center grid columns

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

For vertical centering in BS4 see https://stackoverflow.com/a/41464397

TylerH
  • 20,799
  • 66
  • 75
  • 101
Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
31

You may directly write into your CSS file like this:

.content {
  position: absolute;
  top: 50%;
  left:50%;
  transform: translate(-50%,-50%);
  }
<div class = "content" >
  
   <p> some text </p>
  </div>
TylerH
  • 20,799
  • 66
  • 75
  • 101
Kanat
  • 616
  • 1
  • 9
  • 24
  • 1
    This seems to ignore the fact that this is a Bootstrap question, so interested users will almost certainly have other Bootstrap-specific classes which cause this not to work. – TylerH Dec 16 '21 at 16:57
9

i use this

<style>
    html, body {
        height: 100%;
        margin: 0;
        padding: 0 0;
    }

    .container-fluid {
        height: 100%;
        display: table;
        width: 100%;
        padding-right: 0;
        padding-left: 0;
    }

    .row-fluid {
        height: 100%;
        display: table-cell;
        vertical-align: middle;
        width: 100%;
    }

    .centering {
        float: none;
        margin: 0 auto;
    }
</style>
<body>
    <div class="container-fluid">
        <div class="row-fluid">
            <div class="offset3 span6 centering">
                content here
            </div>
        </div>
    </div>
</body>
Ian Campbell
  • 2,678
  • 10
  • 56
  • 104
atabak
  • 193
  • 1
  • 4
  • 2
    Doesn't work in certain cases (see [comment on essentially the same answer](http://stackoverflow.com/questions/10088706/twitter-bootstrap-how-to-center-elements-horizontally-or-vertically#comment24195315_15853518) and doesn't make use of [Bootstrap-specific classes for centering](http://getbootstrap.com/css/#helper-classes-center). – Dan Dascalescu Oct 07 '14 at 03:25
8

for horizontaly centering you can have something like this:

.centering{
float:none;
margin:0 auto
}

 <div class="span8 centering"></div>
Jsalinas
  • 302
  • 2
  • 11
PersianMan
  • 2,779
  • 2
  • 17
  • 20
  • 5
    Does not work (at least not in current implementation), when directly used in an image: ``. Surrounding `img` with parent `div` and giving `.text-center` or `.pagination-centered` class to parent works like a charm. – trejder May 28 '13 at 10:56
  • 3
    to center an element with margin:0 auto, you need to add a width to that element. In the above example there is no width specified because span8 has a width in boobstrap but your image doesn't so will center horizontally that image inside the parent – Raul Oct 25 '13 at 04:37
  • 4
    This ignores the "Bootstrap" part of the question. – Dan Dascalescu Oct 07 '14 at 03:24
8

With Bootstrap 4 you can use flex-based HTML classes d-flex, justify-content-center, and align-items-center:

<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="d-flex justify-content-center align-items-center">
    <button type="submit" class="btn btn-primary">Create</button>
</div>

Source: Bootstrap 4.1

TylerH
  • 20,799
  • 66
  • 75
  • 101
alvseek
  • 381
  • 3
  • 11
5

I like this solution from a similar question:

Use bootstrap's text-center class on the actual table data <td> and table header <th> elements. So

<td class="text-center">Cell data</td> and <th class="text-center">Header cell data</th>

TylerH
  • 20,799
  • 66
  • 75
  • 101
xander-miller
  • 529
  • 5
  • 12
2

I discovered in Bootstrap 4 you can do this:

center horizontal is indeed text-center class

center vertical however using bootstrap classes is adding both mb-auto mt-auto so that margin-top and margin bottom are set to auto.

online Thomas
  • 8,864
  • 6
  • 44
  • 85
2

Bootstrap 4's use of flex properties solve this. You can use these classes:

<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="d-flex justify-content-center align-items-center">
    <button type="submit" class="btn btn-primary">Create</button>
</div>

These classes will center your content horizontally and vertically.

TylerH
  • 20,799
  • 66
  • 75
  • 101
taoufiqaitali
  • 460
  • 5
  • 10
0

for bootstrap4 vertical center of few items

d-flex for flex rules

flex-column for vertical direction on items

align-items-center for horizontal centering

justify-content-center for vertical centering

style='height: 300px;' must have for set points where center be calc or use h-100 class

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="d-flex flex-column align-items-center justify-content-center bg-secondary" style="
    height: 300px;
">
    <div class="p-2 bg-primary">Flex item</div>
    <div class="p-2 bg-primary">Flex item</div>
    <div class="p-2 bg-primary">Flex item</div>
</div>
Ramil Gilfanov
  • 552
  • 1
  • 8
  • 12
0

With Bootstrap 5 you can use my-auto to verticaly center inside a col

Joffrey Outtier
  • 880
  • 10
  • 9