Center Contents of Bootstrap row container

Twitter BootstrapCenter

Twitter Bootstrap Problem Overview


I have the code below and I'd like to center the "well" elements of the outer "row" div. I've tried various approaches such as text-align: center (which just centers the text and not the inner DIV elements.

Here is a jsfiddle. The idea is that I get a page of "well" tiles and that after 4 or so it wraps. But if it is less than 4, they should appear centered on the page.

<body class="container-fluid">
	<div class="row">
		<div class="well span2">
	        <div class="row">
	            <div class="span2">
	                Header
	            </div>
	        </div>
	        <div class="row">
	            <div class="span2">
	                Sub Header
	            </div>
	        </div>

	    </div>

	    <div class="well span2">
	        <div class="row">
	            <div class="span2">
	                Header
	            </div>
	        </div>
	        <div class="row">
	            <div class="span2">
	                Sub Header
	            </div>
			</div>
		</div>
	</div>
</body>

Twitter Bootstrap Solutions


Solution 1 - Twitter Bootstrap

With Bootstrap 4, there is a css class specifically for this. The below will center row content:

<div class="row justify-content-center">
  ...inner divs and content...
</div>

See: https://v4-alpha.getbootstrap.com/layout/grid/#horizontal-alignment, for more information.

Solution 2 - Twitter Bootstrap

I solved this by doing the following:

<body class="container-fluid">
    <div class="row">
        <div class="span6" style="float: none; margin: 0 auto;">
           ....
        </div>
    </div>
</body>

Solution 3 - Twitter Bootstrap

For Bootstrap 4, use the below code:

<div class="mx-auto" style="width: 200px;">
  Centered element
</div>

Ref: https://getbootstrap.com/docs/4.0/utilities/spacing/#horizontal-centering

Solution 4 - Twitter Bootstrap

Try this, it works!

<div class="row">
    <div class="center">
        <div class="col-xs-12 col-sm-4">
            <p>hi 1!</p>
        </div>
        <div class="col-xs-12 col-sm-4">
            <p>hi 2!</p>
        </div>
        <div class="col-xs-12 col-sm-4">
            <p>hi 3!</p>
        </div>
    </div>
</div>

Then, in css define the width of center div and center in a document:

.center {
    margin: 0 auto;
    width: 80%;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGreggView Question on Stackoverflow
Solution 1 - Twitter BootstrapAyo IView Answer on Stackoverflow
Solution 2 - Twitter BootstrapGreggView Answer on Stackoverflow
Solution 3 - Twitter BootstrapFakhruddin UjjainwalaView Answer on Stackoverflow
Solution 4 - Twitter BootstrapEzequielView Answer on Stackoverflow