topskill.dev
CoursesTracks
0
All courses/CSSSticky scroll headings
Layout Fundamentals
9 / 10
← CSS
○CSS Rule Style an Exhibit Card○Write Your First CSS Rule○CSS Selectors Target Exhibit Details○Target Exhibit Details○Apply CSS Three Sources○Apply CSS Three Ways○Specificity Resolve a Style Conflict○Resolve Specificity○Inheritance and the cascade✦Module Quiz
○Color Palette○Color Palette○Background Layer○Background Layer○Type Rhythm○Type Rhythm○Borders and Shadows○Borders and Shadows✦Module Quiz
○Box Model Seed Packet Card○Space a Seed Packet○Display Swap Table Labels○Arrange Swap Table Labels○Positioning Pickup Pin○Pin the Pickup Location○Sizing and Overflow○Size a Seed-Swap Notice●Sticky scroll headings✦Module Quiz
○Advanced Selectors Book Cart Sign○Target a Book Cart Sign○Pseudo-Classes Night Cart States○Style Night Cart States○Pseudo-Elements○Add a Book-Quote Mark✦Module Quiz
○Lantern Market navigation○Lantern Market navigation✦Module Quiz
○Community garden request card○Community garden request card✦Module Quiz
○Flex Direction○Flex Direction○Main-Axis Spacing○Main-Axis Spacing○Cross-Axis Alignment○Cross-Axis Alignment○Wrapping and Flex Items○Wrapping and Flex Items○Flexbox Navigation○Flexbox Navigation✦Module Quiz
○Grid Tracks○Grid Tracks○Grid Gaps○Grid Gaps○Grid Placement○Grid Placement○Responsive Grid○Responsive Grid✦Module Quiz
○River radio schedule○Harbor radio schedule✦Module Quiz
○Field note color system○Field note color system○Transforms○Rotate a Field Note○Resilient styles✦Module Quiz
○Kite festival signal○Garden festival signal✦Module Quiz
○Garden forecast○Harbor forecast○Fluid sizing○Fluid weather bulletin○Images that fit✦Module Quiz
○Seed inventory table○Tool checkout table✦Module Quiz
○Community maker profile○Maker profile sheet✦Module Quiz

Sticky scroll headings

Keep the storage heading in view

The previous cards kept long text reachable through scrolling. This guide has several paragraphs inside one scrolling panel. Its heading should stay visible as those paragraphs pass beneath it.

position: sticky keeps the heading in normal flow, then offsets it when scrolling would carry it past a chosen edge. top: 0 sets that edge at the top of this scrollport. Unlike an absolute pin, the sticky heading still reserves its original space. It also stays bounded by its containing block; it cannot follow you indefinitely after that block ends.

The supplied panel has a fixed demonstration height and overflow: auto, so it owns the scrolling. Sticky positioning uses the nearest ancestor scrollport. An unexpected overflow rule on an ancestor can therefore change where sticking happens. There must be enough content to scroll and enough room for the sticky box to move.

For comparison, position: fixed leaves normal flow and usually attaches to the viewport; inside this course that would be the preview viewport. A transformed ancestor can establish a different fixed-position containing block. Fixed notices can cover text at zoomed sizes, so they are not a shortcut for this panel heading.

Which box paints on top?

Later paragraphs are positioned too. A z-index on the sticky heading gives it a higher local stacking level so they pass underneath it. Its solid background prevents the passing words from showing through.

A stacking context is a group painted as one unit relative to its siblings. Sticky boxes create one. A positioned box with a non-auto z-index, a transform, or opacity below 1 can create others. A child with z-index: 999 cannot escape its parent’s stacking context to outrank a higher sibling group. Fix the relevant parent relationship instead of continually increasing numbers. Flex and grid items can also use z-index without position: relative.

Your Task

  1. Set .scroll-heading to position: sticky.
  2. Set .scroll-heading to top: 0.
  3. Set .scroll-heading to z-index: 1 so it paints above the positioned paragraphs.

Edit style.css, Run, and scroll inside the bordered panel rather than the whole lesson. With all three changes, the heading stays at the panel’s top while the later paragraphs move underneath. Tab to the supplied scroll region and use the arrow keys as a second way to read it. Submit checks the declarations separately; scrolling is your visual check.

Take the Quiz →
Files
  • ⋄index.html
  • ⋄style.css
Loading editor…