Bootstrap 4:

<div class=" h-100 d-flex justify-content-center align-items-center">
  <div>
      Items are Centered horizontally and vertically
  </div>
</div>

JsFiddle

Answer from speti43 on Stack Overflow
Top answer
1 of 9
40

Bootstrap 4:

<div class=" h-100 d-flex justify-content-center align-items-center">
  <div>
      Items are Centered horizontally and vertically
  </div>
</div>

JsFiddle

2 of 9
17

Some of the other answers on this question use CSS hacks with tables and custom CSS classes. As the poster asked "How to center vertically and horizontally using Bootstrap", here is how to do that using only Bootstrap 4 utility classes:

<div class="d-flex justify-content-md-center align-items-center vh-100">
    <p>Your Content</p>
</div>

Something of note is that due to the styling on the parent div, when adding additional elements in the same div, they will appear beside the first one, rather than below it. To fix this, just add an additional div inside the parent to reset the styling.

<div class="d-flex justify-content-md-center align-items-center vh-100">
    <div>
        <p>Content 1</p>
        <p>Content 2</p>
    </div>
</div>

This does work with Bootstrap flex, I've found that it works best when placed inside a flex component like this, rather than wrapping the entire row.

<div class="container">
    <div class="row">
        <div class="col-md-6">
            <div class="d-flex justify-content-md-center align-items-center vh-100">
                 <p>Content 1</p>
            </div>
        </div>
        <div class="col-md-6">
            <div class="d-flex justify-content-md-center align-items-center vh-100">
                 <p>Content 2</p>
            </div>
        </div>
    </div>
</div>

Here is a breakdown of each class:

  1. d-flex: Effectively display: flex, allows the div to grow or shrink depending on the amount of content.
  2. justify-content-md-center: Justifies content in the center of the page, can also be replaced with justify-content-sm-center or justify-content-lg-center to change the breakpoint.
  3. align-items-center: Centers the alignments of all items in a div.
  4. vh-100: Sets the height of the div to 100vh, or 100 "vertical height". This ensures that the div is the correct height to allow for vertical alignment.
🌐
MDB
mdbootstrap.com › standard › how to center div vertically and horizontally
How to center div vertically and horizontally - code helpers
Add .align-items-center to the parent element to center its content vertically. Add .justify-content-center to the parent element to center its content horizontally. ... <div class="d-flex align-items-center justify-content-center" style="height: ...
Discussions

