CSS Sticky
Anonymous contributor
Published Nov 21, 2024
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: stickyproperty is often used with length or percentage values applied totop,right,bottom, orleftto define the threshold for switching between relative and fixed positioning.
Note: To ensure
position: stickyworks, 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
Learn CSS on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- In this CSS tutorial, you’ll learn how to add CSS to visually transform HTML into eye-catching sites.
- Beginner Friendly.6 hours