html - 将带有图像的子 div 放置在父 div 的顶部

标签 html css

我试图将一个子 div 放在其父 div 的顶部(包括其内容)

<div id="footer">
  <div id="footer-container">
    <div id="icon"></div>                    
  </div>    
</div>​

#footer {
    height:50px;
    border-top:3px #666 solid;
    margin-top:50px;
}

#footer-container {
    height: 30px;
    width: 300px;
    margin-left: auto;
    margin-right: auto;
    margin-top: -15px;   
}

#icon {
    height:30px;
    width:30px;
    background-color:#666;

}​

现在,如果 <div id="icon"> 的内容有效是文本,但如果您在 div 中放置背景图像,则它不会。有什么办法可以使这项工作?这也许可以更好地解释 http://jsfiddle.net/4QxL7/

编辑 道歉。它一直在工作。我将 PNG 用于中间有“空白”的图像,这使得父 div 中的边框(颜色相同)看起来像是越过子元素的顶部,实际上它是落后。

谢谢你的帮助

最佳答案

我刚刚尝试了两种方法,它们都使用我网站上的超大图片...

<div id="footer">
        <div id="footer-container">
            <div id="icon"><img src="image url here" width=30 height=30/></div>                    
        </div>    
    </div>​

http://jsfiddle.net/ZkxSM/

#icon {
    height:30px;
    width:30px;
    background-color:#666; /*unnecessary now probably...*/
    background:url('image url here');
}​

http://jsfiddle.net/b6QyX/ (图像需要事先调整大小才能正常工作……或者可以在 div 的 html 中设置宽度和高度)

您的 jsfiddle 实际上没有任何问题..

关于html - 将带有图像的子 div 放置在父 div 的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12630220/

相关文章:

jquery:动态更改标题(.append()?.html()?或其他?)

html - 使用 bootstrap 在 css 和 html 中定位

html - 无法在 Bootstrap 行中居中放置某些文本

html - 带虚线或虚线边框的圆圈

c# - Bootstrap 表单不显示内联 (C#)

html - 为什么 Chrome 没有正确考虑边距?

javascript - 内联 onclick 与 addeventlistener 的顺序是什么?为什么?

css - 使用 CSS 等距放置对象

javascript - 根据鼠标单击移动和旋转对象?

html - 内联 SVG 不缩放