[英]Fluid image adapts size according to screen size
I have the following HTML: 我有以下HTML:
<img src="https://www.sporedev.ro/pleiade/images/Maya.jpg" class="full-img">
<img src="https://www.sporedev.ro/pleiade/cufar.png" class="treasure treasure-maya">
And the following CSS: 以及以下CSS:
.full-img{
display: block;
width: 100%;
height: 100%;
height: 100vh;
}
.treasure{
height: 125px;
width: 120px;
}
.treasure-maya{
position: absolute;
top: 55%;
left: 44%;
z-index: 2;
}
The reason why I use img
instead of the CSS much simpler method, background-image
, is because the image is being used as an image map. 我之所以使用img
而不是CSS更为简单的方法background-image
的原因是因为该图像被用作图像映射。
Here is a JSFiddle. 这是一个JSFiddle。
Here is a link to my live project. 这是我正在进行的项目的链接。
I tried adapting the .treasure
image to the .full-img
so that, when the .full-img
is reducing in size, the .treasure
image keeps its position and ratio. 我尝试将.treasure
图像调整为.treasure
.full-img
以便在.full-img
缩小尺寸时, .treasure
图像保持其位置和比例。
I managed to find a solution, using media queries, but it's really counter-productive because I'd have to take into account a lot of different resolutions. 我设法使用媒体查询找到了一个解决方案,但这确实适得其反,因为我不得不考虑很多不同的分辨率。 I already added three media queries and that only takes care of some of the resolutions that I'd need to take into account (probably around 20 media queries). 我已经添加了三个媒体查询,并且只处理了一些我需要考虑的分辨率(大约20个媒体查询)。
I looked on Google and SO for a solution for my problem but I couldn't find an answer. 我在Google和SO上寻找问题的解决方案,但找不到答案。
What I'd need, is to somehow make the .treasure img
reduce in size together with the .full-img img
. 我需要的是使.treasure img
和.full-img img
一起减小尺寸。
Is this achievable? 这可以实现吗?
If there's a way to do this in CSS I'd prefer that. 如果有办法在CSS中做到这一点,我会更愿意。 If not, I guess that JS would be the only way to do it but, unfortunately, I'm a novice when it comes to JS. 如果不是这样,我猜想JS将是唯一的方法,但是不幸的是,我是JS的新手。
Set image width using percentage instead of fixed width. 使用百分比而不是固定宽度设置图像宽度。 I have removed the height for the image so the ratio is kept but you can change this if you need to 我已删除了图像的高度,因此可以保持比例,但是如果需要,可以更改此比例
For example 例如
body { padding: 0; margin: 0; } .full-img { display: block; width: 100%; height: 100%; height: 100vh; } .treasure { width: 15%; } .treasure-maya { position: absolute; top: 55%; left: 44%; z-index: 2; }
<!-- This is the background image (I can't implement it in the CSS way because I use it together with <map> in my app ) --> <img src="https://www.sporedev.ro/pleiade/images/Maya.jpg" class="full-img"> <!-- This is the image that needs to reduce its height and width, according to the full-img reduction --> <img src="https://www.sporedev.ro/pleiade/cufar.png" class="treasure treasure-maya">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.