html - 当两个 div 使用自动边距居中时,如何将一个 div 放在另一个之上?

标签 html css

我有 2 个 div,一个 square 和一个 bar。两者都使用 auto 边距居中。但我想把酒吧放在广场上。我该怎么做?

html

<div class="square">square</div>
<div class="bar">square</div>

CSS

.square,.bar{
    margin-left:auto;
    margin-right:auto
}
.square{
    background-color:blue;
    width: 100px;
    height: 100px;
}
.bar{
    background-color:green;
    width: 400px;
    height: 40px;
}

Here 是 fiddle 。

最佳答案

http://jsfiddle.net/atomicrevolution/P9yWK/5/

你是这个意思吗?

.square,.bar{
    margin-left:auto;
    margin-right:auto;
}
.square{
    background-color:blue;
    width: 100px;
    height: 100px;
    position: relative;
}
.bar{
    background-color:green;
    width: 400px;
    height: 40px;
    position: relative;
    margin-top: -70px;
}

关于html - 当两个 div 使用自动边距居中时,如何将一个 div 放在另一个之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24123534/

相关文章:

javascript - 代码返回 JSON 错误 Uncaught SyntaxError : Unexpected token :

html - 菜单的伪下划线选择器

css - 无法将元素 (div) 定位到左侧

javascript - 如何使用 jQuery 和/或 css 在 td 中为该图像制作动画?

php - 文件路径突然不能在 html 中工作

javascript - jQuery 按类计算元素 - 实现这一点的最佳方法是什么?

html - 从表格中抓取 Excel VBA Web

html - 具有响应宽度的元素之前的边距

html - 内联 SVG &lt;title&gt; <desc> 可访问性的正确用法

Javascript 创建可点击的文本