CSS中如何实现图片重叠

17

如何让"mymessage.gif"显示在"bread_wine.jpg"上方。

"mymessage.gif"具有透明背景。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>overlap images</title>
<style type="text/css">
<!--
#navbar {
    font-size: 18px;
    font-weight: bold;
    text-align: center; 
}
#thebigimage {
    background-image: url(bread_wine.jpg);
    height: 548px;
    width: 731px;
    margin-right: auto;
    margin-left: auto;
}
#overlapthis {
    background-image: url(mymessage.gif);
}
-->
</style>
</head>
<body>
<div id="navbar">this is the nav bar</div>
<div id="thebigimage">
<div id="overlapthis"></div>
</div>
</body>
</html>
3个回答

18
#overlapthis {
    background-image:url("mymessage.gif");
    height:100px;
    left:50px; /* play around with this */
    position:absolute;
    top:90px; /* and play around with this */
    width:500px;
}

#thebigimage {
    background-image:url("bread_wine.jpg");
    height:548px;
    margin-left:auto;
    margin-right:auto;
    position:relative; /* and this has to be relative */
    width:731px;
} 

完美地工作了!非常感谢你! - MP123
没问题,伙计。随时可以问我。希望你能理解发生了什么 :) - Claudiu
background-size:80px ??px; 可以用来指定图像的大小。 - Anirudh

2

请尝试

#overlapthis {
    position: absolute;
    top: ??px;
    left: ??px;
    z-index: 1;
}

在我的情况下,z-index是一个重要的参数。谢谢! - Aamol

0

根据您目前的设置,图像已经重叠了。只是因为您没有在overlapthis div中添加尺寸,所以看不到它。请尝试以下操作:

#overlapthis {
    background-image: url(mymessage.gif);
    width: 500px;
    height: 100px;
}

然后添加边距(和position: absolute)来将图像定位到所需位置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接