Bootstrap Center Vertical and Horizontal Alignment
I have a page where only form exists and I want form to be placed in the center of the screen.
<div class="container">
<div class="row justify-content-center align-items-center">
<form>
<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>
The justify-content-center
aligns the form horizontally, but I can't figure out how to align it vertically. I have tried to use align-items-center
and align-self-center
, but it doesn't work.
What am I missing?
DEMO
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-
) insiderow
-
align-items-center
to center columns (col-*
) insiderow
Horizontal center:
-
text-center
to centerdisplay:inline
elements & column content -
mx-auto
for centering inside flex elements -
mx-auto
can be used to center columns (.col-
) insiderow
-
justify-content-center
to center columns (col-*
) insiderow
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 centerdisplay:inline
elements & column content -
mx-auto
for centering inside flex elements -
offset-*
ormx-auto
can be used to center columns (.col-
) -
justify-content-center
to center columns (col-*
) insiderow
Vertical Align Center in Bootstrap
Bootstrap 4 full-screen centered form
Bootstrap 4 center input group
Bootstrap 4 horizontal + vertical center full screen
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>
flexbox can help you. info here
<div class="d-flex flex-row justify-content-center align-items-center" style="height: 100px;">
<div class="p-2">
1
</div>
<div class="p-2">
2
</div>
</div>
Bootstrap has text-center to center a text. For example
<div class="container text-center">
You change the following
<div class="row justify-content-center align-items-center">
to the following
<div class="row text-center">
You need something to center your form into. But because you didn't specify a height for your html and body, it would just wrap content - and not the viewport. In other words, there was no room where to center the item in.
html, body {
height: 100%;
}
.container, .row.justify-content-center.align-items-center {
height: 100%;
min-height: 100%;
}