使用 bootstrap css 将两个 div 横向并排中心对齐到页面

请参考下面的代码,我尝试

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

产出:

First Div
SecondDiv

预期产出:

                      First Div        Second Div

我想对齐两个 div 水平中心的页面使用引导 CSS。我怎么能这么做?我不想使用简单的 css 和浮动的概念来做到这一点。因为我需要使用 bootstrap css 来处理所有类型的布局(即所有窗口大小和分辨率) ,而不是使用媒体查询。

329956 次浏览

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

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.

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>
<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.

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

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-#.

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>

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

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>

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.

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>

Alternative for bootstrap 5

<div class="container">
<div class="row">
<div class="col-sm">
Column
</div>
<div class="col-sm">
Column
</div>
<div class="col-sm">
Column
</div>
</div>
</div>```