我试图在图片中的特定位置修复按钮。有没有办法将按钮与图像的特定部分合并?这就是我到目前为止所拥有的 I have the button exactly where I want it but when the browser is stretched or zoomed in, the button doesn't stay.
这里是我所拥有的JSFiddle https://jsfiddle.net/vkfLywna/1/
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.container {
position: absolute;
width: 100%;
max-width: 400px;
}
.container img {
width: 200%;
height: auto;
}
.container .btn {
position: relative;
top: -330px;
left: 96px;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
color: white;
font-size: 6px;
padding: 4px 10spx;
border: none;
cursor: pointer;
border-radius: 5px;
text-align: center;
}
.container .btn:hover {
background-color: black;
}
</style>
</head>
<body>
<div class="container">
<img src="doc.png.png" alt="img" style="width:100%">
<button class="btn">Button</button>
</div>
</body>
</html>
&#13;
答案 0 :(得分:2)
用img
包裹button
和div
并相应地对其进行定位:
.img-container {
border: solid red 1px;
display: inline-block;
position: relative;
}
.img-container button {
position: absolute;
top: 50px;
left: 50px;
}
&#13;
<div class="img-container">
<img src="http://via.placeholder.com/350x150">
<button>Click here</button>
</div>
&#13;
答案 1 :(得分:0)
你不需要改变这一点。
.container {
position:relative;
width:100%;
}
.container img {
width:100%;
height:auto;
}
.container button {
position:absolute;
top:20%;
left:20%;
}
&#13;
<div class="container">
<img src="https://dummyimage.com/600x400/ff00ff/fff">
<button>
Click
</button>
</div>
&#13;