修复按钮在图像

时间:2018-05-21 16:59:39

标签: javascript html css button

我试图在图片中的特定位置修复按钮。有没有办法将按钮与图像的特定部分合并?这就是我到目前为止所拥有的 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;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

img包裹buttondiv并相应地对其进行定位:

&#13;
&#13;
.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;
&#13;
&#13;

答案 1 :(得分:0)

<。> .container必须是position:relative和.btn position:absolute。你可以在容器中定位顶部和左侧的按钮,使用%而不是px来保持响应。

你不需要改变这一点。

&#13;
&#13;
.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;
&#13;
&#13;