I want to create a fixed element which will be in the same position while scrolling down and up, but also will be relative in x-axis to a different div while resizing the window.
#blackbox {
width: 500px;
height: 2000px;
background-color: black;
color: white;
margin: 0 auto;
}
#floater {
width: 150px;
background-color: blue;
color: white;
position: fixed;
top: 50px;
right: 120px;
/* want here 10px on right from black box */
}
<html>
<div id="blackbox">
This is blackbox
<br> This is blackbox
<br> This is blackbox
<br> This is blackbox
<br> This is blackbox
<br>
</div>
<div id="floater">
Always 10px from black box
</div>
</html>
Perfect scenario when resolution is same as mine:
When screen resolution is smaller:
Whem screem resolution is bigger:
Edit: I've found the solution here.
#floater {
left: calc(50% + 510px); /* 50% + blackbox width + wanted 10px space *.
}