如何使用CSS将按钮放置在图片的边缘?
我的代码
<div>
<button class="" aria-label="Eat cake">Btn</button>
<img class="pull-left" src="style.png" style="width: 160px; border: 1px solid #DEDEDC;"/>
</div>
此致
答案 0 :(得分:1)
在这里仅举一个例子,我只是将红色btn绝对值放置在它的相对父级中。
.blue-div{
margin: 10%;
position: relative;
width: 120px;
height: 120px;
background: blue;
}
.red-btn{
position: absolute;
top: -10px;
right: -10px;
background: red;
border: none;
outline: none;
border-radius: 50%;
padding: 8px 4px;
color: white;
}
<div class="blue-div">
<button class="red-btn">Btn</button>
</div>
答案 1 :(得分:0)
不客气。
请记住,每当您需要某些东西来移动它的父对象时,它必须是绝对的,并且它的父对象必须是相对的。
.container{
position:relative;
width: 150px;
margin: 0 auto;
margin-top: 100px;
}
.container .badge{
position:absolute;
top:-25px;
right:-25px;
}
.container img{
width:100%;
height:auto;
}
.badge{
background-color:red;
border-radius:50%;
height:50px;
width:50px;
text-align:center;
}
<div class="container">
<span class="badge"></span>
<img src="http://via.placeholder.com/150x260"/>
</div>
答案 2 :(得分:0)
.round-close {
border-radius: 50%;
padding: 4px 6px;
position: absolute;
top: 0px;
left: 155px;
background-color: #900;
color: white;
}
<div>
<button class="round-close" aria-label="Eat cake">x</button>
<img class="pull-left" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOgAAADZCAMAAAAdUYxCAAABZVBMVEX////qQzU0qFNChfT7vAVsbGxvb29qamo3gPSjv/lycnJra2tmZmbr8v4pefPV1dX09PSIiIjj4+Pd3d3FxcXs7Oy9vb2BgYHLy8u0tLR5eXmSkpL7twCjo6PpMh/w8PAmpUqPj4+tra3pLBfpNyagoKAToT/oJQv629nqPS4zqz7P3fz8wwDW4vz2+/e53MHwhX/g6f2VzKFVs2xTjvVluHiyyfq/0vtbW1tDg/x2v4fj8eZbtXGSy54Up1frSDvpMjjwioTylpDzoZzsW1D2t7P97u3+7cz+5bWs1rX0rKlFrV/J5M7c7d9AiuRzoPY9ksE5nZT0kSA7l682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PQSHWbdCl6Ho2u0idacrJkFt+igxTwA+GZCxQYVkyiANBlKoFoiX1lAlaVS6ehJydr5kiaKchaodJodQwoQLciiPiaKqn4FZ+xK3iVadImKKCMOUPB9RZGF/TSy4jsKzviRivFx8sO9hYQoTbSB4scZub+ooKD7NSaKA7+DtixX1r0mCsDY3pEvVeVh5Zwj+XgCU7WiDHN2xU9UGuQONGO+oqidiUWdbq9VXqaIboKnqOZdT1WhNHzuW6hAJb4oXlLOD+b9QleVoHPLGwGh2yFjU5wEhvtHO0rTqb5wSvcULYGBUR6X/PmqHRALODPlDPubdr6PC9q3ABljseKbjACE5P152VdURZ/judGgnbybIhatSvbeXMQ7lacoihiR9KBWwKYqQCcOcG4ubjrx24HKJPmoiWK/YZpoe5HxbaeJohEDs1yqyJSsC+0nb/QALxbt+yfPl/WGZGBRdJtQ8ZUzAb6X3qKogdTRy030FZ3USVIzl2vKeC830SZfypWrAFQn8EQRIdlnA3ZSK0mbjqgyOPGAMt6wGxKuCWQDvY9FZXSXC+QSU1acAwagNOzmHQV/XdHyikJqXVS6osyJu7Q7LpP3yTclUhyU0TBQwQHsuiXnvCITa6LeZRHIkz6vq6YGgNa0s2/BdDZn1TSdPb5kEuMy+pZRwu8XnfeK5BKTN0s2+nAVlRsQaLqwYJqk45KGtifypII6yOMWgyKgbJpXwxg+zKhNA71yHtHznYoIG2xOmjgcDofD4XA4HA6Hw+FwOBwOh8PhcD5N/g/J5V5dJz1eXQAAAABJRU5ErkJggg=="
style="width: 160px; border: 1px solid #DEDEDC;" />
</div>
答案 3 :(得分:0)
我总是这样做:
.block {
margin: 20px;
position: relative;
width: 200px;
height: 200px;
background-color: #0ff;
font-size: 0;
}
.badge {
position: absolute;
top: 0;
right: 0;
width: 20px;
height: 20px;
border-radius: 50%;
font-size: 16px;
color: #fff;
text-align: center;
background-color: #f00;
transform: translate(50%, -50%);
}
img{
width: 100%;
height: 100%;
}
<div class="block">
<div class="badge">X</div>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/404.svg">
</div>
答案 4 :(得分:-1)
button {
position: absolute;
top: -5px;
right: -5px;
}
.wrapper {
position: relative;
}
button {
position: absolute;
top: -5px;
right: -5px;
}
<div class="wrapper">
<button></button>
<img />
</div>
答案 5 :(得分:-1)
这是一个简单的解决方案!使用相对位置/绝对位置。
您给父亲元素:position:relative
子position:absolute
和一些值(如(位置)):top,left,right,bottom
对于您的情况,您必须指定 top:margin 和 left:margin +图片宽度
我为您添加了一个代码段。
我希望你能理解。
img {
position: relative;
margin:50px;
}
button {
position: absolute;
left: 210px;
top:50px;
}
<div>
<img class="pull-left" src="https://i.stack.imgur.com/FEFDC.png" style="width: 160px; border: 1px solid #DEDEDC;"/>
<button class="" aria-label="Eat cake">Btn</button>
</div>
您可以在此处了解更多信息:https://www.w3schools.com/cssref/pr_class_position.asp