使用负边距顶部在同级div上使用div重叠img标记 [英] overlay a img tag with a div using negative margin-top on the sibling div

查看:200
本文介绍了使用负边距顶部在同级div上使用div重叠img标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用 div 覆盖 img code>在 div 上, div 中的文本显示在图像上,在 img 下显示 div 的c $ c> background p>

html:

 <!DOCTYPE html& 
< html>
< head>
< meta charset =utf-8>
< title> JS Bin< / title>
< / head>
< body>
< div class =a>
< div class =b>< img src =/>< / div>
< div class =c>有人< / div>
< / div>
< / body>
< / html>

css:

 code> .a {
height:60px;
background:#ccc;
}

.b img {
height:60px;
width:60px;
background:red;
}

.c {
background:blue;
height:20px;
margin-top:-20px;
}

我在IE9和chrome中观察到这种行为。



这是预期的行为吗?
如果是,原因是什么?



我可以使 div 绝对相对来实现我想要做的,但我不能理解这种行为。



http://jsbin.com/gejuzuza/1/edit

解决方案

如果你添加 position:relative; code> .c ,它会将 div c 上面 div b


I am trying to overlay a img with a div using negative margin on the div, the text inside the div is displayed over the image, while the background of the div is being displayed under the img .

html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div class="a">
    <div class="b"><img src=""/></div>
    <div class="c">somebody</div>
 </div>
</body>
</html>

css:

.a{
  height:60px;
  background:#ccc;
}

.b img{
  height:60px;
  width:60px;
  background:red;
}

.c{
  background:blue;
  height:20px;
  margin-top:-20px;
}

i have observed this behavior in IE9 and chrome.

is this behavior as expected? if yes, what is the reason?

I could make the div absolute or relative to achieve what i am trying to do, but i am not able to understand this behaviour.

http://jsbin.com/gejuzuza/1/edit

解决方案

If you add position: relative; to your CSS for .c, it will put the div with the c class above the div with the b class

这篇关于使用负边距顶部在同级div上使用div重叠img标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