html - 将图像定位在居中的 div 后面

标签 html css

我的网站有一个 900 像素的 div #content,它以 margin-left: automargin-right: auto 为中心。我有一张图片需要显示在 div 后面,它会部分重叠 #content

图像目前设置为显示为 block ,我可以将它放到需要的位置,但它不允许 #content 在图像上绘制。我可以使用 position: absolute#content 显示在图像上,但是这会阻止使用 margin-left/margin-right 自动居中。

我目前的定位是:

img#watermark  
{
    margin-left: auto;  
    margin-right: auto;
    display: block;
    padding-left: 900px;
}

#content 只需要出现在水印上即可。

非常感谢帮助。

最佳答案

html:

<img src="http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png" />
<div></div>

CSS:

div {
    margin:auto;
    width: 512px;
    height: 512px;
    background: rgba(0,0,0,.4);
    position: relative;
}
img {
    position: absolute;
    left: 50%;
    margin-left:-256px;
}

http://jsfiddle.net/Db2cw/

关于html - 将图像定位在居中的 div 后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5926626/

相关文章:

html - 奇怪的 CSS 媒体查询逻辑

html - 如何将两个内容放在中间的div

html - 在线上传网站时,CSS 文件未链接到 HTML 文件

html - 至少与浏览器 IE 和 Firefox 不兼容

javascript - 尝试使用 JavaScript 删除 HTML 元素

jquery - 对象 [object Object] 在使用 jquery 将项目添加到列表时没有方法 'listview'

html - 不透明度属性在 css 中没有给出预期的结果

javascript - 获取 HTML 元素 (iframe) 中的元素数量

css - 获取动态变化高度div的高度

css - 内联 block 元素的烦人空格