overlay a img tag with a div using negative margin-top on the sibling div

让人想犯罪 __ 提交于 2019-12-13 05:05:21

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/25164585/overlay-a-img-tag-with-a-div-using-negative-margin-top-on-the-sibling-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!