html - 清除 : both overrides margin: 0 auto?

标签 html css twitter-bootstrap

我有一个定位问题需要处理。在容器 div 中,我有两个 div,它们都应该水平居中。但是,我在定位第二个 div 时遇到了问题。

如果我添加 clear:both div 2 得到一个新行,这是需要的。但是,如果使用 clear:both margin: 0 auto;不起作用。我可以使用什么来使 div 居中?

HTML:

<div class="row-fluid">
    <div class="span6 offset3 text-center header">
        <h1>Title...</h1>
        <p>Text...</p>
    </div>
    <div class="span3 offset4">
        <form>
          ...
        </form>                 
    </div>
</div>

CSS:

.register .span3 {
    width: 270px;
    clear: both;
    margin: 0 auto;
}

输出:

- Without clear:both
 ____________________________________
|                                    |
|              ______       ______   |
|             |      |     |      |  |
|             | div 1|     | div 2|  |
|             |______|     |______|  |
|                                    |
|                                    |
|                                    |
|                                    |
|                                    |
|                                    |


- WITH clear:both
 ____________________________________
|                                    |
|              ______                |
|             |      |               |
|             | div 1|               |
|             |______|               |
|  ______                            |
| |      |                           |
| | div 2|                           |
| |______|                           |
|                                    |
|                                    |

- Desired output
 ____________________________________
|                                    |
|              ______                |
|             |      |               |
|             | div 1|               |
|             |______|               |
|              ______                |
|             |      |               |
|             | div 2|               |
|             |______|               |
|                                    |
|                                    |

最佳答案

当你不漂浮的时候为什么需要clearclear 仅适用于 float 情况。

如果您删除 clear:both 并仅插入 margin:0 auto ,则两个 div 都将在页面上居中,如您所愿,一个在另一个下方。

关于html - 清除 : both overrides margin: 0 auto?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18446087/

相关文章:

javascript - 处理多个 Canvas 上下文的解决方案

CSS flexbox 布局 : child with height 100%?

css - 减少 Bootstrap 导航栏 css 中的页眉/页脚大小

php - Bootstrap 的 onmouseover 错误

html - 目前哪个浏览器对 HTML 5 的支持最好?

javascript - 图片在 iPad 上显示为旋转,而不是在笔记本电脑上

css - 具有核心动画页面和长列表的 polymer 芯片到卡片模式

javascript - Bootstrap 日期选择器,禁用日期 - 可以查看仅模式吗?

java - 提交表单时 Spring 枚举异常

css - 移动 View HTML 中的重叠 div