css - invariant scaling for CSS based image map
问题描述
I'm using a CSS based image-map, which I want to display correctly whatever the size of the browser window. Of course there are actually multiple links.
My HTML ...
<div id="sitemap" >
<img src="img.jpg" class="center"/>
<a href="url1.html" id='id1'></a>
</div
And the CSS ...
#sitemap img{
max-width: 100vw;
max-height: 100vh;
position: relative;
}
#sitemap a {
display: block;
position: absolute;
}
#sitemap a:hover {
background: rgba(255, 255, 0, 0.5);
border-radius: 20px;
}
a#archive {
top: 48%;
margin-left: 14%;
width: 20%;
height: 15%;
}
This works great in a tall, narrow browser, but when the browser window is wider than it is tall, the percentages consider the dead space in the blank sidebars. How can I make the percentages consider only the actuall image?
解决方案
这样你就知道原因了。这是因为 div(id=sitemap) 的宽度。这个怎么样?
#sitemap {
/* for debug background-color: red; */
/* make sure the div width only size of contents */
display: inline-flex;
/* You set position relative to "img", but it semmed doesn't work because it isn't a parent‐child relationship */
position: relative;
}
#sitemap img{
max-width: 100vw;
max-height: 100vh;
/* position: relative; */
}
a#archive {
/* I think it's good enough setting two properties, unless you aren't particular about the details. */
top: 10%;
left: 10%;
}
推荐阅读
- java - HBase 中的过渡区域有哪些?
- mysql - 为什么 pymysql 会挂在在 MySQL 工作台中运行良好的 select 语句上?
- javascript - 将属性传输到组件的简单方法
- c# - C# 我可以启动一个线程并使用调度程序来安排它的工作吗?
- java - XtextdoGenerate 在作为 warfile 运行时返回 xtend 符号
- azure-devops - 寻找回购分支令牌
- javascript - RXJS 立即发出并重试
- python - 使用 Python 的具有 p 值的 Seaborn 相关矩阵
- c# - 从各种 BoxCollider2D 计算总区域高度和宽度
- java - 警报消息验证 - 测试自动化 (Selenium)