html - 元素在居中元素之上的响应式定位

标签 html css responsive-design

我有一个居中的图像。最重要的是,我想放置各种可点击的点。我正在尝试以最灵敏的方式做到这一点,但到目前为止,我没有运气。任何人都可以提供一些见解吗?我不想为此被扣分,但我找不到答案。

这是我的 HTML:

<img src="images/Map.png" id="map" />
<img class="down" id="location1" src="images/square.png"/>
<img class="down" id="location2" src="images/square.png"/>
<img class="down" id="location3" src="images/square.png"/>
<img class="down" id="location4" src="images/square.png"/>

这是我的 CSS:

#map {
    position:relative;
    display: block;
    width:700px;
    margin-top:130px;
    margin-left:auto;
    margin-right:auto;
    height: 515px;
}
.down {
    width: 20px;
    height: auto;
    position: absolute;
    cursor:pointer;
}
#location1 {
    top: 230px;
    left: 39%;
}
#location2 {
    top: 240px;
    left: 38%;
}
#location3 {
    top: 410px;
    left: 60%;
}
#location4 {
    top: 390px;
    left: 59%;
}

最佳答案

我认为您需要将所有 HTML 包装在一个 div 中,并在该 div 上使用 position:relative。截至目前,您在图像上有 position:relative,但图像不是那些正方形的父级,因此 position:absolute 不会相对于该图像。

<div style="position:relative; width:700px; margin:0 auto;">
    <img src="images/Map.png" id="map" />
    <img class="down" id="location1" src="images/square.png"/>
    <img class="down" id="location2" src="images/square.png"/>
    <img class="down" id="location3" src="images/square.png"/>
    <img class="down" id="location4" src="images/square.png"/>
</div>

关于html - 元素在居中元素之上的响应式定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19033935/

相关文章:

html - 在 div 中堆叠元素时遇到问题

html - css中的滚动条拇指高度

javascript - 如果按下按键,Javascript

CSS图像缩放以适应区域内不扭曲

javascript - 即使使用直接 anchor URL,也会显示 Spinner,直到 Mathjax 加载

php - 如何在每个循环中增加 Jquery 变量

javascript - 一次在一个元素上调用自定义函数

image - 响应图像或不同的图片

html - 部分应用了 CSS 规则

html - 调整大小时在浏览器中测试时响应式设计不起作用