我在页面上有一张图片,我想在图片的左上角画一个带有小图形的链接。有没有办法做到这一点?适当地将其重叠在图片上?
像这样做就可以了(建议将内联CSS移动到样式表中):
<div style="position:relative">
<div>
<img src="backgroundimg.png">
</div>
<div style="position:absolute; left:0; top:0;">
<a href="foo.html"><img src="smallgraphic.png"></a>
</div>
</div>
position:absolute
属性将使该 div 相对于已设置了 position
属性的容器定位,这里设置为在左侧 0 顶部 0。因此,它将出现在“backgroundimg”所放置的相同元素的左上角。
明白吗?
不要使用比你需要的更多的div。
<div style="position: relative;">
<a style="position: absolute; top: 0; left: 0;">
<img src="..." />
</a>
<img src="..." />
</div>
简化:
<div style="background:url(yourimage.jpg); position:relative;">
<div style="position:absolute; left:0; top:0;">
<a href="somewhere.html"><img src="inner.jpg"></a>
</div>
</div>
或者:
<div style="background:url(yourimage.jpg); position:relative;">
<a href="somewhere.html"><img src ="innerimage.jpg" style="position:absolute; left:0; top:0;"/></a>
</div>
或者:
<div style="background:url(yourimage.jpg); position:relative;">
<a href="somewhere.html" style="position:absolute; left:0;
top:0;display:block;width:100px;height:100px;"></a>
</div>