Sticky
Anonymous contributor
Published Nov 21, 2024
Contribute to Docs
Sticky Position in CSS allows an element to toggle between relative and fixed positioning based on the person’s scroll position. It commonly keeps certain elements, such as headers or navigation menus, in view when scrolling through content.
Syntax
position: sticky;
- The
position: sticky
property is often used with length or percentage values applied totop
,right
,bottom
, orleft
to define the threshold for switching between relative and fixed positioning.
Note: To ensure
position: sticky
works, the containing block must have a defined height, and the sticky element should not be covered by other positioned elements.
Example
Make the .header
element sticky so it remains visible at the top when scrolling past it.
.header {position: sticky;top: 0; /* Sticks to the top */background-color: white;padding: 10px;z-index: 1000; /* Ensure it's above other content */}
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn CSS on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn CSS
In this CSS tutorial, you’ll learn how to add CSS to visually transform HTML into eye-catching sites.Beginner Friendly6 hours