如何将元素锚定到响应式图像的正确位置?
htmlweb developmentfront end technology更新于 2024/4/22 10:52:17
近年来,将锚点放置在响应式图像的正确位置变得越来越重要。由于我们在日常生活中会遇到很多广告,如果锚点没有正确地放置在响应式图像下方,用户将更难理解该网页。
我们使用 CSS 和 HTML 将元素锚定到响应式图像的正确位置。在深入研究本文以加深理解之前,让我们快速了解一下 HTML 中的锚点和图像标签。
HTML 中的锚点标签
HTML 元素 <a>(或锚元素)通过其 href 属性生成指向网页、文件、电子邮件地址、同一页面上的位置或任何其他可以通过 URL 寻址的内容的超链接。
每个 <a>应包含描述链接最终目标的文本。当焦点位于 <a> 元素上时,按下 Enter 键将激活 href 属性(如果存在)。
语法
以下是 HTML 中锚标签的语法
<a href = "link"> 链接名称 </a>
HTML <img> 标签
要在网页或网站内插入图片,请使用 HTML <img> 标签。在现代网站中,图片使用 <img> 元素链接到网页,该元素包含图片的空间。这可以防止网站直接将图片添加到网页。
语法
以下是 img 标签的语法
<img src="" alt="" width="" height="">
要了解有关将元素锚定到响应式图片上正确位置的更多信息,请查看以下示例
示例
下面,我们使用 CSS 将元素锚定到响应式图片上的正确位置。
<!DOCTYPE html>
<html>
<body>
<style>
.tutorial {
display: flex;
width: 60%;
margin: auto;
}
.type {
text-align: center;
}
.tutorial img {
max-width: 90%;
display: block;
}
</style>
<div class="tutorial">
<div class="type">
<img src="https://www.cainiaomax.com/java/images/java-mini-logo.jpg">
<a href="https://www.cainiaomax.com/java/index.htm">Java Tutorial</a>
</div>
<div class="type">
<img src="https://www.cainiaomax.com/html/images/html-mini-logo.jpg">
<a href="https://www.cainiaomax.com/html/index.htm">HTML Tutorial</a>
</div>
</div>
</body>
</html>
脚本执行后,将生成一个输出,其中包含上传到网页的图像以及其响应式图像底部附加的相应超链接。
示例
考虑以下示例,我们将元素的锚点放置在响应式图像的正确位置。
<!DOCTYPE html>
<html>
<body>
<style>
#tutorial{
float:left;
position: relative;
}
#tutorial img {
max-width: 100%;
display: inline-block;
}
a.link1{
height:15%;
width:15%;
position: absolute;
top:60%; left:10%;
display:block;
background:#00FF00 ;
}
a.link2{
height:15%;
width:15%;
position: absolute;
top:50%;
left:76%;
display: block;
background:#FF0000;
}
</style>
<div id="tutorial">
<div>
<img src="https://www.math-english.com/media/dices/two-dices2.png">
</div>
<a href="https://www.cainiaomax.com/html/index.htm" class="link1">HTML</a>
<a href="https://www.cainiaomax.com/java/index.htm" class="link2">JAVA</a>
</div>
</body>
</html>
运行上述脚本后,将弹出输出窗口,显示上传到网页的图像以及附加在图像两侧的超链接,并在超链接上应用了 CSS。
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

