The loading center of all columns in an entire row

I am currently creating a webpage with Bootstrap and I am using columns. My page looks like this:

My page

I would like to center the last column (in the second row), but the page is dynamic, and I don't know how many containers there are.

I found these two solutions on Google:

1) Add this to my css:

.col-centered{ float: none; margin: 0 auto; } 

2) Add this to the class tag attribute

 col-lg-offset-4 


But both solutions look like this:

My page

This is not what I want. I want it to look like this:

enter image description here


How can I achieve this?

+6
source share
3 answers

Bootstrap columns float by default using the css float property. With float we cannot align average columns. However, with display: inline-block we can. We only need to remove the float from the column styles and change them to inline-block using vertical-align: middle , and you get what you want. But remember to remove the extra space that comes with the inline-block .

Here is the trick.

 .wrapper { background: green; padding: 20px 0; } .box { border-radius: 10px; margin-bottom: 30px; background: #fff; padding: 10px; color: #000; } .center-align { letter-spacing: -4px; text-align: center; font-size: 0; } .center-align [class*='col-'] { display: inline-block; vertical-align: top; letter-spacing: 0; font-size: 14px; float: none; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <div class="wrapper"> <div class="container center-align"> <div class="row"> <div class="col-xs-4"> <div class="box"> <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p> </div> </div> <div class="col-xs-4"> <div class="box"> <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p> </div> </div> <div class="col-xs-4"> <div class="box"> <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p> </div> </div> <div class="col-xs-4"> <div class="box"> <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p> </div> </div> </div> </div> <div class="container center-align"> <div class="row"> <div class="col-xs-4"> <div class="box"> <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p> </div> </div> <div class="col-xs-4"> <div class="box"> <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p> </div> </div> <div class="col-xs-4"> <div class="box"> <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p> </div> </div> <div class="col-xs-4"> <div class="box"> <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p> </div> </div> <div class="col-xs-4"> <div class="box"> <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p> </div> </div> </div> </div> </div> 

Note. Setting the font size: 0; letter spacing: -4px on the parent and applying the parent font size: 14px; letter spacing: 0 back to children removes the empty space that comes with the built-in block.

+3
source

You need to add the last block of text to another line and change "col-md-4" to "col-md-12".

 <div class="row"> <div class="col-md-4"> // column 1 bla bla bla </div> <div class="col-md-4"> //column 2 bla bla bla </div> <div class="col-md-4"> // column 3 bla bla bla </div> </div> <div class="row"> <center> <div class="col-md-12"> //last column, also note I changed it to 12 bla bla bla </div> </center> </div> 
+1
source

Bootstrap has built-in functions to achieve the layout you use without introducing additional CSS rules. Just use the .col-md-offset-* :

Move the columns to the right using the .col-md-offset-* . These classes increase the left margin of the column by * columns. For example, .col-md-offset-4 moves .col-md-4 into four columns.

Your layout will look something like this:

 .show-grid { margin-bottom: 15px; } .your-custom-div { height: 50px; background-color: green; color: white; text-align: center; } 
 <!DOCTYPE html> <html lang="en"> <head> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <div class="container"> <div class="row show-grid"> <div class="col-md-4"> <div class="your-custom-div"> .col-md-4 </div> </div> <div class="col-md-4"> <div class="your-custom-div"> .col-md-4 </div> </div> <div class="col-md-4"> <div class="your-custom-div"> .col-md-4 </div> </div> <div class="clearfix visible-md"></div> </div> <div class="row show-grid"> <div class="col-md-4 col-md-offset-4"> <div class="your-custom-div"> .col-md-4 .col-md-offset-4 </div> </div> <div class="clearfix visible-md"></div> </div> </div> </body> </html> 

EDIT # 1: for your requirement not to know how many columns you select from your database for the second row, another option would be to use a conditional expression during HTML output to output the .col-md-offset-4 class if modulo the number of items in your collection divided by the number of columns is 1 or, as usual, otherwise. In ASP.NET with Razor, it would look something like this (the example below is just simple to demonstrate the proposed logic, it can be reorganized into its own HTML helper class, taking into account other column sizes):

 @{ bool lastItemShouldBeCentered = Foo.Count % 3 == 1; for (int i = 0; i < Foo.Count; i++) { bool isLastItem = i == Foo.Count - 1; if (isLastItem && lastItemShouldBeCentered) { <div class="col-md-4 col-md-offset-4"> // Foo[i] content here </div> } else { <div class="col-md-4"> // Foo[i] content here </div> } } } 

EDIT No. 2: Looks like I misunderstood your requirement. For 1 left column this solution will be enough. Moreover, I would go with @Muhammad's answer.

+1
source

All Articles