css - Bootstrap: Centering Elements Vertically and Horizontally - Stack Overflow
Vertical Align Center in Bootstrap Bootstrap 4 full-screen centered form Bootstrap 4 center input group Bootstrap 4 horizontal + vertical center full screen ... Sign up to request clarification or add additional context in comments. ... Is it OK to use the tag, or should one stick to More on stackoverflow.com
🌐 stackoverflow.com
Vertical and horizontal text align bootstrap
I have the following below: 1 2 3 The text (1, 2, 3) are centered horizontally, but not vertically. Is there a way for me to center the texts both vertically and horizontally at the same tim... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
January 25, 2021
Center div in div horizontally - Tips and Tricks - Bootstrap Studio Forum
I would like to achieve the following: I would like to place text over an image, in such a way that the text is centered on image both horizontally and vertically. I tried this with Flexbox, but failed miserably. Who can help me? What is the easiest way to do this? Is the Flex module the right one? More on forum.bootstrapstudio.io
🌐 forum.bootstrapstudio.io
0
October 11, 2021
css - Vertical Align Center in Bootstrap 4 - Stack Overflow
Option 1 only worked for me when ... to 2020-11-06T11:57:25.443Z+00:00 ... That's correct since h-100 is 100% of its parent container while vh-100 is height of the viewport. At the time the question was asked,vh-100 didn't exist in Bootstrap. 2020-11-06T12:03:51.417Z+00:00 ... Using bootstrap version 5 or later, use the code below to center the content horizontally and vertically... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › bootstrap › how-to-center-an-element-vertical-and-horizontal-alignment-in-bootstrap-5
How to Center an Element Vertical and Horizontal Alignment in Bootstrap 5 ? - GeeksforGeeks
July 23, 2025 - Bootstrap 5 provides a range of utility classes that make it easy to center elements. These classes include d-flex, align-items-center, justify-content-center, and more. <div class="d-flex align-items-center justify-content-center"> <!-- Content --> </div>
🌐
GitHub
github.com › mdbootstrap › bootstrap-how-to-center-div-vertically-and-horizontally
GitHub - mdbootstrap/bootstrap-how-to-center-div-vertically-and-horizontally: Learn how you can simply center div vertically and horizontally in your Bootstrap 5 project. · GitHub
Add .align-items-center to the parent element to center its content vertically. Add .justify-content-center to the parent element to center its content horizontally. <div class="d-flex align-items-center justify-content-center" style="height: ...
Author   mdbootstrap
🌐
Bootstrap
getbootstrap.com › docs › 5.0 › utilities › vertical-align
Vertical alignment · Bootstrap v5.0
Choose from .align-baseline, .align-top, .align-middle, .align-bottom, .align-text-bottom, and .align-text-top as needed. To vertically center non-inline content (like <div>s and more), use our flex box utilities.
🌐
ShapeBootstrap
shapebootstrap.net › home › mastering div centering in bootstrap 5
How to Center a Div in Bootstrap 5: A Comprehensive Guide
July 11, 2023 - The most common way to center a div in Bootstrap 5 is to append a parent element with the “d-flex” and “justify-content-center” classes and a child element with the “align-self-center” class.
Find elsewhere
🌐
Squash
squash.io › how-to-center-a-div-in-a-bootstrap-responsive-page
How to Center a Div in a Bootstrap Responsive Page
October 6, 2023 - The justify-content-center class horizontally centers the content, and the align-items-center class vertically centers the content. The height: 100vh CSS property ensures that the outer div takes up the full height of the viewport.
Top answer
1 of 16
539

Bootstrap 5 (Updated 2021)

Bootstrap 5 is still flexbox based so vertical centering works the same way as it did in Bootstrap 4. For example, align-items-center (flex-direction: row) and justify-content-center (flex-direction: column) can used on the flexbox parent (row or d-flex).

Centering examples in Bootstrap 5

