CSS position: sticky;

cssweb developmentfront end technology更新于 2025/6/4 18:52:17

position: sticky; 属性允许您根据滚动位置定位元素。当用户向下滚动时,将元素设置为粘在顶部。

示例

您可以尝试运行以下代码来实现 CSS 位置:粘性;

<!DOCTYPE html>
<html>
   <head>
      <style>
         div.sticky {
            position: -webkit-sticky;
            position: sticky;
            top: 0;
            padding: 10px;
            background-color: orange;
            border: 1px solid blue;
         }
      </style>
   </head>
   <body>
      <p>Scroll and see the effect!</p>
      <div class = "sticky">Sticky!</div>
      <div style = "padding-bottom:2000px">
         <p>This is demo text.</p>
         <p>Scroll down to view the sticky div.</p>
      </div>
   </body>
</html>

相关文章


有用资源