如何使用 Bootstrap2 将 div 居中?
http://twitter.github.com/bootstrap/scaffolding.html
我像所有人一样尝试过组合:
<div class="row">
<div class="span7 offset5"> box </div>
</div>
或
<div class="container">
<div class="row">
<div class="span7 offset5"> box </div>
</div>
</div>
更改跨度和偏移量...
但我无法得到一个在页面上完美居中的简单框:(
我只想要一个居中的 6 列宽框...
edit:
?
<div class="container">
<div class="row" id="login-container">
<div class="span8 offset2">
box
</div>
</div>
</div>
但盒子太宽了,有什么办法可以用 span7 做到吗
span7 offset2
为左侧提供额外的填充 span7 offset3
为右侧提供额外的填充...
http://twitter.github.com/bootstrap/scaffolding.html
I tried like all combinations:
<div class="row">
<div class="span7 offset5"> box </div>
</div>
or
<div class="container">
<div class="row">
<div class="span7 offset5"> box </div>
</div>
</div>
changed span and offset numbers...
But I cant get a simple box perfectly centered on a page :(
I just want a 6-column-wide box centered...
edit:
did it with
<div class="container">
<div class="row" id="login-container">
<div class="span8 offset2">
box
</div>
</div>
</div>
But the box is too wide, is there any way I can do it with span7 ?
span7 offset2
gives extra padding to the left span7 offset3
extra padding to the right...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Bootstrap 的跨度向左浮动。使它们居中所需要做的就是覆盖此行为。我通过将其添加到我的样式表中来做到这一点:
如果您定义了此类,只需将其添加到跨度中即可。
请注意,此自定义中心类必须在 bootstrap css 之后定义。您可以使用
!important
但不建议这样做。Bootstrap's spans are floated to the left. All it takes to center them is override this behavior. I do this by adding this to my stylesheet:
If you have this class defined, just add it to the span and you're good to go.
Note that this custom center class must be defined after the bootstrap css. You could use
!important
but that isn't recommended.除了将 div 本身缩小到您想要的大小之外,还可以像这样减小跨度大小...
class="span6 offset3"
、class="span4 offset4"
等。 .. div 上像style="text-align: center"
这样简单的东西可能会产生您正在寻找的效果,您不能使用带有任何设置偏移量的 span7 并使跨度居中页面(因为总跨度 = 12)
besides shrinking the div itself to the size you want, by reducing span size like so...
class="span6 offset3"
,class="span4 offset4"
, etc... something as simple asstyle="text-align: center"
on the div could have the effect you're looking foryou can't use span7 with any set offset and get the span centered on the page (Because total spans = 12)
Bootstrap3 有您可以使用的
.center-block
类。它在此处定义为文档。
Bootstrap3 has the
.center-block
class that you can use. It is defined asDocumentation here.
如果您想要完全引导(而不是自动左/右方式),您需要一个适合 12 列的模式,例如 2 个空白、8 个内容、2 个空白。这就是这个设置要做的事情。
它仅涵盖 -md- 变体,我倾向于通过添加 col-xs-12 将其调整为全尺寸
If you want to go full-bootstrap (and not the auto left/right way) you need a pattern that will fit within 12 columns e.g. 2 blanks, 8 content, 2 blanks. That's what this setup will do.
It only covers the -md- variants, I tend to snap it to full size for small by adding col-xs-12
听起来您只想居中对齐单个容器。
引导框架可能过于复杂,例如,您可能只有一个具有您自己的样式的独立 div,例如:
和 style:
如果您嵌套在引导
.container
中的某处,那么应该可以正常工作分区Sounds like you just wanted to center align a single container.
The bootstrap framework might be overcomplicating that one example, you could have just had a standalone div with your own styling, something like:
and style:
That should work fine if you are nested somewhere within a bootstrap
.container
div.添加课程中心内容
add the class centercontents
@ZuhaibAli 代码对我来说有点工作,但我稍微改变了它:
我在 css 中创建了一个新类,
然后 div 变成
我添加了 col-md-6 作为 div 本身的宽度,在这种情况下意味着 div 是一半大小,bootstrap中有1 -12 col md。
@ZuhaibAli code kind of work for me but I changed it a little bit:
I created a new class in css
then the div become
I added col-md-6 for the width of the div itself which in this situation meant the div is half the size, there are 1 -12 col md in bootstrap.
请遵循此指南 https://getbootstrap.com/docs/3.3/css/
使用 <代码>.center-block
Follow this guidance https://getbootstrap.com/docs/3.3/css/
Use
.center-block
将 div 包装在带有类
row
的父 div 中,然后将样式margin:0 auto;
添加到该 divwrap the div in a parent div with class
row
then add stylemargin:0 auto;
to the div