twitter-bootstrap 如何使用浮动居中 DIV:左;在 Bootstrap 3 中?

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

How to center DIVs with float: left; in Bootstrap 3?

htmlcsstwitter-bootstrapcss-floatcenter

提问by user984621

I am fighting with this stuff some time already and cannot figure it out:

我已经有一段时间与这些东西作斗争了,但无法弄清楚:

<div class="container">
  <div>
    <div style="float: left;">xxx</div>
    <div style="float: left;">xxx</div>
    <div style="float: left;">xxx</div>
  </div>
</div>

I cannot center those 3 float: left;DIVs... (I don't want to use hard width for them).

我不能居中这 3 个浮动:左;DIV...(我不想为它们使用硬宽度)。

Thank you for any advice.

谢谢你的任何建议。

回答by Ilia Rostovtsev

Try the following:

请尝试以下操作:

<div class="container">
  <div class="row">
    <div class="span4 offset4" style="float: left; text-align: center">xxx</div>
    <div class="span4 offset4" style="float: left; text-align: center">xxx</div>
    <div class="span4 offset4" style="float: left; text-align: center">xxx</div>
  </div>
</div>

Another possible solution would be creating custom class:

另一种可能的解决方案是创建自定义类:

CSS:

CSS:

.centered {
     float: none;
     margin-left: auto;
     margin-right: auto;
}

HTML:

HTML:

<div class="span4 offset4 centered"> xxx </div>