我有div,想在div的右上角放置图章之类的图像
这是我已经完成的代码
.package-item-header {
margin-top:20px;
margin-left:20px;
width: 200px;
background-color: #efefef;
text-align: center;
font-size: 24px;
position: relative;
padding: 20px;
}
.package_stamp{
height: 30px;
width: 30px;
background: url('https://i.imgur.com/bPR0GVD.png') 50px no-repeat;
background-position: center;
position:absolute;
top:0;
right:0;
}
<div class="package-item-header">
<div class="package_stamp"></div>
GOLD
</div>
但是它只是放在div的右下角,而不是在div上方。
我该如何做,但背景要透明?
答案 0 :(得分:4)
您可以将top
和right
的负值与position: absolute
结合使用,将图像移到其父图像上。 overflow:visible
确保图像在父母的边界之外可见,但是在这种情况下不是必须的。
我还添加了background-size: cover
,以使背景图片填充元素。
.package-item-header {
margin-top: 20px;
margin-left: 0;
width: 200px;
background-color: #efefef;
text-align: center;
font-size: 24px;
position: relative;
padding: 20px;
overflow: visible;
float: left;
}
.package_stamp {
height: 60px;
width: 60px;
background: url('https://i.imgur.com/bPR0GVD.png') 50px no-repeat;
background-position: center;
position: absolute;
top: -20px;
right: -20px;
background-size: cover;
z-index: 1;
}
<div class="package-item-header"><img class="package_stamp" />GOLD</div>
<div class="package-item-header"><img class="package_stamp" />GOLD</div>
<div class="package-item-header"><img class="package_stamp" />GOLD</div>
答案 1 :(得分:0)
检查http://jsfiddle.net/xpvt214o/328587/
.package_stamp{
height: 60px;
width: 60px;
background: url('https://i.imgur.com/bPR0GVD.png') 10px no-repeat;
background-position: center;
background-size: 60px 60px;
position:absolute;
top:5;
right:0;
}
答案 2 :(得分:0)
.package-item-header {
margin-top:20px;
margin-left:20px;
width: 100px;
background-color: #efefef;
text-align: center;
font-size: 24px;
position: relative;
padding: 20px;
position: relative;
float: left;
}
.package-item-header:hover .package_stamp {
opacity: .5;
}
.package_stamp{
height: 80px;
width: 80px;
background: url('https://i.imgur.com/bPR0GVD.png') no-repeat;
background-position: center;
position:absolute;
top:-25px;
right:-25px;
background-size: contain;
z-index: 1;
}
img.package_stamp {
border: 1px solid white;
}
<div class="package-item-header"><div class="package_stamp"></div>GOLD</div>
<div class="package-item-header"><div class="package_stamp"></div>GOLD</div>
<div class="package-item-header"><div class="package_stamp"></div>GOLD</div>