Vertical center (don't forget the parent must have a defined height!):

  • my-auto for centering inside flex (.d-flex) elements
  • my-auto can be used to center columns (.col-) inside row
  • align-items-center to center columns (col-*) inside row

Horizontal center:

  • text-center to center display:inline elements & column content
  • mx-auto for centering inside flex elements
  • mx-auto can be used to center columns (.col-) inside row
  • justify-content-center to center columns (col-*) inside row

Bootstrap 4.3+ (Update 2019)

There's no need for extra CSS. What's already included in Bootstrap will work. Make sure the container(s) of the form are full height. Bootstrap 4 now has a h-100 class for 100% height...

Vertical center:

<div class="container h-100">
  <div class="row h-100 justify-content-center align-items-center">
    <form class="col-12">
      <div class="form-group">
        <label for="formGroupExampleInput">Example label</label>
        <input type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input">
      </div>
      <div class="form-group">
        <label for="formGroupExampleInput2">Another label</label>
        <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input">
      </div>
    </form>   
  </div>
</div>

https://codeply.com/go/raCutAGHre

the height of the container with the item(s) to center should be 100% (or whatever the desired height is relative to the centered item)

Note: When using height:100% (percentage height) on any element, the element takes in the height of it's container. In modern browsers vh units height:100vh; can be used instead of % to get the desired height.

Therefore, you can set html, body {height: 100%}, or use the new min-vh-100 class on container instead of h-100.


Horizontal center:

  • text-center to center display:inline elements & column content
  • mx-auto for centering inside flex elements
  • offset-* or mx-auto can be used to center columns (.col-)
  • justify-content-center to center columns (col-*) inside row

Vertical Align Center in Bootstrap
Bootstrap 4 full-screen centered form
Bootstrap 4 center input group
Bootstrap 4 horizontal + vertical center full screen

2 of 16
37

This work for me:

<section class="h-100">
  <header class="container h-100">
    <div class="d-flex align-items-center justify-content-center h-100">
      <div class="d-flex flex-column">
        <h1 class="text align-self-center p-2">item 1</h1>
        <h4 class="text align-self-center p-2">item 2</h4>
        <button class="btn btn-danger align-self-center p-2" type="button" name="button">item 3</button>
      </div>
    </div>
  </header>
</section>
🌐
freeCodeCamp
forum.freecodecamp.org › html-css
Vertical and horizontal text align bootstrap - HTML-CSS - The freeCodeCamp Forum
January 25, 2021 - I have the following below: <section class="m-5"> <div class="container"> <div class="row text-center justify-content-center" style="height:75px"> <div id="one" class="col-2 border ">1</div> <…
🌐
Bootstrap Studio Forum
forum.bootstrapstudio.io › tips and tricks
Center div in div horizontally - Tips and Tricks - Bootstrap Studio Forum
October 11, 2021 - I would like to achieve the following: I would like to place text over an image, in such a way that the text is centered on image both horizontally and vertically. I tried this with Flexbox, but failed miserably. Who ca…
🌐
Djangoandy
djangoandy.com › 2022 › 09 › 08 › how-to-center-items-horizontally-and-vertically-using-bootstrap-5
How to Center Items Horizontally and Vertically using Bootstrap 5 – Django Andy
And justify-content-center centre’s your item vertically. You have to use d-flex or it won’t work. There you have it! How to center items in a div this week! You’re welcome! ... Just for S&G’s, I Asked AI About My Career. Then It Got Real. Image Background Removal that doesn’t break the bank! Running LLM’s like ChatGPT locally – right on your laptop! Understanding APIs: A Simple Explanation for Non-Tech Folks ... AI API bootstrap cbv chatGPT Cloudflare css django django-extensions django-filter django-q django-rest-framework django-unicorn emails form HorseRecords html interview javascript jquery know what you are doing media mental health nginx optimising pipenv python QR codes radio Retool rich SaaS security security django-admin-honeypot sitemap snippets sqlite Startup success ubuntu user input uuid world of pain
🌐
AZMIND
azmind.com › home › tutorials › bootstrap 4: how to center align a “div” or “form” vertically and horizontally
Bootstrap 4: Align a Div or Form Vertically and Horizontally | AZMIND
June 11, 2019 - 1. First, we add the class “d-flex” to our “div-wrapper”. By doing this, we enable flex behaviors and create a flexbox container (our “div-wrapper” class), and also transform its direct children elements into flex items, as explained ...
🌐
CodePen
codepen.io › david263 › pen › eYvOGOB
Perfect horizontal and vertical centering in Bootstrap 5
<style type="text/css"> /* Required for proper centering */ html, body{ height:100vh; width:100vw; } </style> <!-- Outer container, full page width and height, red border --> <div class="container-fluid d-flex justify-content-center align-items-center" style="height:100vh; overflow:hidden; border: 2px solid red"> <!-- Inner row, half the width and height, centered, blue border --> <div class="row text-center d-flex align-items-center" style="overflow:hidden; width:50vw; height:50vh; border: 1px solid blue"> <!-- Innermost text, wraps automatically, automatically centered --> <h2>Center This Text (Even if Wrapped) in all Viewport Sizes</h2> </div> <!-- Inner row --> </div> <!-- Outer container -->
🌐
MDBootstrap
mdbootstrap.com › standard › vertical alignment
Bootstrap Vertical alignment - free examples & tutorial
Then use (alsso on the parent element) .align-items-center to align content vertically and .justify-content-center to align content horizontally. Example button · Show code Edit in sandbox · HTML · <div class="border d-flex align-items-center ...
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-vertical-align-a-div-in-bootstrap.php
How to Vertical Align a DIV in Bootstrap
In Bootstrap 4, if you want to vertically align a <div> element in the center or middle, you can simply apply the class align-items-center on the containing element.
🌐
Medium
medium.com › wdstack › bootstrap-4-vertical-center-1211448a2eff
Bootstrap 4 Vertical Center. How to vertically align anything | by Carol Skelly | WDstack | Medium
May 4, 2018 - my-auto represents margins on the vertical y-axis, and is equivalent to: ... Since the Bootstrap 4 .row class is now display:flex you can simply use the new align-self-center flexbox utility on any column to vertically center it: <div ...
Top answer
1 of 16
1058

Important! Vertical center is relative to the height of the parent

If the parent of the element you're trying to center has no defined height, none of the vertical centering solutions will work!

Now, onto vertical centering...

Bootstrap 5 (Updated 2021)

Bootstrap 5 is still flexbox based so vertical centering works the same way as Bootstrap 4. For example, align-items-center, justify-content-center or auto margins can used on the flexbox parent (row or d-flex).

  • use align-items-center on a flexbox row parent (row or d-flex)
  • use justify-content-center on a flexbox column parent (d-flex flex-column)
  • use my-auto on a flexbox parent

Vertical Center in Bootstrap 5


Bootstrap 4

You can use the new flexbox & size utilities to make the container full-height and display: flex. These options don't require extra CSS (except that the height of the container (ie:html,body) must be 100%).

Option 1 align-self-center on flexbox child

<div class="container d-flex h-100">
    <div class="row justify-content-center align-self-center">
     I'm vertically centered
    </div>
</div>

https://codeply.com/go/fFqaDe5Oey

Option 2 align-items-center on flexbox parent (.row is display:flex; flex-direction:row)

<div class="container h-100">
    <div class="row align-items-center h-100">
        <div class="col-6 mx-auto">
            <div class="jumbotron">
                I'm vertically centered
            </div>
        </div>
    </div>
</div>

https://codeply.com/go/BumdFnmLuk

Option 3 justify-content-center on flexbox parent (.card is display:flex;flex-direction:column)

<div class="container h-100">
    <div class="row align-items-center h-100">
        <div class="col-6 mx-auto">
            <div class="card h-100 border-primary justify-content-center">
                <div>
                    ...card content...
                </div>
            </div>
        </div>
    </div>
</div>

https://codeply.com/go/3gySSEe7nd


More on Bootstrap 4 Vertical Centering

Now that Bootstrap 4 offers flexbox and other utilities, there are many approaches to vertical alignment. http://www.codeply.com/go/WG15ZWC4lf

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

See this Q/A to center, but maintain equal height


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>

Vertical Center Using Display Utils Demo

More examples
Vertical center image in <div>
Vertical center .row in .container
Vertical center and bottom in <div>
Vertical center child inside parent
Vertical center full screen jumbotron


Important! Did I mention height?

Remember vertical centering is relative to the height of the parent element. If you want to center on the entire page, in most cases, this should be your CSS...

body,html {
  height: 100%;
}

Or use min-height: 100vh (min-vh-100 in Bootstrap 4.1+) on the parent/container. If you want to center a child element inside the parent. The parent must have a defined height.

Also see:
Vertical alignment in bootstrap 4
Bootstrap Center Vertical and Horizontal Alignment

2 of 16
56

Using bootstrap version 5 or later, use the code below to center the content horizontally and vertically.

<div class="vh-100 d-flex justify-content-center align-items-center">
  <h1>Centered horizontally and vertically!</h1>
</div>

🌐
Codeply
codeply.com › go › WG15ZWC4lf
Bootstrap 4 Vertical Center on Codeply
HTML, CSS, JavaScript editor playground for designers & developers to compare, prototype and test frontend frameworks