


Why Does Sticky Positioning Behave Differently Than the MDN Documentation Describes?
Scrutinizing the Discrepancy in Sticky Position Behavior
The MDN documentation portrays sticky positioning as an attribute that transitions elements from relative placement to fixed placement once a defined threshold is surpassed. However, in practice, it behaves differently. This discrepancy stems from a misunderstanding of the order in which these states are implemented.
According to MDN, sticky position elements initially behave like relative position elements until the threshold is exceeded. However, in the context where bottom:0 is specified, the threshold is already surpassed in the initial state. As a result, the element directly enters its fixed placement and remains as such until the element's position exceeds 0px from the bottom of the viewport.
Understanding Sticky Position States
Position:sticky has two distinct states:
- Relative State: The element behaves like a relatively positioned object.
- Fixed State: The element adheres to a fixed position against the viewport.
The initial state is determined by the element's placement and the defined threshold. In the case of bottom:0, the threshold is immediately surpassed, leading to an initial fixed state. As the viewport scrolls, the element will remain fixed until the distance from the bottom edge becomes greater than 0px. At this point, it reverts to its relative state.
Illustrating the Behavior
Consider the following code snippet:
body { height:150vh; margin:0; display:flex; flex-direction:column; border:2px solid; margin:50px; } .b { margin-top:auto; position:sticky; bottom:0; } .a { position:sticky; top:0; }
<div class="a"> I will start relative then I will be fixed </div> <div class="b"> I will start fixed then I will be relative </div>
Element A, with top:0, will initially behave as relative and transition to fixed when it reaches the top of the viewport. Element B, with bottom:0, will immediately start in a fixed position and transition to relative when the distance from the bottom of the viewport exceeds 0px.
The above is the detailed content of Why Does Sticky Positioning Behave Differently Than the MDN Documentation Describes?. For more information, please follow other related articles on the PHP Chinese website!

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
