使用默认的 Twitter Bootstrap CSS 将 div 水平居中对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15113030/
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-08-29 21:48:21  来源:igfitidea点击:

Center align a div horizontally using default Twitter Bootstrap CSS

csstwitter-bootstrap

提问by Debiprasad

I know how to center align a div horizontally in CSS. You need to make the width to something less than 100% and have auto margin on both left and right sides. I want to do this default styles of Twitter Bootstrap. I don't want to write additional CSS. Wondering whether it's possible to achieve using default style used in Twitter Bootstrap.

我知道如何在 CSS 中水平居中对齐 div。您需要将宽度设置为小于 100% 并在左右两侧都有自动边距。我想做这个 Twitter Bootstrap 的默认样式。我不想编写额外的 CSS。想知道是否可以使用 Twitter Bootstrap 中使用的默认样式来实现。

To set width of the div, I am using span*. But span*is float: left. Is there any class, which can set the width of the div, but will not add float: left?

要设置 div 的宽度,我使用span*. 但是span*float: left。有没有类,可以设置div的宽度,但是不会加float: left

回答by JayGee

To horizontally align a div that is using an even span styles (span2, span4, etc...), you need to use offset.

要水平对齐使用偶数跨度样式(span2、span4 等...)的 div,您需要使用偏移。

<div class="offset3 span6">
    ...
</div>

回答by Shail

Use class pagination-centeredinside a div like :

pagination-centered在 div 中使用类,例如:

   <div class="row">
    <div class="span12 pagination-centered">
    <p> content</p>
   </div>
</div>

回答by yanychar

Bootstrap's way to center content seems to be by assigning class="center-block"to it: a link to Bootstrap 3 doc page.

Bootstrap 将内容居中的方式似乎是通过分配class="center-block"给它:指向 Bootstrap 3 文档页面的链接

However, it won't work on a heading or a paragraph. class="text-center"seems to work in those cases.

但是,它不适用于标题或段落。class="text-center"似乎在这些情况下工作。

回答by Zouzias

Based on @JayGee's answer.

基于@JayGee 的回答。

<div class="row">
     <div class="col-sm-offset-4 col-md-offset-4 col-lg-offset-4 col-sm-4 col-md-4 col-lg-4">
     ... content here ...
     </div>
</div>

回答by Rockbot

If the element you want to align horizontal is an odd span (@JayGee) and you don′t want to write additional CSS (not even inline?) then the answer should be: No, that′s not possible. Otherwise I′d go for @JayGee′s answer.

如果您想要水平对齐的元素是一个奇数跨度 (@JayGee) 并且您不想编写额外的 CSS(甚至不是内联?)那么答案应该是:不,那是不可能的。否则我会去@JayGee 的回答。