CSS Sticky

Anonymous contributor's avatar
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.

  • 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

Syntax

position: sticky;
  • The position: sticky property is often used with length or percentage values applied to top,right, bottom, or left 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 */
}

The GIF illustrates how the header element remains sticky at the top of the viewport during scrolling.

All contributors

Contribute to Docs

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