简体   繁体   English

如何在Bootstrap 3中将所有div居中

[英]How to center all divs in bootstrap 3

I need to center align every divs in Bootstrap3. 我需要居中对齐Bootstrap3中的每个div。 To make it an accounting report. 使其成为会计报告。 There're 3 level of divs - Header, subheader and detail list. div分为3级-标题,子标题和详细信息列表。 Each level I use col-md-* to limit the width. 每个级别我都使用col-md-*来限制宽度。 I've tried text-align but it's not help. 我已经尝试过text-align但这无济于事。

Here's the code : 这是代码:

<div class="container">
<div class="header text-info">
        <h1>Heading</h1><br />
        <h2>Acc. Type</h2><br />
        <h3>Month</h3>
    </div>
<legend>Income</legend>
    <div class="acc-content row">
        <div class="col-md-12">
            <b>col-md-12</b>
            <span class="pull-right">#@#</span>
        </div>
    </div>
<legend>Expense</legend>
    <div class="acc-content row">
        <div class="col-md-12">
            <b>col-md-12</b>
            <span class="pull-right">#@#</span>
        </div><!-- /col-md-12 -->
        <div class="col-md-10">
            <b>col-md-10</b>
            <span class="pull-right">#@#</span>
        </div><!-- /col-md-10 -->
        <div class="col-md-8">
            <b>col-md-8</b>
            <span class="pull-right">#@#</span>
        </div><!-- /col-md-8 -->
    </div><!-- /acc-content -->
</div>    

DEMO : http://fiddle.jshell.net/nobuts/mshwa1yt/1/show/ (please view in desktop). 演示: http : //fiddle.jshell.net/nobuts/mshwa1yt/1/show/ (请在桌面上查看)。

I expect all the divs to center aligned like this : (red border is just a test) 我希望所有div都这样居中对齐:(红色边框只是一个测试)

Please be adviced. 请告知。

在此处输入图片说明

Add col-md-offset-* . 添加col-md-offset-* For example: 例如:

<div class="col-md-10 col-md-offset-1">

and

<div class="col-md-8 col-md-offset-2">

You must also change your margin: 5px on .acc-content div[class*='col-md-'] to margin-top: 5px; margin-bottom: 5px 您还必须改变你的margin: 5px.acc-content div[class*='col-md-']margin-top: 5px; margin-bottom: 5px margin-top: 5px; margin-bottom: 5px because it overwrites Bootstrap's margin-left for col-md-offset-* . margin-top: 5px; margin-bottom: 5px因为它会覆盖Bootstrap的col-md-offset-*margin-left

使用块中心类包装使用行

all the col classes from bootstrap have the float left property. 引导程序中的所有col类都具有float left属性。 either remove them (not adivsed) and add margin:auto to the divs...or add more divs: 要么将它们删除(不细分),然后将margin:auto添加到divs中,要么添加更多divs:

if u have a col-8 add col-2 left and col-2 right (offsets)...so you keep the grid system in place 如果您有col-8,则向左添加col-2,向右添加col-2(偏移量)...以便将网格系统保持在原位

You need to add this to the div s: 您需要将此添加到div

.acc-content div[class*='col-md-'] {
    float: none;
    margin: 5px auto;
}

First one overwrites the float: left rule that's pretty much the opposite of center aligning it, the second one actually aligns it to center. 第一个覆盖float: left则与居中对齐相反,第二个实际上将其居中对齐。

Bootstrap 3 has a mixin called center-block found in utilities.less. Bootstrap 3在Utility.less中有一个名为center-block的mixin。

First, add class center-block to elements that should be centered. 首先,将class center-block添加到应居中的元素中。

Then, if using Less, add to CSS: 然后,如果使用Less,则添加到CSS:

.center-block {
.center-block();
}

If using plain CSS, add: 如果使用纯CSS,请添加:

.center-block {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

in this example replace this 在这个例子中替换

     <div class="col-md-10">
        <b>col-md-10</b>
        <span class="pull-right">#@#</span>
    </div><!-- /col-md-10 -->
    <div class="col-md-8">
        <b>col-md-8</b>
        <span class="pull-right">#@#</span>
    </div><!-- /col-md-8 -->

with this 有了这个

<div class="col-md-10 col-md-offset-1">
        <b>col-md-10</b>
        <span class="pull-right">#@#</span>
    </div><!-- /col-md-10 -->
    <div class="col-md-8 col-md-offset-2">
        <b>col-md-8</b>
        <span class="pull-right">#@#</span>
    </div><!-- /col-md-8 -->

add class col-md-offset-"number of cols" to col- divs 将类col-md-offset-“ cols数”添加到coldiv

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

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