Bootstarp 4 has introduced classes to align div or list to horizontal, vertical center to it’s parent. Do you have a list inside a div and you wish to vertically align center to list items inside the parent div? And want to maintain vertical centered position as well as horizontal centering in Bootstrap when viewport size changes? I described here how Bootstrap and Non BS users can accomplish this alignment.
For more explanation, check the top navigation bar at our blog & try resizing screen. The social sharing icons on left always stay to center, both horizontally and vertically. Which classes did I use? Or what to do if you’re not using Bootstrap?
Align Div or List to Horizontal, Vertical Center to Parent
The text-center class in Bootstrap aligns content horizontally but not vertically. Also it’s not made to do the same if content are in a div or list where the div or list is needed to position center. So check a typical structure of Bootstrap markup.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <div class="container"> <div class="row"> <div class="col-sm-6"> <div id="align-content" class="d-flex justify-content-center align-items-center"> <ul class="list-inline mb-0"> <li class="list-inline-item px-2">One</li> <li class="list-inline-item px-2">One</li> <li class="list-inline-item px-2">One</li> <li class="list-inline-item px-2">One</li> </ul> </div> </div> <div class="col-sm-6"> <h4>Here is something large</h4> <p class="mb-0">In this paragraph I don't know how much height will these content might have. I just want to align the menu items in need-align to vertical and horizontal center.</p> </div> </div> </div> |
Here you notice, I have two columns and last columns might have more content than the first one. I need to make unordered list in first column to be horizontal, vertical center to it’s parent having id align-content
. Now notice line #5 in code above. It’s already done there as needed.
In line #5, the class d-flex is rendering the div as flexbox rather than a block. justify-content-center class is making flexbox items in parent to place themselves as center in horizontal and align-items-center is used to center it vertically. All the classes given in Bootstrap 4 flexbox utility and you can read about them by clicking link.
Also a little properties are needed to assign the parent div to maintain responsive behavior.
1 2 3 4 | .align-content { height: 100%; min-height: 40px; } |
The parent div here referred, would have minimum 40px
height in all case (useful when columns are stacked vertically). And 100% height property is needed to let it work.
If you’re not using Bootstrap, the horizontal, vertical center alignment classes used are given here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | .d-flex { display: -webkit-box!important; display: -webkit-flex!important; display: -ms-flexbox!important; display: flex!important; } .justify-content-center { -webkit-box-pack: center!important; -webkit-justify-content: center!important; -ms-flex-pack: center!important; justify-content: center!important; } .align-items-center { -webkit-box-align: center!important; -webkit-align-items: center!important; -ms-flex-align: center!important; align-items: center!important; } .list-inline { padding-left: 0; list-style: none; } .mb-0 { margin-bottom: 0!important; } .list-inline-item { display: inline-block; } .list-inline-item:not(:last-child) { margin-right: 5px; } .px-2 { padding-right: .5rem!important; padding-left: .5rem!important; } |
Al through this post is Bootstrap 4 specific and flexbox classes are given for non BS users too. Still you can also accomplish the same horizontal, vertical center to parent div using classes provided in the article given here.
align-content is ID, not a class (adjustment for a parent div):
#align-content {
height: 100%;
min-height: 40px;
}