heml#将html设置为固定区域

我希望将网页的左侧固定死,不论滚轮怎么移动,左边始终都保持一样的内容,请问我应该怎么做?


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title></title>
  </head>

  <style></style>

  <body>
    <div
      style="
        /* 固定 */
        position: fixed;
        /* 固定离顶部距离 */
        top: 20px;
        /* 固定离左侧距离 */
        left: 0;
        height: 500px;
        width: 50px;
        background-color: yellow;
      "
    >
      固定内容
    </div>
    <div
      style="
        height: 2000px;
        background-image: linear-gradient(to bottom, red, pink);
      "
    ></div>
  </body>
  <script></script>
</html>

img


利用position:fixed 固定定位

要使用fixed定位
fixed元素的位置相对于浏览器窗口是固定位置,即使窗口是滚动的它也不会移动。

position: fixed;