The goal here is to center a column in bootstrap. Bootstrap is a free and open-source CSS framework directed at responsive, mobile-first front-end web development. There are two approaches to centering a column <div> in Bootstrap.
Approach 1 (offsets):
The first approach uses bootstrap offset class. The key is to set an offset equal to half of the remaining size of the row. So for example, a column of size 2 would be centered by adding an offset of 5, that’s (12-2)/2. The below example implements this.
html
<!DOCTYPE html>
< html >
< link rel = "stylesheet"
href =
integrity =
"sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin = "anonymous" />
< body >
< div class = "container" >
< div class = "row" >
< div style = "height: 200px;"
class="col-md-6 offset-md-3
text-center bg-success">.
col-md-6 .offset-md-3</ div >
</ div >
</ div >
</ body >
</ html >
|
Output

Approach 2(Margin auto):
Setting left and right margins to auto will center the div with respect to its parent element. The left and right margin can be set with .ml-auto and .mr-auto class respectively. The below example implements this.
html
<!DOCTYPE html>
< html >
< link rel = "stylesheet"
href =
integrity =
"sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin = "anonymous" />
< body >
< div class = "container" >
< div class = "row" >
< div style = "height: 200px;"
class="mr-auto ml-auto
text-center bg-success">
.ml-auto .mr-auto</ div >
</ div >
</ div >
</ body >
</ html >
|
Output

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
05 Nov, 2021
Like Article
Save Article