1
我想要創建一個固定的元素,它將在向下滾動時位於同一位置,但在調整窗口大小時,也會在x軸上與另一個div相對。與另一個div相關的固定div
#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>
編輯: 我找到了解決方案here。
#floater {
left: calc(50% + 510px); /* 50% + blackbox width + wanted 10px space *.
}
你能告訴我們你什麼工作? –