图像重叠在div上 [英] Image overlapping over div

查看:111
本文介绍了图像重叠在div上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



考虑这个html片段

 < HTML> 
< head>
div
{
width:100%;
height:100px;
}
img
{
width:100%;
}
#div2
{
margin-top:-100px;
}
< / head>
< body>
< div id =div1>
< img src =/>
< / div>
< div id =div2>
一些文本
< / div>
< / body>
< / html>

我想在div1上覆盖div2。由于div2的margin-top等于div1的高度,因此代码会重叠。我的问题是图像重叠div2。这种行为的原因是什么?



我不想给元素赋予绝对位置,因为如果使用绝对位置,代码将会破坏页面的布局。



谢谢。

解决方案

检查这个jsfiddle 。正如onetrickpony提到的,需要定位元素。

  div 
{
width:100%;
height:100px;
}
img
{
width:100%;
}
#div1 {
z-index:10;
职位:亲属;
}

#div2
{
position:relative;
margin-top:-100px;
border:1px solid#f00;
z-index:20;
颜色:#fff;
font-weight:bold;
}


iam facing a small issue with overlapping .

Consider this html snippet

<html> 
  <head>
    div
    {
      width:100%;
      height:100px;
    }
    img
    {
      width:100%;
    }
    #div2
    {
      margin-top:-100px;
    }
  </head>
  <body> 
    <div id="div1"> 
      <img src=""/> 
    </div> 
    <div id="div2"> 
      some text 
    </div> 
  </body> 
</html>

I want to overlap a div2 over div1. As the code will overlap since margin-top of div2 equals height of div1. My problem is image is overlapping div2. What is the reason of this behaviour ?

And i don't want to give position absolute to the elements since this code will break the layout of the page if position absolute is used.

Thanks.

解决方案

Check this jsfiddle. As onetrickpony mentioned, need to be positioning the elements.

div
{
  width:100%;
  height:100px;
}
img
{
  width:100%;
}
#div1 {
z-index: 10;
position: relative;
}

#div2
{
position: relative;  
margin-top:-100px;
border: 1px solid #f00;
z-index: 20;
color: #fff;
font-weight: bold;
}

这篇关于图像重叠在div上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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