I'm working on a React application and I want to overlay multiple links using <a>
tags on top of an image <img>
. Initially, this seemed easy but I ran into an issue - when the window is resized (on different devices, for example), the positioning of the links and the image scale differently.
To demonstrate this problem, I've created a JSFiddle. If you resize the window in the bottom right corner, you'll notice that the link doesn't stay "glued" to the image. How can I solve this?
Here's the functional component code from the fiddle:
<div class="parentDiv">
<div className={styles.imgWrapper}>
<img
className={styles.imgResponsive}
src="https://cdn.pixabay.com/photo/2018/02/08/22/27/flower-3140492__480.jpg"
/>
<div class="divLink">
<a href="some_url">Some link</a>
</div>
</div>
</div>
And here is the CSS being used:
.parentDiv {
height: 70vh;
width: 100vw;
}
.imgResponsive {
position:relative;
height: 100%;
width:auto;
top: 0;
left: 0;
}
.divLink {
position: absolute;
top: 20%;
left: 20%;
}
Any suggestions on how I can make the link stay attached to the image even after window resizing would be greatly appreciated! Thank you!