twitter-bootstrap 引导程序“中心块”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31767748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 23:01:18  来源:igfitidea点击:

bootstrap "center-block"

htmlcsstwitter-bootstrap

提问by Armstrong

I'm using bootstrap to make a UI like the following:

我正在使用引导程序制作如下所示的 UI:

------------------------------------------------
|                                               |
|    div-outside                                |
|         ----------------------------          |
|         |                          |          |
|         |                          |          |
|         |        div-inside        |          |
|         |                          |          |
|         |                          |          |
|         ----------------------------          |
|                                               |
|                                               |
-------------------------------------------------

The code is:

代码是:

   <div id="app-layout-body" class="container-fluid body-content">
     <div id="div-register" class="center-block"     style="width:350px;margin-top:80px;">
      ....
    </div>
   </div>

To make the inner div center at the outer div, I'm using class="center-block". It works well if the screen is wider than a specific number, such as 1000px, but if the screen width is not big enough, the inner div will be adjacent to the right of outer div:

为了使内部 div 在外部 div 中心,我使用 class="center-block"。如果屏幕宽于特定数字(例如 1000px),则效果很好,但如果屏幕宽度不够大,则内部 div 将与外部 div 的右侧相邻:

----------------------------------------
|                                       |
|    div-outside                        |
|         ----------------------------  |
|         |                          |  |
|         |                          |  |
| left    |        div-inside        |  |
| margin  |                          |  |
|         |                          |  |
|         ----------------------------  |
|                                       |
|                                       |
-----------------------------------------

With Chrome's developer tool, I found that when the inner div is not centered, the left margin will be a fixed number (it seems have a lower bound). But when the screen's width is varying above the boundary value, the left margin number will vary according to the width thus making the inner div always centered. In fact, when the inner div is not horizontally centered, if I manually adjust the left margin number, then the inner div will be centered.

用Chrome的开发者工具发现,当内部div没有居中时,左边距会是一个固定的数字(好像有下界)。但是当屏幕的宽度在边界值以上变化时,左边距数将根据宽度而变化,从而使内部 div 始终居中。事实上,当内部div没有水平居中时,如果我手动调整左边距数字,那么内部div就会居中。

回答by Sun

Your example is working. The problem is that the layout does not work if the screen width is smallerthan 350px, because you have chosen a fixedwidth.

你的例子正在起作用。问题是如果屏幕宽度小于350px布局不起作用,因为您选择了固定宽度

Demo

演示

If the <div>should center all the time, use colsand offset:

如果<div>应该一直居中,请使用colsoffset

<div id="app-layout-body" class="container-fluid body-content">
    <div class="row">
        <div id="div-register" class="col-xs-8 col-xs-offset-2 col-sm-8 col-sm-offset-2 col-md-8 col-md-offset-2 col-lg-8 col-lg-offset-2">
            center with offset
        </div>
    </div>
</div>

Example offset

示例偏移

More information about Bootstrap's grid offsetting.

有关Bootstrap 网格偏移的更多信息。

回答by Rupesh Jain

Bootstrap's .center-blockcontains nothing more than

Bootstrap 只.center-block包含

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

There should be something else which is adding some other rules to your container. From the Inspector, try to check the CSS (fixed left margin) which you are talking about is getting added from which file.

应该还有其他东西向您的容器添加一些其他规则。从检查器中,尝试检查您正在谈论的 CSS(固定左边距)是从哪个文件添加的。

回答by vanburen

I think this is what you're trying to do.

我认为这就是你想要做的。

#app-layout-body {
  height: auto;
  margin: 0 auto;
  padding: 25px;
  position: relative;
  border: 5px solid red;
  text-align: center;
}
#div-register {
  border: 5px solid green;
  padding: 10px;
  background: red;
  color: white;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div id="app-layout-body">
    <div id="div-register"><strong>I'm a centered Div inside another centered Div..</strong>
      <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked
        up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
        Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a
        line in section 1.10.32.</p>
    </div>
  </div>
</div>