简体   繁体   English

Bootstrap:如何在液体容器内部正确居中放置6列DIV

[英]Bootstrap: How to correctly centre a 6 column DIV inside of a fluid container

I am curious to know what is the proper way of centring a DIV (col-lg-6) inside of a fluid container (12 columns) using Bootstrap? 我很好奇,知道使用Bootstrap将DIV(col-lg-6)置于流体容器(12列)内的正确方法是什么?

Here is a screenshot of my design mockup, which explains what I am trying to achieve: http://d.pr/i/1iBrP . 这是我的设计模型的屏幕截图,它解释了我要实现的目标: http : //d.pr/i/1iBrP

So far I've read on Bootstraps website that this sort of thing can be achieved by adding an offset class eg: 到目前为止,我已经在Bootstraps网站上阅读了可以通过添加偏移量类来实现这种事情的方法,例如:

<div class="container-fluid">
    <div class="row">
      <h1 class="col-xs-12 col-sm-10 col-md-6 col-sm-offset-1 col-md-offset-3">Auto cargo</h1>
    </div>
</div>

Is it a correct way of doing it or is there a better (or preferable) way of achieving this task? 是这样做的正确方法,还是有更好(或更可取)的方法来完成此任务?

Also, what if the width of the DIV would be 7 columns, meaning that the number of columns on the side of the DIV would be different because 12 - 7 = 5 columns and 5 is not an even number? 另外,如果DIV的宽度为7列,那意味着DIV侧面的列数会有所不同,因为12-7 = 5列,而5不是偶数?

Thank you very much in advance. 提前非常感谢您。

[Edited answer] [编辑答案]

You can use the helper class center-block in a div which can be a row . 您可以在可以为rowdiv使用帮助程序类的center-block

Here is the document about this helper class: Center content blocks 这是有关此帮助器类的文档: 中心内容块

 .container-fluid { border: 1px solid red; } .col-xs-7 { border: 1px solid blue; } .text-center > .single { display: inline-block; float: none; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <div class="container-fluid"> <div class="row"> <div class="col-xs-7 col-xs-offset-2"> I'm not centered </div> </div> <div class="row"> <div class="col-xs-7 col-xs-offset-3"> I'm not centered </div> </div> <div class="row center-block"> <div class="col-xs-7 col-xs-offset-3"> I'm not really centered </div> </div> <div class="row text-center"> <div class="col-xs-7 single"> I'm really centered </div> </div> </div> 

EDIT: 编辑:

I updated the snippet because center-block doesn't work in the example I gave. 我更新了代码段,因为在我给出的示例中, center-block不起作用。

So you can use text-center on the row and a custom class called single which uses display: inline-block and float: none . 因此,您可以row上使用text-center ,并使用一个名为single的自定义类,该类使用display: inline-blockfloat: none

  1. If you have 7 columns you can't center it with pure Bootstrap, you can do it like this: 如果您有7列,则无法使用纯Bootstrap对其进行居中,则可以这样操作:

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

<div class="col-md-7 col-center">
  1. Your code is correct, but you don't need to set col-xs-12 class (it will automaticaly set 12 columns on mobile without that class - div width = 100% . 您的代码是正确的,但是您不需要设置col-xs-12类(如果没有该类,它将自动在移动设备上设置12列div width = 100%
  2. You set col-md-6 and col-md-offset-3 for medium screen sizes, it means that it will be the same on large( lg ) screens, if you want to reset it on large screen you have to do it like this (change column size and reset offset): 您将col-md-6col-md-offset-3为中等屏幕尺寸,这意味着它在大( lg )屏幕上将是相同的,如果要在大屏幕上重置它,则必须像这(更改列大小和重置偏移量):

<h1 class="col-sm-10 col-md-6 col-sm-offset-1 col-md-offset-3 col-lg-12 col-lg-offset-0">Auto cargo</h1>
  1. You shouldn't set this class on h1 element, this is better way: 您不应该在h1元素上设置此类,这是更好的方法:

<div class="container-fluid">
  <div class="row">
    <div class="col-xs-12 col-sm-10 col-md-6 col-sm-offset-1 col-md-offset-3"> 
      <h1>Auto cargo</h1>
    </div>
  </div>
</div>

You don't need to do those complex code, just use below code, this will work in all screen. 您无需执行那些复杂的代码,只需使用以下代码,即可在所有屏幕上使用。

  <div class="container-fluid"> <div class="col-sm-6 col-sm-offset-3 text-center"> <h2>Lorem ipsum dolor sit amet</h2> </div> </div> 

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

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