简体   繁体   English

建议使用Bootstrap在div中水平对齐元素的方法

[英]Recomended way to horizontally align elements in div with bootstrap

I want the elements to be side by side and not on top of one another. 我希望元素并排,而不是彼此重叠。

When I say best way, I want the elements to not overlap when you change the size of the screen. 当我说最好的方法时,我希望元素在更改屏幕大小时不重叠。

Here is my HTML: 这是我的HTML:

<div class="container-fluid" style="text-align:center; background-color: #f6f6ff;">
    <div class="col-md-offset-1 col-sm-12 col-md-10" style="background-color: #f6f6ff;">
        <img src="media/subversion_logo-384x332.png" alt="Subversion" height="150" width="150">
        <h1>2</h1>
        <img src="media/github.png" alt="GitHub" height="150" width="150">
    </div>
</div>

Here is a picture of what it looks like: 这是它的外观图:

Right now you have an h1 separating the two images. 现在,您有一个将两个图像分开的h1。 Since heading tags are block level elements by default, it's not possible to line up the images side by side with the h1 separating them. 由于标题标签默认为块级元素,因此无法使用h1将它们并排排列。 However, if you put each image/heading in their own column, they will line up: 但是,如果将每个图像/标题放在它们自己的列中,它们将对齐:

<div class="container-fluid" style="text-align:center; background-color: #f6f6ff;">
    <div class="row">

        <div class="col-xs-6">
            <h1>1</h1>
            <img src="http://placehold.it/150x150" alt="Subversion" height="150" width="150">
        </div>

        <div class="col-xs-6">
            <h1>2</h1>
            <img src="http://placehold.it/150x150" alt="GitHub" height="150" width="150">
        </div>

        <div class="clearfix"></div>

  </div>
</div>

Bootply 自举

Using the Grid system to scale up to 12 columns, as described here 使用网格系统规模达12列,如描述在这里

<div class="row">
  <div class="col-md-4">
    <img src="media/subversion_logo-384x332.png" alt="Subversion" height="150"  width="150">
  </div>
  <div class="col-md-4">
    <h1>2</h1</div>
  <div class="col-md-4"> 
    <img src="media/github.png" alt="GitHub" height="150" width="150
</div>

You can change the size of the columns by increasing/decreasing col-md-4 您可以通过增加/减少col-md-4来更改列的大小

If you want two elements to be side by side, you can use "row" and "col" from the bootstrap grid . 如果希望两个元素并排,则可以使用引导网格中的 “ row”和“ col”。

For example, this is how to code 2 images: 例如,这是编码2张图像的方法:

<div class="row">
  <div class="col-lg-6">
    <img src="media/subversion_logo-384x332.png" alt="GitHub" height="150" width="150">
  </div>
  <div class="col-lg-6">
    <img src="media/subversion_logo-384x332.png" alt="GitHub" height="150" width="150">
  </div>
</div

The bootstrap answer is the best. 引导程序答案是最好的。 I had the same requirement before, only difference being that it was not acceptable to have the content in the different columns visually spread apart too much on large screens. 之前我有相同的要求,唯一的区别是不同列中的内容在大屏幕上的视觉分布过于分散是不可接受的。 In that case, you would use the original html and use css 在这种情况下,您将使用原始的html并使用css

.container-fluid div > * {
    margin: 0 auto;
    display: inline;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM