Align two divs horizontally side by side center to the page using bootstrap css

HtmlCssTwitter Bootstrap

Html Problem Overview


Please refer below code what i tried

<div class="row">
  <div class="center-block">First Div</div>
  <div class="center-block">Second DIV </div>
</div>

output :

First Div
SecondDiv

Expected output :

                      First Div        Second Div

i want to align the two divs horizontally center to page using bootstrap css. how can i do this ? i dont want to use simple css and floating concept to do this. because i need to make use of the bootstrap css to work in all kind of layouts (i.e. all window size and resolutions ) instead of using media query.

Html Solutions


Solution 1 - Html

This should do the trick:

<div class="container">
    <div class="row">
        <div class="col-xs-6">
            ONE
        </div>
        <div class="col-xs-6">
            TWO
        </div>
    </div>
</div>

Have a read of the grid system section of the Bootstrap docs to familiarise yourself with how Bootstrap's grids work:

http://getbootstrap.com/css/#grid

Solution 2 - Html

Use the bootstrap classes col-xx-# and col-xx-offset-#

So what is happening here is your screen is getting divided into 12 columns. In col-xx-#, # is the number of columns you cover and offset is the number of columns you leave.

For xx, in a general website, md is preferred and if you want your layout to look the same in a mobile device, xs is preferred.

With what I can make of your requirement,

<div class="row">
  <div class="col-md-4">First Div</div>
  <div class="col-md-8">Second DIV </div>
</div>

Should do the trick.

Solution 3 - Html

Alternate Bootstrap 4 solution (this way you can use divs which are smaller than col-6):

Horizontal Align Center

<div class="container">
  <div class="row justify-content-center">
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
</div>

More

Solution 4 - Html

To align two divs horizontally you just have to combine two classes of Bootstrap: Here's how:

<div class ="container-fluid">
  <div class ="row">
    <div class ="col-md-6 col-sm-6">
        First Div
    </div>
    <div class ="col-md-6 col-sm-6">
        Second Div
     </div>
  </div>
</div>

Solution 5 - Html

<div class="container">
    <div class="row">
       <div class="col-xs-6 col-sm-6 col-md-6">
          First Div
       </div>
       <div class="col-xs-6 col-sm-6 col-md-6">
          Second Div
       </div>
   </div>

This does the trick.

Solution 6 - Html

Alternative which I did programming Angular:

    <div class="form-row">
        <div class="form-group col-md-7">
             Left
        </div>
        <div class="form-group col-md-5">
             Right
        </div>
    </div>

Solution 7 - Html

The response provided by Ranveer (second answer above) absolutely does NOT work.

He says to use col-xx-offset-#, but that is not how offsets are used.

If you wasted your time trying to use col-xx-offset-#, as I did based on his answer, the solution is to use offset-xx-#.

Solution 8 - Html

I recommend css grid over bootstrap if what you really want, is to have more structured data, e.g. a side by side table with multiple rows, because you don't have to add class name for every child:

// css-grid: https://www.w3schools.com/css/tryit.asp?filename=trycss_grid
// https://css-tricks.com/snippets/css/complete-guide-grid/
.grid-container {
  display: grid;
  grid-template-columns: auto auto; // 20vw 40vw for me because I have dt and dd
  padding: 10px;
  text-align: left;
  justify-content: center;
  align-items: center;
}

.grid-container > div {
  padding: 20px;
}


<div class="grid-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
  <div>7</div>
  <div>8</div>
</div>

css-grid

Solution 9 - Html

Make sure you wrap your "row" inside the class "container" . Also add reference to bootstrap in your html.
Something like this should work:
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
 <body>
     <p>lets learn!</p>
  <div class="container">
          <div class="row">
              <div class="col-lg-6" style="background-color: red;">
                  ONE
              </div>
              <div class="col-lg-2" style="background-color: blue;">
                  TWO
              </div>
              <div class="col-lg-4" style="background-color: green;">
              THREE
           </div>
          </div>
      </div>
 </body>
 </html>

Solution 10 - Html

Anyone going for Bootstrap 5 (beta as on date), here is what worked for me. In my case, I had to align two items in the card's header section side-by-side:

<div class="card small-card text-white bg-secondary">
	<div class="d-flex justify-content-between card-header">
		<div class="col-md-6 col-sm-6">
			<h4>100+ Components</h4>
		</div>
		<div class="ml-auto">
        <!--<div class="col-md-6 col-sm-6 ml-auto"> -->
			<i class="data-feather hoverzoom" data-feather="grid"></i>
		</div>
	</div>
	<div class="card-body">
		<p>Lorem ipsum dolor sit amet, adipscing elitr, sed diam
			nonumy eirmod tempor ividunt labor dolore magna.</p>
	</div>
</div>

The catch here is justify-content-between and ml-auto. You can get more info here at the official link. And a live working example here.

Solution 11 - Html

In Bootstrap 4+, col-xs-6 doesn't exist. Instead use col-6:

<div class="container">
    <div class="row">
        <div class="col-6">
            ONE
        </div>
        <div class="col-6">
            TWO
        </div>
    </div>
</div>

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
QuestionSivaRajiniView Question on Stackoverflow
Solution 1 - HtmlBilly MoatView Answer on Stackoverflow
Solution 2 - HtmlRanveerView Answer on Stackoverflow
Solution 3 - HtmlErTRView Answer on Stackoverflow
Solution 4 - HtmlDev RathiView Answer on Stackoverflow
Solution 5 - HtmlOluwasayo BabalolaView Answer on Stackoverflow
Solution 6 - Htmlsam klokView Answer on Stackoverflow
Solution 7 - HtmlMichael NovelloView Answer on Stackoverflow
Solution 8 - HtmlYuqiu G.View Answer on Stackoverflow
Solution 9 - HtmlShijithaView Answer on Stackoverflow
Solution 10 - HtmlAjay KumarView Answer on Stackoverflow
Solution 11 - HtmlMiguel Galán LerateView Answer on Stackoverflow