【问题标题】:How to center DIVs with float: left; in Bootstrap 3?如何使用浮动使 DIV 居中:左;在引导程序 3 中?
【发布时间】:2014-01-31 06:45:28
【问题描述】:

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

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

我无法将这 3 个 float: left; DIV 居中...(我不想为它们使用硬宽度)。

感谢您的建议。

【问题讨论】:

  • 给你的.container一些width和使用margin: auto;

标签: html css twitter-bootstrap css-float center


【解决方案1】:

尝试以下方法:

<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>

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

CSS:

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

HTML:

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

【讨论】:

  • 我没有注意到 - DIV 在一行中彼此相邻,而不是在一行中 1 个 DIV。
  • 如果您希望它们位于一行,只需使用clear:both。它对你有用吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
  • 2010-12-20
  • 2021-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多